import java.sql.*;
public class NewODBC {
// 試著將以下的設定以 properties 的檔案讀進來
// 設定 ODBC-JDBC 驅動程式的名稱
static String classname = "sun.jdbc.odbc.JdbcOdbcDriver";
// ODBC's DNS 設為 csie,你也可以設成其他名稱
static String jdbcURL = "jdbc:odbc:csie";
static String UID = "jlu";
static String PWD = "newpasswd";
static Connection conn = null;
public static void main( String argv[] ) {
// 說明這個程式的使用方式為執行:java NewODBC Product
if(argv.length != 1) {
System.out.println("Usage: java NewODBC Product");
System.exit(2);
}
// 查詢用的 SQL 字串
String aQuery = "select * from " + argv[0];
// 新增資料的 SQL 字串
String iSQL = "insert into " + argv[0] + " values(5,'鍵盤',14.5,2)";
// 修改資料的 SQL 字串
String uSQL = "update " + argv[0] + " set Name='無線鍵盤' where ID=5";
// 刪除資料的 SQL 字串
String dSQL = "delete from " + argv[0] + " where ID=5";
try {
// 載入 JDBC-ODBC 驅動程式
Class.forName(classname);
// 連結資料庫(藉由 dns、帳號、密碼)
conn = DriverManager.getConnection(jdbcURL,UID,PWD);
// 顯示目前表格的內容
System.out.println("Display current content");
ShowResults(aQuery);
// 新增資料並顯示目前表格的內容
System.out.println("\nInserting a new record .....");
InsertNew(iSQL);
ShowResults(aQuery);
// 修改資料並顯示目前表格的內容
System.out.println("\nUpdateing a record .....");
UpdateNew(uSQL);
ShowResults(aQuery);
// 刪除資料並顯示目前表格的內容
System.out.println("\nDeleting a record .....");
DeleteNew(dSQL);
ShowResults(aQuery);
conn.close();
} catch (Exception sqle) {
System.out.println(sqle);
System.exit(1);
}
}
private static void DeleteNew(String dSQL) {
try {
Statement aStatement = conn.createStatement();
aStatement.executeUpdate(dSQL);
} catch (Exception e) {
System.out.println("Delete Error: " + e);
System.exit(1);
}
}
private static void UpdateNew(String uSQL) {
try {
Statement aStatement = conn.createStatement();
aStatement.executeUpdate(uSQL);
} catch (Exception e) {
System.out.println("Update Error: " + e);
System.exit(1);
}
}
private static void InsertNew(String iSQL) {
try {
Statement aStatement = conn.createStatement();
// 新增、修改、刪除都跟查詢一樣,在執行 SQL 字串前,必須先
// 產生 statement 物件。但是跟查詢不一樣的地方在於,真正
// 執行的方法用的是 executeUpdate(),而不是 executeQuery()
aStatement.executeUpdate(iSQL);
} catch (Exception e) {
System.out.println("Insert Error: " + e);
System.exit(1);
}
}
private static void ShowResults(String aQuery) {
try {
// 產生一個 SQL statement 物件並利用之前建立的連線 conn
// 送給 MySQL,然後在伺服器端執行該 statement 物件中的字串
Statement aStatement = conn.createStatement();
ResultSet rs = aStatement.executeQuery(aQuery);
// 執行 SQL 字串後的結果被置放於一個 ResultSet 的物件
// 依據 SQL 的原理,回傳的結果其實也是一個表格(table)
// 因此 ResultSet 的物件包含該表格的 Metadata 以及資料
// 表格的 Medata 可以經由 ResultSet 物件的 getMetaData 的
// 方法取得,而取得的 Metadata 由一個 ResultSetMetaData 物件代表
ResultSetMetaData rsmeta = rs.getMetaData();
// 由 getColumnCount() 可以知道表格共有幾個欄位
int cols = rsmeta.getColumnCount();
// 經由 for 迴圈可以得出每一個欄位的名稱
for(int i=1; i<=cols; i++)
{
if(i > 1) System.out.print("\t");
// 由 getColumnLabel(i) 可以取得第 i 個欄位名稱
System.out.print(rsmeta.getColumnLabel(i));
}
System.out.print("\n");
// 經由 while 迴圈可以將資料一筆一筆取出來
// rs.next() 是查詢是否還有下一筆;若有,為 true;反之,為 false
while(rs.next()) {
for(int i=1; i<=cols; i++)
{
if (i > 1) System.out.print("\t");
// rs.getString(i) 取得第 i 欄的資料並以字串的方式回傳
System.out.print(rs.getString(i));
}
System.out.print("\n");
}
// 將 rs 以及 statement 清掉。這個動作很重要,如果不清除的話,
// 在資料量很大的情形下,Java 垃圾處理器來不及處理的話,就會出現異常
aStatement.close();
}
// a better exception handling can be used here.
catch (Exception e) {
System.out.println("Exception Occurs.");
}
}
}