JAVA复习之JDBC

一、使用JDBC操作数据库的基本步骤

在这里插入图片描述

二、数据库

(一)数据库基础

在这里插入图片描述
1、select语句(用于查询数据表的数据)
在这里插入图片描述
2、insert语句(用于向数据表中插入新的数据)
在这里插入图片描述
3、update语句(用于修改数据表中的数据)
在这里插入图片描述
4、delete语句(用于删除数据表中的数据)
在这里插入图片描述

(二)连接数据库

在这里插入图片描述
连接数据库常见的错误:
在这里插入图片描述
在这里插入图片描述
3.用户名密码错误
4.驱动包的版本不兼容

案例康康:

public class Demo7 {
    public static void main(String[] args) {
        Connection con = null;
        Statement sm = null;
        ResultSet rs = null;
        try {
            //加载驱动包
            Class.forName("com.mysql.jdbc.Driver");
            //建立连接
            con = DriverManager.getConnection("jdbc:mysql:///user","root","root");
            //编写sql
            String sql = "select * from user";
            sm = con.createStatement();
            rs = sm.executeQuery(sql);
            while (rs.next()){
                int id = rs.getInt("id");
                String username = rs.getString("username");
                String password = rs.getString("password");
                System.out.println("编号:"+id+"\t"+"姓名:"+username+"\t"+"密码:"+password);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(rs != null) {
                if (sm != null) {
                    if (con != null) {
                        try {
                            rs.close();
                            sm.close();
                            con.close();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}

(三)数据库查询

1、数据库查询在这里插入图片描述
2、创建接口对象
在这里插入图片描述
3、ResultSet常用方法
在这里插入图片描述

(四)动态查询

1、你的SQL安全吗?
在这里插入图片描述
为了防止这种SQL注入,JDBC提供了动态查询
2、动态查询
在这里插入图片描述

在这里插入图片描述
案例分析:

public class Demo7 {
    public static void main(String[] args) {
        Connection con = null;
        PreparedStatement sm = null;
        ResultSet rs = null;
        try {
            //加载驱动包
            Class.forName("com.mysql.jdbc.Driver");
            //建立连接
            con = DriverManager.getConnection("jdbc:mysql:///user","root","root");
            //编写sql
            String sql = "select * from user where username like ?";
            sm = con.prepareStatement(sql);
            sm.setString(1,"小%");
            rs = sm.executeQuery();
            while (rs.next()){
                System.out.println("姓名:"+rs.getString(2)+"\t密码:"+rs.getString(3));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(rs != null) {
                if (sm != null) {
                    if (con != null) {
                        try {
                            rs.close();
                            sm.close();
                            con.close();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}

3、添加、修改、删除记录在这里插入图片描述
案例分析:

public class Demo7 {
    public static void main(String[] args) {
        Connection con = null;
        Statement sm = null;
        ResultSet rs = null;
        try {
            //加载驱动包
            Class.forName("com.mysql.jdbc.Driver");
            //建立连接
            con = DriverManager.getConnection("jdbc:mysql:///user","root","root");
            
            /*插入数据*/
            String sql = "insert into user(username,password) values('赵亮亮','123')";
            sm = con.createStatement();
            int i = sm.executeUpdate(sql);
            System.out.println(i);
            

            /*修改数据*/
            String sql1 = "update user set username = '赵丹' where id = 10";
            sm = con.createStatement();
            int j = sm.executeUpdate(sql1);
            System.out.println(j);
            

            /*删除数据*/
            String sql2 = "delete from user where id = 10 ";
            sm = con.createStatement();
            int k = sm.executeUpdate(sql2);
            System.out.println(k);

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(rs != null) {
                if (sm != null) {
                    if (con != null) {
                        try {
                            rs.close();
                            sm.close();
                            con.close();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                        
                    }
                }
            }
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

java亮小白1997

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

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

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

打赏作者

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

抵扣说明:

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

余额充值