JDBC与SQL 最基本的求教一下!!!!谢谢了!!
要实现下面功能
// TODO: Update db with a prepared statement
(SQL: UPDATE Students SET grade = 'C ' WHERE name = ? )
// You need to print out "name " "grade "
表如下
id name grade
007 James Bond A
008 chen B
上面这个我不知道怎么实现...执行更新的语句还要返回resultset 这个好象比较困难....我实现不了.....
final String valueUpdate= "UPDATE Students SET grade = 'C ' WHERE name = ? ";
PreparedStatement valueUpdatestmt=con.prepareStatement(valueUpdate);
valueUpdatestmt.setString(1, "chen ");
//valueUpdatestmt.executeUpdate();//执行更新语句,可是不返回行集
//rs=valueUpdatestmt.executeQuery(valueUpdate);这句执行有错误..
while(rs.next()){
System.out.println( "the name is "+rs.getString(2)+ "the grade is " +rs.getString(3));
}
不知道什么实现上面那个功能 既实现更新又有行集返回...
希望了解的帮帮忙......
------解决方案--------------------executeQuery 中放select语句即可
String valueUpdate= "UPDATE Students SET grade = 'C ' WHERE name = ? ";
PreparedStatement valueUpdatestmt=con.prepareStatement(valueUpdate);
valueUpdatestmt.setString(1, "chen ");
valueUpdatestmt.executeUpdate();
PreparedStatement pstmt=con.prepareStatement( "select * from Students where name =? ");
pstmt.setString(1, "chen ");
rs=pstmt.executeQuery();
由于只有1个结果,这里用if比较好
if(rs.next()){
System.out.println( "the name is "+rs.getString(2)+ "the grade is "+rs.getString(3));
}