gpt4 book ai didi

java - 检查该用户是否已经存在于struts2中

转载 作者:行者123 更新时间:2023-11-30 08:50:04 25 4
gpt4 key购买 nike

如果用户尝试使用我的表中已经存在的代码进行注册,那么它应该提供一个错误

This code is already taken please try with different one.

使用 Struts2 我想这样做。下面是我的 Action 类。

我所做的是:首先,我触发了一个使用结果集选择代码的查询,并将所有结果存储在字符串变量 temp_uservalidate() 方法中我刚刚使用 equals(temp_user) 方法对其进行了比较,但它不起作用。

注册操作.java

package org.entity;

import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.wsdl.Message;

import org.apache.struts2.interceptor.SessionAware;

import nl.captcha.Captcha;

import com.opensymphony.xwork2.ActionSupport;

public class RegistrationAction extends ActionSupport implements SessionAware {
BloodBank register;
private Map<String, Object> session;
private String temp_user;

public BloodBank getRegister() {
return register;
}

public void setRegister(BloodBank register) {
this.register = register;
}

public Map<String, Object> getSession() {
return session;
}

public void setSession(Map<String, Object> session) {
this.session = session;
}

public String getTemp_user() {
return temp_user;
}

public void setTemp_user(String temp_user) {
this.temp_user = temp_user;
}

@Override
public String execute() throws Exception {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/foryou", "root", "siddheshkk");
System.out.println("Driver Loaded");
Statement st12 = con.createStatement();
PreparedStatement st = con
.prepareStatement("insert into bbinfo(code,name,address,city,district,contactno,stdcode,password,aname,email) "
+ "values(?,?,?,?,?,?,?,?,?,?)");
st.setString(1, register.getCode());
st.setString(2, register.getBbname());
st.setString(3, register.getAddress());
st.setString(4, register.getCity());
st.setString(5, register.getDistrict());
st.setString(6, register.getNumber());
st.setString(7, register.getStd());
st.setString(8, register.getPassword());
st.setString(9, register.getAname());
st.setString(10, register.getEmail());
st.executeUpdate();
st.close();

PreparedStatement st1 = con
.prepareStatement("insert into stockinfo(name,city,address,contact,email,code,stdcode,apositive,anegative,bpositive,bnegative,abpositive,abnegative,opositive,onegative)"
+ "values(?,?,?,?,?,?,?,'0','0','0','0','0','0','0','0')");
st1.setString(1, register.getBbname());
st1.setString(2, register.getCity());
st1.setString(3, register.getAddress());
st1.setString(4, register.getNumber());
st1.setString(5, register.getEmail());
st1.setString(6, register.getCode());
st1.setString(7, register.getStd());
st1.executeUpdate();
st1.close();
System.out.println("done");
ResultSet rs12 = st12.executeQuery("select code from bbinfo");
while (rs12.next()) {
temp_user = rs12.getString("code");
}

con.close();
} catch (Exception e) {
e.printStackTrace();
}
return "success";
}

@Override
public void validate() {
super.validate();
if (register.getCode().length() < 1 | register.getCode().equals("")) {
addFieldError("register.code", "Mandatory Field.");
}
if(register.getCode().equals(getTemp_user()))
{
addFieldError("register.code", "This Code is already taken. Please try different one.");
}
if (register.getBbname().equals("") | register.getBbname().length() < 1) {
addFieldError("register.bbname", "Invalid Name");
}
if (register.getAddress().equals("")) {

addFieldError("register.address", "Invalid Address");
}
if (register.getCity().equals("")) {
addFieldError("register.city", "Invalid City");

}
if (register.getDistrict().equals("")) {
addFieldError("register.district", "Invalid District");

}
if (register.getPassword().length() > 8
| register.getPassword().equals("")) {
addFieldError("register.password", "Please Enter a valid Password");
}
if (!(register.getConfirm().equals(register.getPassword()))) {
addFieldError("register.confirm", "Password doesnt match");
}
if (register.getConfirm().isEmpty()) {
addFieldError("register.confirm", "Please enter confirmed password");
}
if (register.getAname().equals("")) {
addFieldError("register.aname", "Please enter your name");
;
}
if (register.getEmail().equals("")) {
addFieldError("register.email", "Invalid Email Id");
} else {
String expression = "^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
CharSequence inputStr = register.getEmail();
Pattern pattern = Pattern.compile(expression,
Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inputStr);
if (!matcher.matches())
addFieldError("register.email", "Invalid email address");
}
Captcha captcha = (Captcha) session.get(Captcha.NAME);
if (!(captcha.isCorrect(register.getCaptchaAnswer()))) {
addFieldError("register.captchaAnswer", "incorrect captcha");
}

}
}

最佳答案

感谢@Andrea Sir 的建议,这对我很有帮助。 :)我在 Validate() 方法中编写了以下代码,并且我的代码已成功执行。再次感谢先生 :) 并且您发布的链接也清除了我关于如何在 JSP 和 Action 之间调用拦截器的概念。 :) 下面是我的代码。

String temp_user = null;
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/foryou", "root", "siddheshkk");
System.out.println("Step 1");
Statement st12 = con.createStatement();
ResultSet rs12 = st12.executeQuery("select code from bbinfo");
while (rs12.next()) {

temp_user = rs12.getString("code");
System.out.println("step2");
if (register.getCode().equals(temp_user)) {
addFieldError("register.code",
"This username is already taken .Try Different one !");
System.out.println("sid");
}

}
st12.close();
} catch (Exception e) {
e.printStackTrace();
}

关于java - 检查该用户是否已经存在于struts2中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31165266/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com