//// 一個簡單的利用JDBC(SYBAE jconnect-5_2)查詢Sybase ASA7.0中數(shù)據(jù)的圖形化Java程序范例// 執(zhí)行的SQL語句是" select * from employee " ,可以改成自己所需的.// 運(yùn)行方式為: c:\&; java JDBCTest //import java.awt.*; import java.sql.*; // 在使用JDBC之前,必須引入JAVA的SQL包class JDBCTest extends Frame {TextArea myTextArea;public JDBCTest () {//設(shè)定程序的顯示界面super("一個簡單的利用JDBC(jconnect-5_2)查詢Sybase ASA7.0中數(shù)據(jù)的圖形化Java程序范例");setLayout(new FlowLayout());myTextArea = new TextArea(30,80);add(myTextArea);resize(500,500);show();myTextArea.appendText("數(shù)據(jù)庫查詢中,請等待......\n");}void displayResults(ResultSet results) throws SQLException {//首先得到查詢結(jié)果的信息ResultSetMetaData resultsMetaData = results.getMetaData();int cols = resultsMetaData.getColumnCount();//將等待信息清除myTextArea.setText("");//顯示結(jié)果while(results.next()) {for(int i=1;i=cols;i++) {if(i>1)myTextArea.appendText("\t");try{myTextArea.appendText(results.getString(i));}// 捕獲空值時產(chǎn)生的異常catch(NullPointerException e){}}myTextArea.appendText("\n");}}public boolean handleEvent(Event evt) {if (evt.id == Event.WINDOW_DESTROY) {System.exit(0);return true;}return super.handleEvent(evt);}public static void main(String argv[]) throws SQLException,Exception {//設(shè)定查詢字串String queryString = "select * from employee";JDBCTest myJDBCTest = new JDBCTest();//加載驅(qū)動程序Class.forName("com.sybase.jdbc2.jdbc.SybDriver").newInstance();//建立連接,localhost為主機(jī)名,dba為用戶名,sql為密碼Connection myConn = DriverManager.getConnection("jdbc:sybase:Tds:localhost:2638","dba","sql");Statement myStmt = myConn.createStatement();//執(zhí)行查詢ResultSet myResults = myStmt.executeQuery(queryString);myJDBCTest.displayResults(myResults);//關(guān)閉所有打開的資源myResults.close();myStmt.close();myConn.close();}}
|