数据库查询插入不正常

时间:2016-06-03 05:00:50

标签: java mysql

status的值是false而不是true。我在数据库中单独使用了相同的插入查询,但它正在工作,但在此不是。我要插入值的表包含4列departmentiddepartmentnamecompanyplacedepartmentid是自动增量。有人可以纠正它。

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;
}  

}

1 个答案:

答案 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());