status的值是false而不是true。我在数据库中单独使用了相同的插入查询,但它正在工作,但在此不是。我要插入值的表包含4列departmentid
,departmentname
,company
,place
和departmentid
是自动增量。有人可以纠正它。
package bean;
import java.sql.*;
public class LoginDao {
public static boolean validate(LoginBean bean){
boolean status=false;
try{
Connection con=ConnectionProvider.getCon();
String sql="insert into department(departmentname,company,place) values (?,?,?)";
PreparedStatement pstmt =con.prepareStatement(sql);
pstmt.setString(2,bean.getdepartment());
pstmt.setString(3,bean.getcompany());
pstmt.setString(4, bean.getplace());
int rs=pstmt.executeUpdate();
if(rs>0){status=true;}
}catch(Exception e){}
return status;
}
}
在
pstmt.setString(2,bean.getdeparment());
pstmt.setString(3,bean.getcompany());
pstmt.setString(4, bean.getplace());
我将索引值分别更改为1,2,3,但仍然无效。
此文件是loginbean,getdepartment(),getcompany,getplace方法来自。 包豆;
public class LoginBean {
private String company,department,place;
public String getcompany() {
return company;
}
public void setcompany(String company) {
this.company = company;
}
public String getdepartment() {
return department;
}
public void setdepartment(String department) {
this.department = department;
}
public String getplace() {
return place;
}
public void setplace(String place) {
this.place = place;
}
}
答案 0 :(得分:3)
Hello尝试使用此代码,并确保插入与Department表列具有相同DataType的值,并希望它能够正常工作。
String sql="insert into department values(?,?,?)";
PreparedStatement pstmt =con.prepareStatement(sql);
pstmt.setString(1,bean.getdepartment());
pstmt.setString(2,bean.getcompany());
pstmt.setString(3, bean.getplace());