1.單用一個JSP文件測試Sybase(Sybase認證 Sybase培訓 ) jconnect-5_2 JDBC數據庫接口:
JSP測試Sybase jconnect-5_2 JDBC數據庫接口
JSP測試Sybase jconnect-5_2 JDBC數據庫接口
<%
Class.forName("com.Sybase.jdbc2.jdbc.SybDriver");
String url ="jdbc:Sybase:Tds:localhost:2638";
Connection conn= DriverManager.getConnection(url, "dba","sql");
Statement stmt=conn.createStatement();
String sql="select emp_lname,dept_id,street,city,state from employee order by emp_lname";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
out.print(""+rs.getString("emp_lname")+"");
out.print(""+rs.getString("dept_id")+"");
out.print(""+rs.getString("street")+"");
out.print(""+rs.getString("city")+"");
out.print(""+rs.getString("state")+"");
}
%>
<%
rs.close();
stmt.close();
conn.close();
%>
2.再用JSP和Java Bean的方法:
JSP Code:
<%
ResultSet rs=Sybase.Query("select * from tjck_dh");
while(rs.next()) {
out.print("|"+rs.getString("name")+"|");
out.print(rs.getString("card_no")+"|");
out.print(rs.getString("amount")+"|");
out.print(rs.getString("home_call")+"|");
out.print(rs.getString("Office_call")+"|
");
}
rs.close();
%>
Bean的代碼:
package test;
import Java.sql.*;
public class sybconn {
// String sDBDriver = "com.Sybase.jdbc2.jdbc.SybDriver";
String sConnStr = "jdbc:Sybase:Tds:localhost:2638";
// String user="dba";
// String passwd="sql";
Connection conn = null;
ResultSet rs = null;
public ResultSet Query(String sql) throws SQLException,Exception {
Class.forName("com.Sybase.jdbc2.jdbc.SybDriver").newInstance();
conn = DriverManager.getConnection(sConnStr,"dba","sql");
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
return rs;
}
}
3.利用JDBC(SYBAE jconnect-5_2)查詢Sybase ASA7.0中數據的圖形化Java程序范例:
//
// 一個簡單的利用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("數據庫查詢中,請等待......
");
}
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("");
try{
myTextArea.appendText(results.getString(i));
}
// 捕獲空值時產生的異常
catch(NullPointerException e){
}
}
myTextArea.appendText("
");
}
}
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();
}
}