JDBC在java代码中的使用

声明

对于数据库的增删改查,在使用jdbc的过程中分二类,查(DQL)语句和增,删,改(DML语句)

他们的整体都分为以下五部分,只是DQL语句多了数据的处理部分。

在使用之前需要导入相关的jar包

 

1,加载驱动器(非必要,但是建议手动加载养成好习惯)

2,创建连接

3,创建会话

4,执行会话

5,关闭会话

1,数据库操作插入语句(增)

操作前数据

jdbc:mysql://localhost:3306/huohua257?serverTimezone=Asia/Shanghai代表的含义是jdbc:操作的是那个数据库(mysql,orcale 等数据库)//你要连接的数据的地址,这里是本地所以就是localhost:端口号/数据库的名称/占位符 时区(时区不能少)

 public static void main(String[] args) {
        try {
//            增
//            1,加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
//            2,创建连接 localhost 与127.0.0.1 都是本地连接地址
            String url= "jdbc:mysql://localhost:3306/huohua257?serverTimezone=Asia/Shanghai";
            String user="root";
            String password="123456";
            Connection connection= DriverManager.getConnection(url,user,password);
//            3,创建会话
            String sql="insert into  t_people values(null,'李四2',null)";
            PreparedStatement ps=connection.prepareStatement(sql);

//            4,执行回话
            ps.executeUpdate();

//            关闭回话
            ps.close();
            connection.close();


        }catch (Exception e){
            e.printStackTrace();
        }

    }

2,数据库操作删除语句(删除)

刚刚展示的插入语句只是最基础的,没有对使用占位符来让我们用户输入来决定插入什么信息,这次删除使用占位符来指定删除信息

在sql语句中使用?占位符,

  try {
//        第一步加载驱动类
        Class.forName("com.mysql.cj.jdbc.Driver");
//        第二步创建连接 导入的java.sql的包
        String url= "jdbc:mysql://localhost:3306/huohua257?serverTimezone=Asia/Shanghai";
        String user="root";
        String password="123456";
        Connection  connection= DriverManager.getConnection(url,user,password);
//        第三部 书写sql 并创建会话
        String sql="delete from t_people where pid=? ";
        PreparedStatement ps=connection.prepareStatement(sql);
//        1代表第一个参数 x 代表参数的内容
        ps.setInt(1,14);
//        第四部 执行会话
        ps.executeUpdate();
//        第五步关闭连接
        ps.close();
        connection.close();

    } catch (Exception e) {
       e.printStackTrace();
    }
}

操作前数据操作成功后数据

 3,数据操作修改语句(改)

  try {
        //    第一步加载驱动器
        Class.forName("com.mysql.cj.jdbc.Driver");
//        2,创建连接
        String url= "jdbc:mysql://localhost:3306/huohua257?serverTimezone=Asia/Shanghai";
        String user="root";
        String password="123456";
        Connection connection= DriverManager.getConnection(url,user,password);
//        3,书写sql 创建会话
        String sql="update t_people set name='zhangsan' where pid=?";
        PreparedStatement ps= connection.prepareStatement(sql);
        ps.setInt(1,1);

//        4,执行绘画
        ps.executeUpdate();
//        5,关闭会话与连接
        ps.close();
        connection.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

修改前数据

修改后数据 

4,数据操作查找内容(查)

查找和其余部分有些不同

1,普通查找不使用占位符

   public static void main(String[] args) {
        try {
//            加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
//            创建连接
            String url= "jdbc:mysql://localhost:3306/huohua257?serverTimezone=Asia/Shanghai";
            String user="root";
            String password="123456";

            Connection connection= DriverManager.getConnection(url,user,password);
//       书写sql并创建会话
            String sql="select *from t_people";
            PreparedStatement ps=connection.prepareStatement(sql);
//            接收并处理结果
          ResultSet rs= ps.executeQuery();
//          拿到结果后用迭代器遍历
            while(rs.next()){
                int id=rs.getInt("pid");
                String pname=rs.getString("name");
                String bir=rs.getString("birthday");
                System.out.println(id+"\t"+pname+"\t"+bir+"\t");
            }
            rs.close();
            ps.close();
            connection.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

接收处理结果的原理就是把他们放到一个类似set的集合中依靠迭代器遍历得到全部内容

2,使用占位符的情况

public static void main(String[] args) {
    Scanner scanner=new Scanner(System.in);
    System.out.println("输入要查询的人物名称");
    String name=scanner.nextLine();
//    1,加载驱动
    try {
        Class.forName("com.mysql.cj.jdbc.Driver");
        String url= "jdbc:mysql://localhost:3306/huohua257?serverTimezone=Asia/Shanghai";
        String user="root";
        String password="123456";
        Connection connection= DriverManager.getConnection(url,user,password);

        String sql="select *from  t_people where name=?";
        PreparedStatement ps=connection.prepareStatement(sql);
        ps.setString(1,name);
       ResultSet rs= ps.executeQuery();
       while(rs.next()){
       int id=rs.getInt("pid");
       String pname= rs.getString("name");
       String bir=rs.getString("birthday");
        System.out.println(id+" "+pname +" "+bir);}
rs.close();
ps.close();
connection.close();

    } catch (Exception e) {
        e.printStackTrace();
    }

}

3,不使用占位符使用字符串拼接 

不要使用会造成sql注入问题这里 只是演示要知道有这种情况

public static void main(String[] args) {
    Scanner scanner=new Scanner(System.in);
    System.out.println("输入要查询的人物名称");
    String name=scanner.nextLine();
//    1,加载驱动
    try {
        Class.forName("com.mysql.cj.jdbc.Driver");
        String url= "jdbc:mysql://localhost:3306/huohua257?serverTimezone=Asia/Shanghai";
        String user="root";
        String password="123456";
        Connection connection= DriverManager.getConnection(url,user,password);
        String sql="select *from  t_people where name='"+name+"'";
//        String sql="select *from  t_people where name=?";
        PreparedStatement ps=connection.prepareStatement(sql);
//        ps.setString(1,name);
       ResultSet rs= ps.executeQuery();
       while(rs.next()){
       int id=rs.getInt("pid");
       String pname= rs.getString("name");
       String bir=rs.getString("birthday");
        System.out.println(id+" "+pname +" "+bir);}
rs.close();
ps.close();
connection.close();

    } catch (Exception e) {
        e.printStackTrace();
    }

}

 如果我们正常输入就可以正常查询,但是存在特殊情况

 特殊情况

没有输入表的内容依旧查出来了全部的内容,早期我们的某些软就就存在这个弊端,qq早期就是这种情况

  • 10
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值