JAVA review of JDBC

One, the basic steps of using JDBC to operate the database

Insert picture description here

Second, the database

(1) Database foundation

Insert picture description here
1. Select statement (used to query data in the data table)
Insert picture description here
2. Insert statement (used to insert new data into the data table)
Insert picture description here
3. Update statement (used to modify the data in the data table)
Insert picture description here
4. Delete statement (used to Delete the data in the data table)
Insert picture description here

(2) Connect to the database

Insert picture description here
Common errors in connecting to the database:
Insert picture description here
Insert picture description here
3. Incorrect user name and password
4. Incompatible version of the driver package

Case Kangkang:

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();
                        }
                    }
                }
            }
        }
    }
}

(3) Database query

1. Database query Insert picture description here
2. Create interface object
Insert picture description here
3. Common methods of ResultSet
Insert picture description here

(4) Dynamic query

1. Is your SQL safe?
Insert picture description here
To prevent this SQL injection, JDBC provides dynamic query
2. Dynamic query
Insert picture description here

Insert picture description here
case study:

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. Case analysis of adding, modifying and deleting records Insert picture description here
:

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();
                        }
                        
                    }
                }
            }
        }
    }
}

Guess you like

Origin blog.csdn.net/javaScript1997/article/details/108937119