0

我正在用 JAVA 学习 MySQL,但不理解准备好的语句。从我将学习它中抽象出来,我想寻求帮助以完成此代码以“准备说明”:-)

String stringQuery = "INSERT INTO banlist (name, reason, admin, time, temptime, IP) VALUES (testNick, testPowod, testAdmin, CURRENT_TIMESTAMP, NOW(), NULL);=?";

PreparedStatement statement = this.connection.prepareStatement( stringQuery );
statement.setString( 1,  ); // after ' 1, ' we define what we want to get
ResultSet resultSet = statement.executeUpdate(); 
4

2 回答 2

1

这是我的做法:

String insertQuery = "INSERT INTO banlist(name, reason, admin, time, temptime, IP) VALUES (?, ?, ?, ?, ?, ?)";
PreparedStatement statement = this.connection.prepareStatement( stringQuery );
statement.setString(1, name);  // These values come from your code; dynamic
statement.setString(2, reason);
statement.setString(3, admin);
statement.setString(4, time);
statement.setString(5, tempTime);
statement.setString(6, ip); 
int numRowsAffected = statement.executeUpdate(); 

一定要适当地结束你的陈述。

于 2012-12-19T21:59:05.720 回答
1
String stringQuery = 
    "INSERT INTO banlist (name, reason, admin, time, temptime, IP)"
    + " VALUES (?, ?, ?, CURRENT_TIMESTAMP, NOW(), NULL)";

PreparedStatement statement = this.connection.prepareStatement(stringQuery);
statement.setString(1, testNick);
statement.setString(2, testPowod);
statement.setString(3, testAdmin);
int inserted = statement.executeUpdate();

阅读JDBC 教程

于 2012-12-19T22:00:33.643 回答