80.Statement和PreparedStatement的区别.pptxVIP

  • 2
  • 0
  • 约5.11千字
  • 约 20页
  • 2017-03-01 发布于广东
  • 举报
Statement和PreparedStatement的区别学习目标01知识目标掌握PreparedStatement的使用掌握语句对象与预处理语句对象的区别02能力目标能够使用PreparedStatement对象实现数据维护操作使用Statement执行SQL语句使用Statement维护SQL Server数据库表中的数据,对数据表进行增删改查操作使用Statement对象根据运行时输入的数据对数据表进行增删改查操作使用Statement对象添加数据3-1使用Statement对象把运行时输入的员工信息添加到数据表emptry { Class.forName(com.microsoft.sqlserver.jdbc.SQLServerDriver);} catch (ClassNotFoundException e) { System.out.println(无法找到驱动类);}Connection con = null; Statement stmt = null; ResultSet rs = null; 使用Statement对象添加数据3-2try { con = DriverManager.getConnection(“…”, “…, “…); stmt = con.createStatement(); System.out.println(“请输入姓名:“); String eName = input.next(); System.out.println(“请输入性别:“); String eSex = input.next(); String sql = “insert into emp(eName, eSex) value(‘” + eName + ”’, ‘” + eSex + ”’)”; int rows = stmt.executeUpdate(sql);}使用Statement对象添加数据3-3catch (SQLException e) { e.printStackTrace(); }finally { try { if(stmt != null) stmt.close(); if(con != null !con.isClosed()) con.close(); } catch (SQLException e) { e.printStackTrace(); }}练习使用Statement对象更新数据表emp中的员工信息,对应的更新信息需要运行时输入使用Statement对象删除数据表emp中符合条件的员工信息,对应的条件取值需要运行时输入使用Statement对象在emp表中查询符合条件的员工信息,对应的条件取值需要运行时输入PreparedStatementPreparedStatement接口继承Statement接口实现将SQL语句预先处理,减轻数据库的负担,提高访问数据库的速度简化SQL语句的书写 Statement 接口 PreparedStatement接口 (预编译的 SQL 语句)提高了安全性提高了代码的可读性和可维护性 提高了SQL语句执行的性能 JDBC工作步骤——第三步第三步:创建PreparedStatement对象try { …… String sql = “SQL语句”; PreparedStatement pstmt = con.prepareStatement(sql);} catch (SQLException e) { e.printStackTrace();}创建语句对象前,要先确定预处理的SQL语句以下为创建添加员工语句对象示例:try { Connection con = DriverManager.getConnection(……); String sql = “insert into emp(eName, eSex) values(?, ?)”; PreparedStatement pstmt = con.prepareStatement(sql);} catch (SQLException e) { e.printStackTrace(); }JDBC工作步骤——第四步第四步:执行SQL的增删改语句try { …… pstmt.setXXX(第n个问号, 对应问号位置上的赋值); int row = pstmt.executeUpdate();} catch (SQLException e) {e.printStackTrace(); }以下为执行添加员工信息示例:try { …… System.out.println(“请输入姓名:“); String eName = input.next(); Sy

文档评论(0)

1亿VIP精品文档

相关文档