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.
这是我的数据库表 展开
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()) {
ResultSetMetaData md = rs.getMetaData();
int num = md.getColumnCount();
List listOfRows = new ArrayList();
while (rs.next()) {
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询