• 预处理对象,增删改查


    package com.orcal.demo001;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;
    import java.util.Scanner;
    
    public class Demo01 {
        //删除数据
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入删除的姓名:");
        String name=sc.next();
        Class.forName("com.mysql.jdbc.Driver");
        String url="jdbc:mysql://localhost:3306/shop?characterEncoding=utf8";
        String uname="root";
        String ped="123456";
        Connection co=DriverManager.getConnection(url,uname,ped);
        String sql="delete from user where uname=?";
        PreparedStatement jie=co.prepareStatement(sql);
        jie.setString(1, name);
        int i=jie.executeUpdate();
        co.close();
        jie.close();
    }
    }
    package com.orcal.demo001;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;
    import java.util.Scanner;
    //修改数据
    public class Demo02 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        
        Class.forName("com.mysql.jdbc.Driver");
        String url="jdbc:mysql://localhost:3306/shop?characterEncoding=utf8";
        String uname="root";
        String ped="123456"; 
        Connection co=DriverManager.getConnection(url,uname,ped);
        String sql="update user set uname=? where pwd=?";
        PreparedStatement feng=co.prepareStatement(sql);
        feng.setString(1, "xiaoxiao");//给第一个问好赋值
        feng.setInt(2, 111);
        int aa=feng.executeUpdate();
        System.out.println(aa);
        feng.close();
        co.close();
        
    }
    }
    package com.orcal.demo001;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Scanner;
    
    public class Demo04 {
        //添加数据
    public static void main(String[] args) throws ClassNotFoundException , SQLException{
        
            System.out.println("请输入用户名");
            Scanner sc=new Scanner(System.in);
            String name=sc.next();
            System.out.println("密码");
            String password=sc.next();
            
        
        Class.forName("com.mysql.jdbc.Driver");
        String url="jdbc:mysql://localhost:3306/shop?characterEncoding=utf8";
        String uname="root";
        String ped="123456";
        Connection conn=DriverManager.getConnection(url, uname,ped);//连接对象
        String sql="insert into user(uname,pwd) values(?,?) ";
        PreparedStatement pst=conn.prepareStatement(sql);
        pst.setString(1, name);
        pst.setString(2, password);
        
        int rs=pst.executeUpdate();
        
        
        pst.close();
        conn.close();//先开的后关
    }
    }
    package com.orcal.demo001;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.Scanner;
    
    public class DemoLianxi {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        
        Class.forName("com.mysql.jdbc.Driver");
        String url="jdbc:mysql://localhost:3306/shop?characterEncoding=utf8";
        String uname="root";
        String ped="123456"; 
        //
        /*Connection co=DriverManager.getConnection(url,uname,ped);
        String sql="insert into user(uname,pwd) values(?,?)";
        PreparedStatement tian=co.prepareStatement(sql);
        tian.setString(1, "小红");
        tian.setInt(2, 123);
        int s=tian.executeUpdate();
        System.out.println(s);
        tian.close();
        co.close();*/
        //
        /*Connection co=DriverManager.getConnection(url, uname, ped);
        String sql="delete from user where uname=?";
        PreparedStatement stat=co.prepareStatement(sql);
        stat.setString(1, "小红");
        int a=stat.executeUpdate();
        System.out.println(a);
        stat.close();
        co.close();*/
        //
        /*Connection co=DriverManager.getConnection(url,uname,ped);
        String sql="update user set uname=? where pwd=?";
        PreparedStatement sta=co.prepareStatement(sql);
        
        sta.setString(1, "dada");
        sta.setInt(2, 999);
        int a=sta.executeUpdate();
        System.out.println(a);
        sta.close();
        co.close();*/
        //
        Connection co=DriverManager.getConnection(url,uname,ped);
        String sql="Select * from user where uname=?";
        PreparedStatement sta=co.prepareStatement(sql);
        sta.setString(1, "xiaoxiao");
        ResultSet aa=sta.executeQuery();
        while(aa.next()){
            String a=aa.getString("uid");
            String b=aa.getString("uname");
            String c=aa.getString("pwd");
            System.out.println(a+b+c);
        }
        aa.close();
        sta.close();
        co.close();
        /*Connection co=DriverManager.getConnection(url,uname,ped);
        String sql="select * from user where pwd=?";
        PreparedStatement sta=co.prepareStatement(sql);
        sta.setInt(1, 111);
        ResultSet rs=sta.executeQuery();
        
        while(rs.next()){
            String uid=rs.getString("uid");
            String uname1=rs.getString("uname");
            String pwd=rs.getString("pwd");
                    System.out.println(uid+uname+pwd);
        }
        rs.close();
        sta.close();
        co.close();
        */
        
        
    }
    }
  • 相关阅读:
    真题-中国科学技术大学2017年夏令营数学分析试题
    真题-中国科学技术大学2019年夏令营实变函数复变函数试题
    真题-中国科学技术大学2019年夏令营数学分析高等代数试题
    真题-中国科学技术大学2021年夏令营微分几何近世代数试题
    真题-中国科学技术大学2021年夏令营实变函数复变函数试题
    真题-中国科学技术大学2021年夏令营数学分析高等代数试题
    竞赛厦门大学第15届景润杯数学竞赛数学类试题参考解答
    厦门大学第16届景润杯数学竞赛数学类试题参考解答
    厦门大学第17届景润杯数学竞赛数学类试题参考解答
    厦门大学第18届景润杯数学竞赛数学类试题参考解答
  • 原文地址:https://www.cnblogs.com/111wdh/p/13343735.html
Copyright © 2020-2023  润新知