Java資料庫連接技術——JDBC,java資料庫連接jdbc

來源:互聯網
上載者:User

Java資料庫連接技術——JDBC,java資料庫連接jdbc

  大家好,今天我們學習了Java如何串連資料庫。之前學過.net語言的資料庫操作,感覺就是一通百通,大同小異。

  JDBC是Java資料庫連接技術的簡稱,提供串連各種常用資料庫的能力。

    JDBC API (主要功能:與資料庫建立串連、執行語句、處理結果):

      提供者:Sun公司

      內容:供程式員調用的介面與類,整合在java.sql和javax.sql(後面的x是extend,擴充的意思)。如:

        DriverManager類(管理各種不同的JDBC驅動),

        Connection介面(串連資料、傳送資料),

        statement介面(執行SQL語句),

        ResultSet介面(根據statement返回的結果集,來處理結果)。

  編寫步驟:

    1.載入JDBC驅動;

    2.建立資料庫連接對象;

    3.建立命令對象statement,來執行SQL語句並返回結果集;

    4.ResultSet通過返回的結果集,來處理結果;

    5.關閉資料庫連接,釋放資源。

  舉一個驗證使用者登入的例子吧:

  

package jdbc;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.Scanner;/** * JDBC串連資料庫.java * 2017-1-6 上午11:14:48 * John */public class Test {    public static void main(String[] args) {        Scanner input=new Scanner(System.in);        System.out.println("請輸入使用者名稱:");        String username=input.next();        System.out.println("請輸入密碼:");        String userpwd=input.next();        //1.載入JDBC驅動        try {            Class.forName("com.mysql.jdbc.Driver");//MySQL使用的磁碟機        } catch (ClassNotFoundException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        //2.建立資料庫連接對象        Connection conn=null;        Statement stat=null;        ResultSet rs=null;        try {                        //mysql串連URL編寫格式為:                        //jdbc:mysql://主機名稱:串連連接埠/資料庫名稱            String url="jdbc:mysql://localhost:3306/Pet";             conn=DriverManager.getConnection(url, "root","123456");            //3.建立命令對象statement            stat=conn.createStatement();            //4.執行命令,並返回結果集            String sql="select count(1) as cnt from users where UserName='"+username+"' and userpwd='"+userpwd+"'";            rs=stat.executeQuery(sql);            while(rs.next()){                int count=rs.getInt("cnt");                if (count==1) {                    System.out.println("登入成功!");                }else {                    System.out.println("登入失敗!");                }            }        } catch (SQLException e) { //此處記住要對異常進行處理            // TODO Auto-generated catch block            e.printStackTrace();        }finally{            //5.關閉資料庫連接            try {                conn.close();                stat.close();                rs=null;            } catch (SQLException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }    }}                                                                                                                                                                                

 

  以上是我的個人觀點,有異議的地方望大家指正,共同學習,共同進步!

  

  

 

 

 

 

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.