获取Access数据库中的下一个ID(现在ID为M15,我想获得M16)

时间:2013-07-08 08:57:49

标签: java

My Access Database memberID现在是M15,我想获得下一个memberID M16,如何获取它并在文本字段中显示?

字符串查询;

query = "INSERT INTO Membership VALUES ('" +memberID_a.getText() + "','" +memberName_a.getText() + "','" + icNo_a.getText()  + "','" + gender_a.getText()  +"','" + birthday_a.getText()  +"','" + telephoneNo_a.getText()  + "','" + mobileNo_a.getText()  + "','" + email_a.getText()  + "','" + address_a.getText()  + "','" + postalCode_a.getText() + "','" + state_a.getText()  + "','" + country_a.getText()  + "','" + memberPoint_a.getText() + "');"; 



try{

        stmtInsert = conn.createStatement(); 
        stmtInsert.executeUpdate(query);

} catch(Exception ex){
            JOptionPane.showMessageDialog(null,"ERROR"+ex.toString(),"ERROR",JOptionPane.ERROR_MESSAGE);
}

1 个答案:

答案 0 :(得分:0)

query = "INSERT INTO Membership VALUES ('" +memberID_a.getText() + "','" +memberName_a.getText() + "','" + icNo_a.getText()  + "','" + gender_a.getText()  +"','" + birthday_a.getText()  +"','" + telephoneNo_a.getText()  + "','" + mobileNo_a.getText()  + "','" + email_a.getText()  + "','" + address_a.getText()  + "','" + postalCode_a.getText() + "','" + state_a.getText()  + "','" + country_a.getText()  + "','" + memberPoint_a.getText() + "');";

永远不要那么做。

http://en.wikipedia.org/wiki/SQL_injection

改为使用

query = "INSERT INTO Membership VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)";

每个?是一个占位符。

http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html

无论如何,要实现数据库表的重要一点是,除非在查询中使用“order by”子句,否则行的顺序是没有意义的。因此,如果您想通过id列进行排序,请使用

SELECT * from Membership ORDER BY id;