Java 程式設計 -- 資料庫

Java 程式

簡介

運算式

分枝

迴圈

陣列

函數

遞迴

錯誤處理

物件導向

封裝

繼承

多型

技巧

函式庫

字串

數學

正規表達式

容器

檔案

網路

資料庫

視窗

Thread

Listener

錯誤陷阱

相關檔案

相關資源

教學錄影

Eclipse

考題解答

訊息

相關網站

參考文獻

最新修改

簡體版

English

資料檔:person.mdb

範例:個人資料查詢 (簡易版)

import java.util.*;
import java.sql.*;
 
class Sql2 {
  public static void main(String[] args) throws Exception {
    Sql2 sql = new Sql2();
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:Driver="+
      "{Microsoft Access Driver (*.mdb)};DBQ=person;DriverID=22;READONLY=false}");
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT * FROM person");
    while (rs.next()) {
      String name = rs.getString("name");
      String tel  = rs.getString("tel");
      String email = rs.getString("email");
      String address = rs.getString("address");
      System.out.println(name);
      System.out.println("  電話 : "+tel);
      System.out.println("  E-mail : "+email);
      System.out.println("  地址 : "+address);
    }
    con.close();
  }
}

範例:個人資料查詢 (完整版)

import java.util.*;
import java.sql.*;
 
class Sql1 {
  Connection con = null;
  Statement  stmt = null;
  String     driver = "sun.jdbc.odbc.JdbcOdbcDriver";
  String     dbHead = "jdbc:odbc:";
 
  public static void main(String[] args) throws Exception {
    Sql1 sql = new Sql1();
    sql.openDB("person");
    ResultSet rs = sql.stmt.executeQuery("SELECT * FROM person");
    while (rs.next()) {
      String name = rs.getString("name");
      String tel  = rs.getString("tel");
      String email = rs.getString("email");
      String address = rs.getString("address");
      System.out.println(name);
      System.out.println("  電話 : "+tel);
      System.out.println("  E-mail : "+email);
      System.out.println("  地址 : "+address);
    }
    sql.closeDB();
  }
 
  void openDB(String dbName) throws Exception {
    Class.forName(driver);
    con = DriverManager.getConnection(dbHead+dbName);
    stmt = con.createStatement();
  }
 
  void closeDB() throws Exception {
    con.close();
  }
}

範例:取得資料庫的 Metadata

import java.util.*;
import java.sql.*;
 
class SqlMeta {
  public static void main(String[] args) throws Exception {
    SqlMeta sql = new SqlMeta();
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:Driver="+
      "{Microsoft Access Driver (*.mdb)};DBQ=person;DriverID=22;READONLY=false}");
    Statement stmt = con.createStatement();
    DatabaseMetaData meta = con.getMetaData();
    ResultSet mrs = meta.getCatalogs();
    System.out.println("===========meta==========\n"+meta);
    System.out.println("===========metaRs==========\n"+mrs);
    while (mrs.next()) {
        String field = mrs.getString("TABLE_CAT");
        System.out.println(field);
    }
    showDatabaseMetaData(meta);
  }
 
  public static void showDatabaseMetaData(DatabaseMetaData dbmd) throws SQLException {
/*    displayResult(dbmd.getProcedures(null,null,null));    */
    displayResult(dbmd.getTables(null,null,null,null));
/*    displayResult(dbmd.getColumns(null,null,null,null));
    displayResult(dbmd.getColumnPrivileges(null,null,null,null));
    displayResult(dbmd.getPrimaryKeys(null,null,null));
    displayResult(dbmd.getTypeInfo());    */
  }
 
  public static void displayResult(ResultSet rs) throws SQLException
  {
    ResultSetMetaData rsmd = rs.getMetaData();
    int count=0;
 
    // Print the result column names
    int cols = rsmd.getColumnCount();
    for(int i=1;i<=cols;i++)
      System.out.print(rsmd.getColumnLabel(i)+(i<cols?"\t":"\n"));
 
    // now the results
    while(rs.next()) {
      count++;
      for(int i=1;i<=cols;i++) {
    Object o = rs.getObject(i);
    if(rs.wasNull())
      System.out.print("{null}"+(i<cols?"\t":"\n"));
    else
      System.out.print(o.toString()+(i<cols?"\t":"\n"));
      }
    }
 
    System.out.println("Result returned "+count+" rows.");
 
    // finally close the result set
    rs.close();
  }
}

Facebook

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License