ベースJDBC API使用


ベースJDBC API使用
  • 基本フロー
  • 保存動作
  • 変更操作削除操作
  • 基本プロセス
    
    import com.mysql.jdbc.Driver;
    import org.junit.Test;
    
    import java.sql.*;
    
    public class Demo1 {
        @Test
        public void demo1() {
            //               ,   try  
            Connection conn = null;
            Statement stmt = null;
            ResultSet rs = null;
            //1.    
            try {
                //DriverManager.registerDriver(new Driver());
                //         
                //   Class.forName()  
                Class.forName("com.mysql.cj.jdbc.Driver");
                //2.    
                conn = DriverManager.getConnection("jdbc:mysql:///jdbctest", "root", "root");
                //      DriverManager
                //Connection       sql     
                //Statement createStatement/                     ===>   sql  (    sql    )
                //PreparedStatement prepareStatement(String sql) ===>    sql  
                //CallableStatement prepareCall(String sql)      ===>   sql     
    
                //    
                //setAutoCommit(boolean autoCommit) ===>         
                //commit()                          ===>     
                //rollback()                        ===>     
                //3.    
                String sql = "select * from user";
                stmt = conn.createStatement();
                //Statement
                //boolean execute(String sql)            ===>  sql(  select    true/  false)
                //ResultSet executeQuery(String sql)     ===>sql select  
                //int executeUpdate(String sql)          ===>sql insert/update/delete  
    
                //   
                //addBatch(String sql)        ===>      
                //executeBatch()              ===>     
                //clearBatch()                ===>     
                rs = stmt.executeQuery(sql);
                //   (        )
                //next()         
                //              getString...
                //    getObject   
                while (rs.next()) {
                    int uid = rs.getInt("uid");
                    String username = rs.getString("username");
                    String password = rs.getString("password");
                    String name = rs.getString("name");
                    System.out.println(uid + "" + username + " " + password + " " + name);
                }
                //4.    
                //rs.close();
                //stmt.close();
                //conn.close();
                //connection      ,       
                //             final 
    
            } catch (SQLException | ClassNotFoundException e) {
                e.printStackTrace();
            } finally {
                //      
                if (rs != null) {
                    try {
                        rs.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    rs = null;
                    //       
                }
                if (stmt != null) {
                    try {
                        stmt.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    stmt = null;
                }
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    conn = null;
                }
            }
    
        }
    

    保存アクション
    public class Demo2 {
        @Test
        public void demo1() {
            Connection conn = null;
            Statement stmt = null;
            ResultSet rs = null;
    
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
    
                conn = DriverManager.getConnection("jdbc:mysql:///jdbctest","root","root");
    
                stmt = conn.createStatement();
    
                String sql = "insert into user values(null,'eee','123','  ')";
    
                int i = stmt.executeUpdate(sql);
    
                if(i>0){
                    System.out.println("success");
                }
    
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (stmt != null) {
                    try {
                        stmt.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    stmt = null;
                }
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    conn = null;
                }
            }
        }
    }
    

    変更操作削除操作
    基本的な類似はsql文を変更するだけでよい
    String sql = "update user set username = 'qqq',password = '423', name ='  ' where uid = 4";
    
    String sql = "delete from user where uid = 4";