java串連本機access 資料庫的方法__資料庫

來源:互聯網
上載者:User

本代碼實現串連 本機資料庫的方法。

 

操作步驟:

1、進入控制台,開啟“管理工具→資料來源(ODBC)”,彈出“ODBC資料來源管理器”,在“使用者DSN”選項卡中,單擊選中名稱為“Visio Database Sample”,驅動程式為“Microsoft Access Driver(*.mdb,*.accdb)”的選項(注意:*.mdb 是ACCESS 2003的資料庫副檔名,*.accdb是access 2007及以上的副檔名,如果看不到該選項,請確認已經安裝access軟體),然後單擊 “添加”按鈕,彈出“建立新資料來源”對話方塊,選擇“Microsoft Access Driver(*.mdb,*.accdb)”(對access 2003 和 access2007均適用),單擊“完成”按鈕,彈出 "ODBC Microsoft Access 安裝" 對話方塊,在 資料來源名 中輸入 你的access 資料庫名稱,比如 “book”,然後單擊 “選擇”按鈕,在彈出的 對話方塊中找到 你電腦上的資料庫檔案(如我的 “book”資料庫檔案),最後單擊確定按鈕,返回相應的對話方塊,然後選擇 “確定” “完成”,最後回到 “ODBC資料來源管理器”可以看到 使用者資料來源中出現了“book”資料來源,

2、串連資料庫的關鍵語句:

載入驅動程式:
String sDriver="sun.jdbc.odbc.JdbcOdbcDriver";
Class.forName(sDriver);

串連資料庫:
Connection dbCon=null;
String sCon="jdbc:odbc:book"; //book 就是資料庫名稱
dbCon=DriverManager.getConnection(sCon);

執行資料庫操作:
Statement stmt=stmt=dbCon.createStatement();
String sSQL="SELECT * "+" FROM bookindex";
ResultSet rs=stmt.executeQuery(sSQL);
while(rs.next()){
 int num;
 System.out.print(rs.getString("BookID")+"  ");  //輸出對應欄位的值
 System.out.print(rs.getString("BookTitle")+"  ");
 System.out.print(rs.getString("BookAuthor"));
 System.out.println("  " +rs.getFloat("BookPrice"));
}

關閉資料庫連接
stmt.close();
dbCon.close();

執行個體代碼如下(有 book資料庫,表bookindex,欄位有 :BookID、BookTitle、BookAuthor、BookPrice)


 

import java.sql.*;public class DBconnTest {public static void main(String args[]) {//步驟1:載入驅動程式String sDriver="sun.jdbc.odbc.JdbcOdbcDriver";try{Class.forName(sDriver);}catch(Exception e){System.out.println("無法載入驅動程式");return;}System.out.println("步驟1:載入驅動程式——成功。");Connection dbCon=null;Statement stmt=null;String sCon="jdbc:odbc:book";try{dbCon=DriverManager.getConnection(sCon);if(dbCon!=null){System.out.println("步驟2:串連資料庫——成功。");}//步驟3:建立JDBC的Statement對象stmt=dbCon.createStatement();if(stmt!=null){System.out.println("步驟3:建立JDBC的Statement對象——成功。");}}catch(SQLException e){System.out.println("串連錯誤:"+sCon);System.out.println(e.getMessage());if(dbCon!=null){try{dbCon.close();}catch(SQLException e2){}}return;}try{//執行資料庫查詢,返回結果String sSQL="SELECT * "+" FROM bookindex";ResultSet rs=stmt.executeQuery(sSQL);while(rs.next()){System.out.print(rs.getString("BookID")+"  ");System.out.print(rs.getString("BookTitle")+"  ");System.out.print(rs.getString("BookAuthor"));System.out.println("  " +rs.getFloat("BookPrice"));}}catch(SQLException e){System.out.println(e.getMessage());}            finally{                try{                    //關閉步驟3所開啟的statement對象                    stmt.close();                    System.out.println("關閉statement對象");                }                catch(SQLException e){}                try{                    //關閉資料庫連接                    dbCon.close();                    System.out.println("關閉資料庫連接對象");                }                catch(SQLException e){}           }      }}


上面的方法問題:只對本機有效,換個電腦就找不到資料來源了,需要重新設定 access資料來源

 

在串連資料庫時,可以直接指定資料庫 的路徑,最好將資料庫和源檔案放到一個目錄裡,這樣就不用再配置資料來源了。方法如下:

將指定資料來源語句代碼更改為如下:

原先代碼:

String sCon="jdbc:odbc:book";

更改後代碼:(注意,這裡將資料庫與程式源檔案放到同一目錄下,如果用 eclipse 則是放到 專案檔夾中,與.classpath 同級。

String sCon = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=book.mdb";

也可以在eclipse 中建立個檔案夾 比如 DB 檔案夾,則語句還要加上目錄如下:

String sCon = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=DB/book.mdb";

 

執行個體如下:

import java.sql.*;public class DBconnTest {public static void main(String args[]) {//步驟1:載入驅動程式String sDriver="sun.jdbc.odbc.JdbcOdbcDriver";try{Class.forName(sDriver);}catch(Exception e){System.out.println("無法載入驅動程式");return;}System.out.println("步驟1:載入驅動程式——成功。");Connection dbCon=null;Statement stmt=null;String sCon = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=book.mdb";try{dbCon=DriverManager.getConnection(sCon);if(dbCon!=null){System.out.println("步驟2:串連資料庫——成功。");}//步驟3:建立JDBC的Statement對象stmt=dbCon.createStatement();if(stmt!=null){System.out.println("步驟3:建立JDBC的Statement對象——成功。");}}catch(SQLException e){System.out.println("串連錯誤:"+sCon);System.out.println(e.getMessage());if(dbCon!=null){try{dbCon.close();}catch(SQLException e2){}}return;}try{//執行資料庫查詢,返回結果String sSQL="SELECT * "+" FROM bookindex";ResultSet rs=stmt.executeQuery(sSQL);while(rs.next()){System.out.print(rs.getString("BookID")+"  ");System.out.print(rs.getString("BookTitle")+"  ");System.out.print(rs.getString("BookAuthor"));System.out.println("  " +rs.getFloat("BookPrice"));}}catch(SQLException e){System.out.println(e.getMessage());}finally{try{//關閉步驟3所開啟的statement對象stmt.close();System.out.println("關閉statement對象");}catch(SQLException e){}try{//關閉步驟3所開啟的statement對象dbCon.close();System.out.println("關閉資料庫連接對象");}catch(SQLException e){}}}}


 

 

 

 

聯繫我們

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