Pages

標題

技術提供:Blogger.

2010/08/13

JAVA&ACCESS 讀取資料庫內資料並顯示執行結果

使用JAVA實做資料庫連線字串 並將ACCESS資料庫內資料讀取出來

顯示在畫面上


import java.io.*;
import java.sql.*;
import jxl.*;
public class AccseeODBCTest {
 public static void main(String[] argv){
  String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
  ResultSet re;
  try {
   Class.forName(driver); 
   Connection cnDB = DriverManager.getConnection("jdbc:odbc:myAccess");
   Statement stmt = cnDB.createStatement();
   re = stmt.executeQuery("SELECT COUNT(*) AS allTable FROM StudentTable");
   if(re.next()){
    if(re.getInt("allTable")==0)System.out.println("目前尚無資料");
   }
   re = stmt.executeQuery("SELECT * FROM StudentTable");
   while(re.next()){
    System.out.println(
     "客戶編號: " +re.getObject("客戶編號")+ "\n" +
     "客戶姓名: " +re.getObject("客戶姓名")+ "\n" +
     "---------------------------------------------\n"
    );
   }
   stmt.close();
   cnDB.close();
  } 
  catch(ClassNotFoundException e) { 
   System.out.println("找不到驅動程式類別"); 
  } 
  catch(SQLException e) { 
   System.out.println("找不到資料庫路徑"); 
   e.printStackTrace();
  }
 }// end of main
}// end of class

沒有留言:

張貼留言

 

Analytics