package excute和excuteupdate; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class excute { //使用预编译来完成 public static void main(String[] args) { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } String sqlInsert = "insert into user value(?,?)"; //查询的sql String sqlquery = "select * from user where id =?"; try ( Connection c = DriverManager.getConnection( "jdbc:mysql://127.0.0.1:3306/hero?characterEncoding=UTF-8", "root", "123"); //填入sql PreparedStatement ps = c.prepareStatement(sqlquery); ) { /* excute和excuteupdate的区别 * 1 * 相同之处: * 都可以执行增删改操作 * 2 * 不同之处: * excute可以在执行完查询后,通过getResultSet()得到结果集 * excuteupdate则不能得到 * * excute返回值是boolean,代表执行的是否是查询语句,是查询为true否则为false * excuteupdate返回值是int类型,表示影响到了多少条数据 * */ ps.setInt(1, 1); //执行查询操作 ps.execute(); //得到结果集 ResultSet rs = ps.getResultSet(); //替代原来的下边一个步骤的这种得到结果集方式 // ResultSet rs = ps.executeQuery(); while(rs.next()) { int i = rs.getInt(1); String name = rs.getString(2); System.out.printf("%d : %s%n",i,name); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
JDBC中excute和excuteupdate的区别
猜你喜欢
转载自blog.csdn.net/dzz_bc/article/details/80184687
今日推荐
周排行