和 Statement一样,PreparedStatement也是用来执行sql语句的
与创建Statement不同的是,需要根据sql语句创建PreparedStatement
除此之外,还能够通过设置参数,指定相应的值,而不是Statement那样使用字符串拼接
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class TestJDBC { public static void main(String[] args) { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } String sql = "insert into hero values(null,?,?,?)"; try (Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8","root", "admin"); // 根据sql语句创建PreparedStatement PreparedStatement ps = c.prepareStatement(sql); ) { // 设置参数 ps.setString(1, "提莫"); ps.setFloat(2, 313.0f); ps.setInt(3, 50); // 执行 ps.execute(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
Statement 需要进行字符串拼接,可读性和维护性比较差
String sql = "insert into hero values(null,"+"'提莫'"+","+313.0f+","+50+")";
PreparedStatement 使用参数设置,可读性好,不易犯错
String sql = "insert into hero values(null,?,?,?)";
public class TestJDBC { public static void main(String[] args) { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } String sql = "insert into hero values(null,?,?,?)"; try (Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8","root", "admin"); Statement s = c.createStatement(); PreparedStatement ps = c.prepareStatement(sql); ) { // Statement需要进行字符串拼接,可读性和维修性比较差 String sql0 = "insert into hero values(null," + "'提莫'" + "," + 313.0f + "," + 50 + ")"; s.execute(sql0); // PreparedStatement 使用参数设置,可读性好,不易犯错 // "insert into hero values(null,?,?,?)"; ps.setString(1, "提莫"); ps.setFloat(2, 313.0f); ps.setInt(3, 50); ps.execute(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
原文地址:http://how2j.cn/k/jdbc/jdbc-preparedstatement/388.html#nowhere
execute与executeUpdate的相同点:都可以执行增加,删除,修改
public class TestJDBC { public static void main(String[] args) { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } try (Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8","root", "admin"); Statement s = c.createStatement();) { String sqlInsert = "insert into Hero values (null,'盖伦',616,100)"; String sqlDelete = "delete from Hero where id = 100"; String sqlUpdate = "update Hero set hp = 300 where id = 100"; // 相同点:都可以执行增加,删除,修改 s.execute(sqlInsert); s.execute(sqlDelete); s.execute(sqlUpdate); s.executeUpdate(sqlInsert); s.executeUpdate(sqlDelete); s.executeUpdate(sqlUpdate); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
不同点:
不同1:
execute可以执行查询语句
然后通过getResultSet,把结果集取出来
executeUpdate不能执行查询语句
不同2:
execute返回boolean类型,true表示执行的是查询语句,false表示执行的是insert,delete,update等等
executeUpdate返回的是int,表示有多少条数据受到了影响
public class TestJDBC { public static void main(String[] args) { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } try (Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8","root", "admin"); Statement s = c.createStatement();) { // 不同1:execute可以执行查询语句 // 然后通过getResultSet,把结果集取出来 String sqlSelect = "select * from hero"; s.execute(sqlSelect); ResultSet rs = s.getResultSet(); while (rs.next()) { System.out.println(rs.getInt("id")); } // executeUpdate不能执行查询语句 // s.executeUpdate(sqlSelect); // 不同2: // execute返回boolean类型,true表示执行的是查询语句,false表示执行的是insert,delete,update等等 boolean isSelect = s.execute(sqlSelect); System.out.println(isSelect); // executeUpdate返回的是int,表示有多少条数据受到了影响 String sqlUpdate = "update Hero set hp = 300 where id < 100"; int number = s.executeUpdate(sqlUpdate); System.out.println(number); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
原文地址:http://how2j.cn/k/jdbc/jdbc-execute/389.html#nowhere