我正在使用以下设置:
public MySQLProcessWriter(Connection con) throws SQLException {
String returnNames[] = {"processId","length","vertices"};
addresser = con.prepareStatement("INSERT INTO addressbook (length, vertices, activity) VALUES (?, ?, ?)", returnNames);
}
processId
对应于地址簿表中的自动递增列。所以想法是:我有一个重复的插入,我取回一些插入的内容+自动生成的processId。但是,当我在执行准备好的语句(在适当设置值之后)后尝试 addresser.getGenerateKeys().getInt("processId");
时,出现“未找到列”SQLException 。其代码是
addresser.setInt(1, length);
addresser.setInt(2, vertices);
addresser.setDouble(3, activity);
addresser.executeUpdate();
int processId = addresser.getGeneratedKeys().getInt("processId");
在更新长度、顶点、 Activity 的循环内。那么...什么给出了?我是否误解了prepareStatement(sqlstring, string[])方法的作用?
最佳答案
我认为你需要对返回的结果集调用 next()
ResultSet keys = addresser.getGeneratedKeys();
int processId = -1;
if (keys.next())
{
processId = keys.getInt("processId");
}
关于java - JDBC、MySQL : getting back row data from PreparedStatement executes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1604931/