在JAVA中完成MySQL准备语句

时间:2012-12-19 21:55:06

标签: java mysql prepared-statement

我正在使用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(); 

2 个答案:

答案 0 :(得分: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(); 

请务必妥善关闭您的陈述。

答案 1 :(得分: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 tutorial