//// 一个简单的利用JDBC(SYBAE jconnect-5_2)查询Sybase ASA7.0中数据的图形化Java程序范例// 执行的SQL语句是" select * from employee " ,可以改成自己所需的.// 运行方式为: c:\&; java JDBCTest //import java.awt.*; import java.sql.*; // 在使用JDBC之前,必须引入JAVA的SQL包class JDBCTest extends Frame {TextArea myTextArea;public JDBCTest () {//设定程序的显示界面super("一个简单的利用JDBC(jconnect-5_2)查询Sybase ASA7.0中数据的图形化Java程序范例");setLayout(new FlowLayout());myTextArea = new TextArea(30,80);add(myTextArea);resize(500,500);show();myTextArea.appendText("数据库查询中,请等待......\n");}void displayResults(ResultSet results) throws SQLException {//首先得到查询结果的信息ResultSetMetaData resultsMetaData = results.getMetaData();int cols = resultsMetaData.getColumnCount();//将等待信息清除myTextArea.setText("");//显示结果while(results.next()) {for(int i=1;i=cols;i++) {if(i>1)myTextArea.appendText("\t");try{myTextArea.appendText(results.getString(i));}// 捕获空值时产生的异常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 {//设定查询字串String queryString = "select * from employee";JDBCTest myJDBCTest = new JDBCTest();//加载驱动程序Class.forName("com.sybase.jdbc2.jdbc.SybDriver").newInstance();//建立连接,localhost为主机名,dba为用户名,sql为密码Connection myConn = DriverManager.getConnection("jdbc:sybase:Tds:localhost:2638","dba","sql");Statement myStmt = myConn.createStatement();//执行查询ResultSet myResults = myStmt.executeQuery(queryString);myJDBCTest.displayResults(myResults);//关闭所有打开的资源myResults.close();myStmt.close();myConn.close();}}
|