码迷,mamicode.com
首页 > 其他好文 > 详细

Statement与 PreparedStatement(增删改查)区别及代码

时间:2014-12-03 11:48:33      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:style   io   ar   color   os   使用   sp   java   for   

1.Statement与 PreparedStatement的区别

    (1)数据库在执行 sql 语句的时候如果使用 PreparedStatement 语句会有一点优势:因为数据
库会对 preparedStatement 语句进行预编译,下次执行相同的 sql 语句时,数据库端不
会再进行预编译了,而直接用数据库的缓冲区,提高数据访问的效率

 

    (2)使用 Statement 对象。在对数据库只执行一次性存取的时侯,用 Statement 对象进行处
理。PreparedStatement 对象的开销比 Statement 大,对于一次性操作并不会带来额外
的好处。

 

    (3)statement 每次执行 sql 语句,相关数据库都要执行 sql 语句的编译,preparedstatement
是预编译得, preparedstatement 支持批处理

 

    (4)无论是 Statement 还是 PreparedStatement,增删改执行的 executeUpdate(),属于更
新操作,而查询执行的是 executeQuery(),属于查询操作,不可混用。

 

 

2. Statement执行增删改查的代码

    import java.sql.*;
    public class CreateTitle {
             public static void main(String[] args) {
             Connection conn = null;
             Statement stmt = null;
             ResultSet rs = null;
             try {
                     Class.forName("com.mysql.jdbc.Driver");//注册驱动,必须导包到项目
                   } catch (ClassNotFoundException cnfe) {
                     System.out.println("没有找到驱动类");
                   }             
             try {

                    String sql = "insert intofirstleveltitle(titlename,creator,createtime) values(‘访问数据库‘,‘gxs‘,now())";//获取连接
                    conn = DriverManager.getConnection  ("jdbc:mysql://localhost:3306/newsuseUnicode=true&characterEncoding=UTF-8","root", "123456");
                    stmt = conn.createStatement();//创建Statement对象
                    int line = stmt.executeUpdate(sql);//返回更新记录的行数
                    System.out.println(line);
                    rs = stmt.getGeneratedKeys();//获取结果集中存放更新记录主键
                    rs.next();
                    System.out.println(rs.getInt(1));
                  
} catch (SQLException sqlE) {
                    sqlE.printStackTrace();
                  } finally {//释放资源
            try {
                    if (rs != null)
                    rs.close();
                    if (stmt != null)
                    stmt.close();
                    if (conn != null)
                    conn.close();
                  } catch (SQLException e) {
                     e.printStackTrace();
                                                             }}}}

 

3.  PreparedStatement执行增删改查的代码

     import java.sql.*;
     import java.text.SimpleDateFormat;
     import java.util.Date;
     public class CreateFisrstPrepared {
              public static void main(String[] args) {
              Connection conn = null;
              PreparedStatement pstmt = null;
              ResultSet rs = null;
       try {                                  // 1.注册驱动,前提必须加载驱动包
               Class.forName("com.mysql.jdbc.Driver");
             } catch (ClassNotFoundException e) {
               System.out.println("未发现驱动");
             }
       try {
              conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/news", "root","123456");
              String sql = "insert intofirstleveltitle(titlename,creator,createtime) values(?,?,?)";
              pstmt = conn.prepareStatement(sql);//预编译SQL语句
              //设定SQL语句中的参数
              pstmt.setString(1, "PreparedStatement练习");
              pstmt.setString(2, "gxs");
              SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
              pstmt.setString(3, formate.format(new Date()));

              int lineNum = pstmt.executeUpdate();
              System.out.println(lineNum);
              rs = pstmt.getGeneratedKeys();
              rs.next();
              System.out.println(rs.getInt(1));
            } catch (SQLException e) {
              e.printStackTrace();
            } finally {
      try {
              if (rs != null)
              rs.close();
              if (pstmt != null)
              pstmt.close();
              if (conn != null)
              conn.close();
            } catch (SQLException e) {
              e.printStackTrace();

                                                       } } } }

(注:代码中用到是mysql数据库,见一个intofirstleveltitle的表,表中有titlename,creator,createtime三个属性

处理建立数据库,还要在程序中添加mysql的jar包实现连接数据库的功能)

Statement与 PreparedStatement(增删改查)区别及代码

标签:style   io   ar   color   os   使用   sp   java   for   

原文地址:http://www.cnblogs.com/gxseveryday/p/4139321.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!