将eclipse连接到ODBC驱动程序时计算字段不正确的错误

时间:2012-12-12 06:31:17

标签: java eclipse swing ms-access

我在尝试使用microsoft access和eclipse创建一个记录到数据库时遇到了这个错误。

    [Microsoft][ODBC Microsoft Access Driver]COUNT field incorrect

Connection显示已关闭。这是我的代码连接到ODBC驱动程序。

    public class DBController {
private Connection con;

public void setUp(String dsn) {
    try {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");          
    } catch (Exception e) {
        System.out.println("Load driver error");
    }
    try {

        String s = "jdbc:odbc:" + dsn;
        con = DriverManager.getConnection(s, "", "");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public ResultSet readRequest(String dbQuery) {
    ResultSet rs = null;
    try {
        Statement stmt = con.createStatement();
        rs = stmt.executeQuery(dbQuery);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return rs;
}

public int updateRequest(String dbQuery) {
    int count = 0;
    try {
        Statement stmt = con.createStatement();
        count = stmt.executeUpdate(dbQuery);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return count;
}

public void terminate() {
    try {
        con.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

这些是我创建用户的代码。

        public boolean createUser() {
    boolean success = false;
    DBController db = new DBController();
    db.setUp("IT Innovation Project");
    String sql = "INSERT INTO forumUsers(users_userName,users_password,users_NRIC,users_securityQuestion,users_answer) ";
    sql += "VALUES ('" + userName + "','" + pwd + "','" + nric + "','"
            + securityQuestion + "','" + answer + "')";
    System.out.println(sql);
    if (db.updateRequest(sql) == 1)
        success = true;
    db.terminate();
    return success;
}

我已经按照数据库表列重新排列序列,但它仍然显示计数字段不正确的错误。那么我该如何解决呢?任何帮助将不胜感激。 这就是我的挥杆。

    public void submitSignup(ActionEvent e) {
    String userName = jTextField_userName.getText();
    String pwd = new String(jPasswordField.getPassword());
    String nric = jTextField_nric.getText();
    String securityQuestion = jComboBox_securityQuestion.getSelectedItem()
            .toString();
    String answer = jTextField_answer.getText();

    if (userName.equals("") || pwd.equals("") || nric.equals("")
            || securityQuestion.equals("") || answer.equals(""))
        JOptionPane.showMessageDialog(null, "Please enter all text field");
    else {
        eForumUser user = new eForumUser(userName, pwd, nric,
                securityQuestion, answer);
        if (user.isExist())
            JOptionPane.showMessageDialog(null,
                    "User already exists! Cannot create.");
        else {
            if (user.createUser() == true)
                ;
            JOptionPane.showMessageDialog(null,
                    "User record created successfull!");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我认为应该是:

  String sql = "INSERT INTO forumUsers(users_userName,users_password,users_NRIC,users_securityQuestion,users_answer) " +
         "VALUES (?,?,?,?,?)";
            System.out.println(sql);
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1,users_userName);
            ps.setString(2,users_password);
            ps.setString(3,users_NRIC);
            ps.setString(4,users_securityQuestion);
            ps.setString(5,users_answer);
            int checks = ps.executeUpdate();
            if(checks != 0){                
                  success = true;                       
            }
            else{
              System.out.println("Failed to insert user data");
            }
 db.terminate();