mysql缺少主键无法更新结果集

importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStateme... import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ResultSetTest {
private String driver = "com.mysql.jdbc.Driver";
private String url = "jdbc:mysql://127.0.0.1:3306/teacher";
private String user = "root";
private String pass = "123456";
public Connection getConnection(){
Connection conn=null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url,user,pass);
} catch (ClassNotFoundException | SQLException e) {

e.printStackTrace();
}
return conn;
}
public void query(String sql){
PreparedStatement pstmt = null;
ResultSet rs=null;
Connection conn = this.getConnection();
try {
pstmt = conn.prepareStatement(sql,
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
rs = pstmt.executeQuery();
rs.last();
int rowCount = rs.getRow();
for(int i = rowCount;i > 0 ; i--){
rs.absolute(i);
System.out.println(rs.getInt(1)+"\t"+rs.getString(2));

rs.updateString(2, "teacher name"); //此处报错,错误原因在下面
rs.updateRow();
}

} catch (SQLException e) {
e.printStackTrace();
}
try {
rs.close();
pstmt.close();
conn.close();
} catch (SQLException e) {

e.printStackTrace();
}
}
public static void main(String[] args) {
ResultSetTest rt = new ResultSetTest();
rt.query("select tid,tname from teacher_table1");
}
}
com.mysql.jdbc.NotUpdatable: Result Set not updatable (referenced table has no primary keys).This result set must come from a statement that was created with a result set type of ResultSet.CONCUR_UPDATABLE, the query must select only one table, can not use functions and must select all primary keys from that table. See the JDBC 2.1 API Specification, section 5.6 for more details.
这是我的数据库表
展开
 我来答
匿名用户
2015-07-31
展开全部
public List extractData(ResultSet rs) throws SQLException {
ResultSetMetaData md = rs.getMetaData();
int num = md.getColumnCount();
List listOfRows = new ArrayList();
while (rs.next()) {
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式