JDBC回顾二PreparedStatement与Statement

程序对于数据库的操作基本都是增删改,但是对于删我们一般不会用,主要原因是因为在大型开发中,我们删除语句只是更改某一字段的值使其隐藏,这样的话,我们可以保留用户的所有信息,然后可以进行分析,或者避免一些不必要的麻烦。

DML:Data Manipulation Language,数据操纵语言。一般包括INSERT 、UPDATE 、 DELETE。

数据库操作Statement和PreparedStatement接口之间的区别
Statement:用于执行静态 SQL 语句并返回它所生成结果的对象。 他所执行的sql语句需要进行拼接,一方面写着比较麻烦,另一方面也不安全,容易造成sql注入。
PreparedStatement:表示预编译的 SQL 语句的对象。 PreparedStatement接口是Statement接口的子接口,所以他比Statement更具有特点。他所执行的sql语句可以利用占位符进行表示,大大简化了sql语句的编写,而且能够有效的防治sql语句注入。

综上,我们一般使用PreparedStatement而不是用Statement。

executeUpdate():执行给定 SQL 语句,该语句必须是一个 SQL 数据操作语言(Data Manipulation Language,DML)语句;或者是无返回内容的 SQL 语句,比如 DDL 语句。

利用Statement进行插入

public class Jdbcsta {

    public static void main(String[] args){
        Connection conn =null;
        Statement stmt = null;

        try{
            Class.forName("com.mysql.jdbc.Driver"); //建立驱动
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/studemo","root","root");//建立连接

            stmt = conn.createStatement();//创建语句

            String sql="insert into stu() values ("+args[0]+",'"+args[1]+"','"+args[2]+"')"; 
            //  Statement拼接语句过于麻烦
            System.out.println(sql);
            stmt.executeUpdate(sql);//执行语句
            }catch(ClassNotFoundException e){
                e.printStackTrace();
            }catch(SQLException e){
                e.printStackTrace();
            }finally{
                try{
                if(stmt!=null){stmt.close();stmt=null;}
                if(conn!=null){conn.close();conn=null;}
                }catch(SQLException e){
                e.printStackTrace();                
                }
            }

        }  
    }

利用PreparedStatement进行插入

public class JdbcPresta {

    public static void main(String[] args){
        Connection conn =null;
        PreparedStatement ptmt= null;

        try{
            Class.forName("com.mysql.jdbc.Driver"); //建立驱动
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/studemo","root","root");//建立连接


           ptmt = conn.prepareStatement("insert into stu values (?,?,?)"); 
           //  preparedStatement 直接进行预编译,利用占位符可以有效防治sql注入
           int id=0;
            try {
                id = Integer.parseInt(args[0]);
            } catch (NumberFormatException e) {
                // TODO Auto-generated catch block
                System.out.println("参数类型错误");
                System.exit(-1);
            }

          ptmt.setInt(1, id);  //  占位符参数设置 ,一参表示当前第几个占位符,二参表示传递的数值
          ptmt.setString(2, args[1]);
          ptmt.setString(3, args[2]);
          ptmt.executeUpdate();


            }catch(ClassNotFoundException e){
                e.printStackTrace();
            }catch(SQLException e){
                e.printStackTrace();
            }finally{
                try{
                if(ptmt!=null){ptmt.close();ptmt=null;}
                if(conn!=null){conn.close();conn=null;}
                }catch(SQLException e){
                e.printStackTrace();                
                }
            }

        }  
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值