JDBC学习----09--PreparedStatement完成增删改

import com.sun.tools.javac.Main;

import java.sql.*;

/**
 * @author zzw
 * @create 2020/11/16-10:55
 */
public class jdbcTest09 {
    public static void main(String[] args) {
        //jdbc6步走
        Connection conn = null;
        PreparedStatement ps = null;
        int count ;
        try {
            //1、注册驱动
            Driver driver = new com.mysql.jdbc.Driver();
            DriverManager.registerDriver(driver);
            //2、获取连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc", "root", "a");
            /*
            给数据库增加一个数据
            //3、获取数据库操作对象
            String sql = "insert  into jdbc01(语文,数学,英语) values (?,?,?)";
            ps = conn.prepareStatement(sql);
            //编译之后给占位符赋值
            ps.setInt(1,60);
            ps.setInt(2,60);
            ps.setInt(3,60);
            //4、执行sql,根据数据库的操作不同,使用不同的PreparedStatement方法,这里我们使用executeUpdate()
            count = ps.executeUpdate();
            System.out.println(count);
            */


            /*
            数据更新,修改
            //3、获取数据库操作对象
            //String sql = "update jdbc01 set 语文=?,数学=?,英语=?";这个就全部改了
            String sql = "update jdbc01 set 语文=?,数学=? where 英语=?";
            ps = conn.prepareStatement(sql);
            //编译之后给占位符赋值
            ps.setInt(1,70);
            ps.setInt(2,70);
            ps.setInt(3,80);
            //4、执行sql,根据数据库的操作不同,使用不同的PreparedStatement方法,这里我们使用executeUpdate()
            count = ps.executeUpdate();
            System.out.println(count);
            */

            //3、获取数据库操作对象
            String sql="delete from jdbc01 where 语文=?";
            ps = conn.prepareStatement(sql);
            //编译之后给占位符赋值
            ps.setInt(1,70);
            //4、执行sql,根据数据库的操作不同,使用不同的PreparedStatement方法,这里我们使用executeUpdate()
            count = ps.executeUpdate();
            System.out.println(count);




            //5、处理查询结果集
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //6、释放资源
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值