java 串連資料庫

來源:互聯網
上載者:User

標籤:cte   static   test   介面   man   擷取   sys   ack   col   

JDBC(Java Data Base Connectivity,java資料庫連接)是一種用於執行SQL語句的Java API,可以為多種關聯式資料庫提供統一訪問,它由一組用Java語言編寫的類和介面組成。是Java訪問資料庫的標準規範

 

JDBC是介面,驅動是介面的實現,沒有驅動將無法完成資料庫連接,從而不能操作資料庫!每個資料庫廠商都需要提供自己的驅動,用來串連自己公司的資料庫,也就是說驅動一般都由資料庫產生廠商提供。

串連資料庫步驟

 

1 1.註冊驅動.2         告知JVM使用的是哪一個資料庫的驅動3         //DriverManager.registerDriver(new Driver());4         //把com.mysql.jdbc.driver這個實作類別之間扔到記憶體裡5         //扔到記憶體之後調用static靜態代碼塊6         //靜態代碼塊中註冊驅動7         Class.forName("com.mysql.jdbc.Driver");

 

1 2.獲得串連.2         使用JDBC中的類,完成對MySQL資料庫的串連 3         //要串連的資料庫名字,寫死,編碼方式 ?characterEncoding=utf-84         String url ="jdbc:mysql://localhost:3306/goods?characterEncoding=utf-8";5         String user ="root";6         String password ="1";7         Connection conn =DriverManager.getConnection(url, user, password);
1 3.獲得語句執行平台2         通過連線物件擷取對SQL語句的執行者對象3         Statement sta =conn.createStatement();
4.執行sql語句        使用執行者對象,向資料庫執行SQL語句        擷取到資料庫的執行後的結果      //增加語句    /*    String str ="insert into sort values(‘3‘,‘文具‘,‘鋼筆‘)";        int i =se.executeUpdate(str);        System.out.println(i);*/        //修改語句 update 表名 set 要修改的表名   where 條件  /*        String str ="update sort set sdesc =‘paike‘ where sid =‘1‘";        int i =se.executeUpdate(str);        System.out.println(i);*/        //刪除語句    /*    String str ="delete from sort where sid =‘7‘";        int i = se.executeUpdate(str);        System.out.println(i);*/        //查詢語句        String str ="select * from sort";        ResultSet re =se.executeQuery(str);        while(re.next()){            System.out.println(re.getInt("sid")+re.getString("sname")+re.getString("sdesc"));        }
6.釋放資源.(先開的後關)        調用一堆close()方法        rs.close();        sta.close();        conn.close();*/

 封裝    重點

 1 這裡建立一個新的類 2 package com.orcale.demo; 3  4 import java.sql.Connection; 5 import java.sql.DriverManager; 6 import java.sql.ResultSet; 7 import java.sql.SQLException; 8 import java.sql.Statement; 9 10 public class Demo04 {11     private static Connection conn =null;      設定成全域變數12     static{ 13         try {    14             Class.forName("com.mysql.jdbc.Driver");   註冊程式,然後寫傳過去參數內容15             String url ="jdbc:mysql://localhost:3306/aaa";    16             String user = "root";17             String password ="1";18             conn =DriverManager.getConnection(url, user, password);    19         } catch (ClassNotFoundException | SQLException e) {   拋出異常20             e.printStackTrace();21         }22     }23         //擷取連線物件24         public static Connection getConnection(){   25             return conn;26         }27         //關閉資料庫28         public static void close(Connection conn,Statement stat,ResultSet re) throws SQLException{29             if(conn!=null){30                 conn.close();31             }32             if(stat!=null){33                 conn.close();34             }35             if(re!=null){36                 re.close();37             }38         }39         public static void close(Connection conn,ResultSet re) throws SQLException{40             if(conn!=null){41                 conn.close();42             }43             if(re!=null){44                 re.close();45             }46         }47 }
1     Connection conn =Demo04.getConnection();   調用方法擷取2         String sql ="select * from user";        sql語句3         Statement stat =conn.createStatement();      擷取語句執行平台4         ResultSet re =stat.executeQuery(sql);        調用5         while(re.next()){6             System.out.println(re.getString("name")+re.getString("password"));7         }

 

 

 

 

 

 

 

 


java 串連資料庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.