为什么我的Jsp程序,JavaBean不能把数据插入数据库
JavaBeanpackageU_14_java;importjava.sql.*;publicclasszhuce{intn;intm=0;Stringusername...
JavaBean
package U_14_java;
import java.sql.*;
public class zhuce {
int n;
int m=0;
String username;
String password;
String repassword;
String email;
String question;
String reply;
String un[];
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public String getRepassword() {
return repassword;
}
public void setRepassword(String repassword) {
this.repassword = repassword;
}
public String getReply() {
return reply;
}
public void setReply(String reply) {
this.reply = reply;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public void SQL() {
/*finally {
//关闭语句和数据库连接
try {
if (conn != null) conn.close();
} catch(SQLException e) {
System.out.println("关闭数据库连接时出现异常");
}
}*/
}
public boolean GetName() throws SQLException {
boolean name = false;
String sql = "";
Connection conn = null;
Statement stmt = null;
sql="select username from userinfo";
ResultSet rs ;
String url = "jdbc:mysql://localhost/user";
String userName = "root";
String Password = "root";
try {
Class.forName("com.mysql.jdbc.Driver");
} catch(ClassNotFoundException e) {
System.out.println("加载驱动器类时出现异常");
}
try {
conn = DriverManager.getConnection(url, userName, Password);
//创建Statement语句
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
un[m]= rs.getString(1);
m++;
}
// 判断如果username与数组un里面的一个相同,则返回true否则返回false
name=BiJiao(un,username);
if(!name&&password.equals(repassword)){
sql="INSERT INTO userinfo(id,username, password, email, question,reply) VALUES("+"'"+m+"'"+"'"+username+"'"+","+"'"+password+"'"+"," +"'"+email+"'"+","+ "'"+question+"'"+","+"'"+reply+"'"+")";
stmt.executeUpdate(sql);
}
} catch(SQLException e) {
System.out.println("出现SQLException异常");
}finally {
//关闭语句和数据库连接
try {
if (conn != null) conn.close();
} catch(SQLException e) {
System.out.println("关闭数据库连接时出现异常");
}
}
return name;
}
public boolean BiJiao(String[] str,String name){
boolean bl = false;
for(int i=0;i<str.length;i++){
if(name.equals(str[i])){
bl=true;
break;
}else
{
bl=false;
}
}
return bl;
}
} 展开
package U_14_java;
import java.sql.*;
public class zhuce {
int n;
int m=0;
String username;
String password;
String repassword;
String email;
String question;
String reply;
String un[];
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public String getRepassword() {
return repassword;
}
public void setRepassword(String repassword) {
this.repassword = repassword;
}
public String getReply() {
return reply;
}
public void setReply(String reply) {
this.reply = reply;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public void SQL() {
/*finally {
//关闭语句和数据库连接
try {
if (conn != null) conn.close();
} catch(SQLException e) {
System.out.println("关闭数据库连接时出现异常");
}
}*/
}
public boolean GetName() throws SQLException {
boolean name = false;
String sql = "";
Connection conn = null;
Statement stmt = null;
sql="select username from userinfo";
ResultSet rs ;
String url = "jdbc:mysql://localhost/user";
String userName = "root";
String Password = "root";
try {
Class.forName("com.mysql.jdbc.Driver");
} catch(ClassNotFoundException e) {
System.out.println("加载驱动器类时出现异常");
}
try {
conn = DriverManager.getConnection(url, userName, Password);
//创建Statement语句
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
un[m]= rs.getString(1);
m++;
}
// 判断如果username与数组un里面的一个相同,则返回true否则返回false
name=BiJiao(un,username);
if(!name&&password.equals(repassword)){
sql="INSERT INTO userinfo(id,username, password, email, question,reply) VALUES("+"'"+m+"'"+"'"+username+"'"+","+"'"+password+"'"+"," +"'"+email+"'"+","+ "'"+question+"'"+","+"'"+reply+"'"+")";
stmt.executeUpdate(sql);
}
} catch(SQLException e) {
System.out.println("出现SQLException异常");
}finally {
//关闭语句和数据库连接
try {
if (conn != null) conn.close();
} catch(SQLException e) {
System.out.println("关闭数据库连接时出现异常");
}
}
return name;
}
public boolean BiJiao(String[] str,String name){
boolean bl = false;
for(int i=0;i<str.length;i++){
if(name.equals(str[i])){
bl=true;
break;
}else
{
bl=false;
}
}
return bl;
}
} 展开
2个回答
展开全部
username 这个比较的变量没有定义啊, 我没找到么?
补充下报错没有吧! 如果报错把错误贴出来!
还有这句:
// 判断如果username与数组un里面的一个相同,则返回true否则返回false
name=BiJiao(un,username);
if(!name&&password.equals(repassword)){
如果 name 返回的是true 也是不执行的.
另外, 直接用个sql 就出来了, 何必要写个BIJiao去比较呢?
sql="select username from userinfo";
换成
sql="select username from userinfo where username =" + username;
补充下报错没有吧! 如果报错把错误贴出来!
还有这句:
// 判断如果username与数组un里面的一个相同,则返回true否则返回false
name=BiJiao(un,username);
if(!name&&password.equals(repassword)){
如果 name 返回的是true 也是不执行的.
另外, 直接用个sql 就出来了, 何必要写个BIJiao去比较呢?
sql="select username from userinfo";
换成
sql="select username from userinfo where username =" + username;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询