JDBC 用PreparedStatement实现MySQL增删改查

连接数据库(整体代码在文末)

 //要连接的数据库url,注意:此处连接的应该是服务器上的MySQl的地址
    String url = "jdbc:mysql://192.168.3.5:3306/liaotian?useUnicode=true&characterEncoding=utf8";
    //连接数据库使用的用户名
    String userName = "root";
    //连接的数据库时使用的密码
    String password = "123456";
 
    Connection connection = null;
 
     try {
         //1、加载驱动
         Class.forName("com.mysql.jdbc.Driver").newInstance();
         //2、获取与数据库的连接
         connection = DriverManager.getConnection(url, userName, password);
     } catch (SQLException throwables) {
            throwables.printStackTrace();
     } catch (ClassNotFoundException e) {
            e.printStackTrace();
     } catch (InstantiationException e) {
            e.printStackTrace();
     } catch (IllegalAccessException e) {
            e.printStackTrace();
     }

  //3.sql添加数据语句
  String sql = "INSERT INTO user (zhanghao, possword) VALUES ( ?, ?)";
  //4.获取用于向数据库发送sql语句的ps
  PreparedStatement ps = connection.prepareStatement(sql);
  //获取输入框的数据 添加到mysql数据库
   ps.setString(1, "112233");
   ps.setString(2, "223344");
   int n = ps.executeUpdate();//更新数据库
   System.out.println("添加:"+n);

     //预编译
     String sql = "delete from user where zhanghao = ? ";
     PreparedStatement ps = connection.prepareStatement(sql);
     ps.setString(1,"369");
     int n = ps.executeUpdate();
     System.out.println("删除:"+n);

 String sql="update user set possword = ?  where zhanghao= ?";
 PreparedStatement ps = connection.prepareStatement(sql);
 ps.setString(1,"258888888");
 ps.setString(2,"258");
 int n = ps.executeUpdate();
 System.out.println("修改:"+n);

 String sql = "select * from user ";
  PreparedStatement ps = connection.prepareStatement(sql);
  ResultSet resultSet = ps.executeQuery();
  while (resultSet.next()){
        String zhanghao = resultSet.getString(1);
        String password = resultSet.getString(2);
        System.out.println("查询:"+ zhanghao +"\n"+"密码:"+password);
  }

整体代码

 //要连接的数据库url,注意:此处连接的应该是服务器上的MySQl的地址
    public static String url = "jdbc:mysql://192.168.3.5:3306/liaotian?useUnicode=true&characterEncoding=utf8";
    //连接数据库使用的用户名
    public static String userName = "root";
    //连接的数据库时使用的密码
    public static String password = "123456";
    public static void main(String[] args) {
        Connection connection = null;
        //输入数字 
        Scanner scanner = new Scanner(System.in);
        int i = scanner.nextInt();
        System.out.println("输入:"+i);
        try {
            
            //1、加载驱动
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            //2、获取与数据库的连接
            connection = DriverManager.getConnection(url, userName, password);

            /**
             * 增
             */
            if(i == 1){   
                //3.sql添加数据语句
                String sql = "INSERT INTO user (zhanghao, possword) VALUES ( ?, ?)";
                //4.获取用于向数据库发送sql语句的ps
                PreparedStatement ps = connection.prepareStatement(sql);
                //获取输入框的数据 添加到mysql数据库
                ps.setString(1, "112233");
                ps.setString(2, "223344");
                int n = ps.executeUpdate();//更新数据库
                System.out.println("添加:"+n);
            }
            /**
             * 删
             */
            else if(i == 2){
                //预编译
               String sql = "delete from user where zhanghao = ? ";
               PreparedStatement ps = connection.prepareStatement(sql);
               ps.setString(1,"369");
                int n = ps.executeUpdate();
                System.out.println("删除:"+n);
            }
            /**
             * 改
             */
            else if(i == 3){
                String sql="update user set possword = ?  where zhanghao= ?";
                PreparedStatement ps = connection.prepareStatement(sql);
                ps.setString(1,"258888888");
                ps.setString(2,"258");
                int n = ps.executeUpdate();
                System.out.println("修改:"+n);
            }
            /**
             * 查
             */
            else {
                String sql = "select * from user ";
                PreparedStatement ps = connection.prepareStatement(sql);
                ResultSet resultSet = ps.executeQuery();
                while (resultSet.next()){
                      String zhanghao = resultSet.getString(1);
                      String password = resultSet.getString(2);
                      System.out.println("查询:"+ zhanghao +"\n"+"密码:"+password);
                }
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

记得加载驱动哦

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Android 《开发》

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值