Java 程式設計 -- 資料庫
Java 程式簡介運算式分枝迴圈陣列函數遞迴錯誤處理物件導向封裝繼承多型技巧函式庫字串數學正規表達式容器檔案網路資料庫視窗ThreadListener錯誤陷阱相關檔案相關資源教學錄影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(); } } 範例:取得資料庫的 Metadataimport 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(); } } |
page revision: 5, last edited: 04 Nov 2010 05:15
my question is ~ how do you know what to import ?
import java.util.*;
import java.sql.*;
Post preview:
Close preview