1. 程式人生 > 其它 >javaweb學習25:JDBC複習

javaweb學習25:JDBC複習

javaweb學習25:JDBC複習

  • JDBC:

    • 什麼是JDBC:Java連線資料庫;

       

    • 需要jar包的支援:

      • Java.sql

      • javax.sql

      • mysql-connector-java:連線驅動,必須要導

 

  • 實驗環境搭建:

    • 匯入資料庫依賴:

      <!--mysql的驅動-->
      <dependency>
         <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
         <version>8.0.28</version>
      </dependency>
    • IDEA中連線資料庫:

       

    • JDBC固定步驟:

      1. 載入驅動

      2. 連線資料庫

      3. 向資料庫傳送SQL的物件Statement

      4. 編寫SQL:根據業務編寫不同的SQL

      5. 執行SQL

      6. 關閉連線/流

       

 

  • 程式碼案例:JDBC基本程式碼Statement

    /**
     普通SQL執行物件:Statement
    */
    public class TestJdbc {
       public static void main(String[] args) throws ClassNotFoundException, SQLException {

           //配置資訊
           //useUnicode=true&characterEncoding=UTF-8 : 解決中文亂碼
           String url="jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=UTF-8";

           String username="demo";
           String password="demo";

           //1,載入驅動
           Class.forName("com.mysql.jdbc.Driver");
           //2,連線資料庫 : 代表資料庫
           Connection conn = DriverManager.getConnection(url, username, password);
           //3,向資料庫傳送SQL的物件Statement : CRUD
           //PreparedStatement安全的,
           Statement state = conn.createStatement();
           
           //4,編寫SQL
           String sql="SELECT * FROM users";
           
           
           //5,執行查詢SQL:返回一個ResultSet:結果集
           //增刪改都是用state.exeucteUpdate()
           ResultSet resultSet = state.executeQuery(sql);

           while (resultSet.next()){
               System.out.println("id="+resultSet.getInt("id"));
               System.out.println("name="+resultSet.getString("name"));
               System.out.println("password="+resultSet.getString("password"));
               System.out.println("email="+resultSet.getString("email"));
               System.out.println("birthday="+resultSet.getObject("birthday"));
          }

           //6,關閉連線:釋放資源(一定要做)先開後關
           resultSet.close();
           state.close();
           conn.close();


      }
    }

     

  • 程式碼案例:預編譯物件:PreparedStatement


    /**
    * 預編譯物件:PreparedStatement
    */
    public class TestJjdbc2 {

       public static void main(String[] args) throws ClassNotFoundException, SQLException {

           //配置資訊
           String url="jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf-8";
           String username="root";
           String password="root";

           //1,載入驅動
           Class.forName("com.mysql.jdbc.Driver");
           //2,建立連線:
           Connection conn = DriverManager.getConnection(url, username, password);
           //3,編寫SQL
           String sql="insert into users (id, name, password, email, birthday)value (?,?,?,?,?)";
           //4,預編譯SQL
           PreparedStatement pre=conn.prepareStatement(sql);
           //5,賦值:
           pre.setInt(1,4);//給第一個佔位符?的值賦值為1
           pre.setString(2,"demo04");//給第二個佔位符?的值賦值為demo04
           pre.setString(3,"111111");//給第三個佔位符?的值賦值為111111
           pre.setString(4,"[email protected]");//給第四個佔位符?的值賦值為[email protected]
           pre.setDate(5,new java.sql.Date(new Date().getTime()));//給第五個佔位符?的值賦值

           //6,執行SQL
           int i= pre.executeUpdate();
           if(i>0){
               System.out.println("插入成功");
          }

           //6,關閉連線
           pre.close();
           conn.close();


      }
    }