如何插入具有主键自动增量的表? 我想使用预处理语句,我不断收到此错误0< 1 ... 我尝试了声明并且它有效:S
public void insertDobavitelj (String dobavitelj,String naslov, String telefon) {
String query = "INSERT INTO dobavitelj(ime,naslov,telefon) VALUES ('"+dobavitelj+"','"+naslov+"','"+telefon+"')";
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
Statement stmt = conn.createStatement();
stmt.executeUpdate(query);
/*stmt = conn.prepareStatement(query);
// stmt.setInt(1, 0);
stmt.setString(0, dobavitelj);
stmt.setString(1, naslov);
stmt.setString(2, telefon);
if (stmt.executeUpdate() == 1) {
JOptionPane.showMessageDialog(frame, "Uspesno ste dodali novega dobavitelja");
}
*/
}catch (ClassNotFoundException e) {
JOptionPane.showMessageDialog(frame,"Class not found - insert dobavitelj" );
}catch (SQLException exception) {
JOptionPane.showMessageDialog(frame, "SQL Exception - insert dobavitelj");
exception.printStackTrace();
}
我试过了:
"INSERT INTO dobavitelj(ime,naslov,telefon) VALUES ('?'.'?','?')"
"INSERT INTO dobavitelj(idDobavitelja,ime,naslov,telefon) VALUES (?.'?'.'?','?')"
Thanx好人:)
答案 0 :(得分:0)
您使用stmt.setString(0, dobavitelj);
且索引为零,语句索引从1开始。
同时将您的查询语法更改为:INSERT INTO dobavitelj(ime,naslov,telefon) VALUES ('?'.'?','?')
答案 1 :(得分:0)
您可以创建一个随机数变量,您将在mysql的自动增量列中插入该变量。将随机数乘以1000得到999个值或10000得到9999个值限制;
int randomnumber = (int) math.random() * 1000
statement.setInt(1, randomnumber)