測試ODBC串連MySQL資料庫

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   io   os   java   ar   

下面這個例子是最簡單的ODBC串連MySQL資料庫的例子。

一般步驟:

1.註冊驅動;

2.建立串連;

3.建立語句;

4.處理結果;

5.釋放資源。

注意:

1.軟體開發環境:MyEclipse 8.5+ MySQL5.1

2.代碼運行前需要載入Mysql驅動包;

資料庫資訊:

 

源碼如下:

import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;/*********************************** * 測試ODBC串連MySQL資料庫 *  * MySQL資料庫  * 登陸帳號:root * 登陸密碼:toor *  * 建立資料庫名稱:testODBC * 表名:T_users * 欄位: *       ID          int *       username    varchar 255 *       password    varchar 255 *        **********************************/public class ODBC_conn_MySQL {        public static void main(String[] args) throws SQLException {        // 1.註冊驅動        DriverManager.registerDriver(new com.mysql.jdbc.Driver());        // 2.建立串連        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testODBC", "root", "toor");        // 建立語句        Statement st = conn.createStatement();        // 執行語句        ResultSet rs = st.executeQuery("select * from T_users");        // 處理結果        while (rs.next()) {                        // 讀取並儲存資料庫查詢結果            int x = rs.getInt("ID");            String s1 = rs.getString("username");            String s2 = rs.getString("password");                        // 列印結果            System.out.println("ID=" + x);            System.out.println("username=" + s1);            System.out.println("password=" + s2);        }                //釋放資源        rs.close();        st.close();        conn.close();    }}

運行:

測試ODBC串連MySQL資料庫

聯繫我們

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