JDBC prepareStatement方法书写(增删改查)



系列文章目录

JDBC增删改查。


目录

前言

一、使用PrepareStatement对象有三大优点:

二、使用步骤

1.查询

2.插入

3.修改

4.删除

总结





前言

PrepareStatement更具有效率,同时可以防止sql注入。



一、使用PrepareStatement对象有三大优点:

    1、防止sql注入

    2、提高代码可读性、可维护行

    3、提高sql执行效率



二、使用步骤



1.查询

代码如下:

​
public static void Select() throws ClassNotFoundException, SQLException{

             //1.把mysql所对应的驱动包加载到当前项目的classpath路径下,右键lib add as library
             //2.加载mysql驱动
             Class.forName("com.mysql.jdbc.Driver");
             //3.获取连接对象
             Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/Test?characterEncoding=utf-8","root","***");
             //4.获取执行sql语句的对象

             String sql="select * from user ";
             PreparedStatement preparedStatement=connection.prepareStatement(sql);

             //6.执行sql语句
            ResultSet rs= preparedStatement.executeQuery();
             while(rs.next()){
                 int id= rs.getInt("id");
                 String name=rs.getString("name");
                 int age=rs.getInt("age");
                 java.sql.Date birthday =rs.getDate("birthday");
                 System.out.println(id+"--"+name+"---"+age+"---"+birthday);

             }
             //7.释放资源
             preparedStatement.close();
             connection.close();
}

​



2.插入

代码如下:

public static void Insert()throws ClassNotFoundException, SQLException{

    //1.把mysql所对应的驱动包加载到当前项目的classpath路径下,右键lib add as library
    //2.加载mysql驱动
    Class.forName("com.mysql.jdbc.Driver");
    //3.获取连接对象
    Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/Test?characterEncoding=utf-8","root","***");
    //4.获取执行sql语句的对象

    String sql="insert into user(name,age,birthday,id) values (?,?,?,?)";
    PreparedStatement preparedStatement=connection.prepareStatement(sql);
        //5.给占位符赋值
    preparedStatement.setString(1,"aaa");
    preparedStatement.setInt(2,1);
    preparedStatement.setDate(3,new java.sql.Date(new Date().getTime()));
    preparedStatement.setInt(4,1);
    //6.执行sql语句
    int count=preparedStatement.executeUpdate();
    System.out.println(count);
    //7.释放资源
    preparedStatement.close();
    connection.close();
}

3.修改

代码如下:

public static void Update()throws ClassNotFoundException, SQLException{
    //1.把mysql所对应的驱动包加载到当前项目的classpath路径下,右键lib add as library
    //2.加载mysql驱动
    Class.forName("com.mysql.jdbc.Driver");
    //3.获取连接对象
    Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/Test?characterEncoding=utf-8","root","***");
    //4.获取执行sql语句的对象

    String sql="update user set  name=?,age=?,birthday=? where id=?";
    PreparedStatement preparedStatement=connection.prepareStatement(sql);
    //5.给占位符赋值
    preparedStatement.setString(1,"sss");
    preparedStatement.setInt(2,12);
    preparedStatement.setDate(3,new java.sql.Date(new Date().getTime()));
    preparedStatement.setInt(4,1);
    //6.执行sql语句
    int count=preparedStatement.executeUpdate();
    System.out.println(count);
    //7.释放资源
    preparedStatement.close();
    connection.close();

}

4.删除

代码如下:

public static void Delete()throws ClassNotFoundException, SQLException{
    //1.把mysql所对应的驱动包加载到当前项目的classpath路径下,右键lib add as library
    //2.加载mysql驱动
    Class.forName("com.mysql.jdbc.Driver");
    //3.获取连接对象
    Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/Test?characterEncoding=utf-8","root","***");
    //4.获取执行sql语句的对象

    String sql="delete from user  where id=?";
    PreparedStatement preparedStatement=connection.prepareStatement(sql);
    //5.给占位符赋值

    preparedStatement.setInt(1,1);
    //6.执行sql语句
    int count=preparedStatement.executeUpdate();
    System.out.println(count);
    //7.释放资源
    preparedStatement.close();
    connection.close();



}


 

                                                

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值