JDBC、MySQL:从执行的PreparedStatement获取行数据
我正在使用以下设置:
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");
更新长度、顶点、活动的循环内。那么...什么给出了?我是否误解了prepareStatement(sqlstring, string[]) 方法的作用?
I'm using the following setup:
public MySQLProcessWriter(Connection con) throws SQLException {
String returnNames[] = {"processId","length","vertices"};
addresser = con.prepareStatement("INSERT INTO addressbook (length, vertices, activity) VALUES (?, ?, ?)", returnNames);
}
processId
corresponds to an auto-incrementing column in the addressbook table. So the idea is: I have a repeated insert, I get back some of what was inserted + the auto-generated processId. However, I'm getting a "column not found" SQLException when I attempt to addresser.getGeneratedKeys().getInt("processId");
after executing the prepared statement (after the appropriate setting of values). The code for that is
addresser.setInt(1, length);
addresser.setInt(2, vertices);
addresser.setDouble(3, activity);
addresser.executeUpdate();
int processId = addresser.getGeneratedKeys().getInt("processId");
inside a loop that is updating length, vertices, activity. So...what gives? Am I misunderstanding what the prepareStatement(sqlstring, string[]) method does?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你需要对返回的结果集调用 next()
I think you need to call next() on the returned result set
在调用
getInt()
之前,您必须对从getGenerateKeys()
返回的ResultSet
调用next()
方法You have to call
next()
method onResultSet
returned fromgetGeneratedKeys()
prior to callinggetInt()