1. test SYBASEjconnect-5_2JDBC database interface with a single JSP file: % @ pagecontentTypetexthtml; charsetGB2312 % @ pageimportjava. SQL. * % HTMLHEADTITLEJSP test SYBASEjconnect-5_2JDBC database interface TITLEMETANAMEGeneratorCONTENTEditPlus2.11ME SybaseJDBC
1. use a single JSP file to test the SYBASE jconnect-5_2 JDBC database interface:
<% @ Page contentType = "text/html; charset = GB2312" %>
<% @ Page import = "java. SQL. *" %>
JSP test SYBASE jconnect-5_2 JDBC database interface
JSP test SYBASE jconnect-5_2 JDBC database interface
<%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 rs1_stmt.exe cuteQuery (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 ") +" |
");}%>
<% Out. print ("database operation successful, congratulations"); %>
<%
Rs. close ();
Stmt. close ();
Conn. close ();
%>
2. use JSP and Java Bean methods:
JSP Code:
<% @ Page contentType = "text/html; charset = GB2312" %>
<% @ Page import = "java. SQL. *" %>
<%
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 code:
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.exe cuteQuery (SQL );
Return rs;
}
}
3. use JDBC (SYBAE jconnect-5_2) to query the data in Sybase ASA7.0 graphical Java program example:
//
// A simple example of a graphical Java program that uses JDBC (SYBAE jconnect-5_2) to query data in Sybase ASA7.0
// The executed SQL statement is "select * from employee", which can be changed to what you need.
// Running mode: c: \> java JDBCTest
//
Import java. awt .*;
Import java. SQL. *; // before using JDBC, you must introduce the java SQL package
Class JDBCTest extends Frame {
TextArea myTextArea;
Public JDBCTest (){
// Set the display interface of the program
Super ("A simple example of a graphical Java program that uses JDBC (jconnect-5_2) to query data in Sybase ASA7.0 ");
SetLayout (new FlowLayout ());
MyTextArea = new TextArea (30, 80 );
Add (myTextArea );
Resize (500,500 );
Show ();
MyTextArea. appendText ("querying the database, please wait... \ n ");
}
Void displayResults (ResultSet results) throws SQLException {
// Obtain the query result first.
ResultSetMetaData resultsMetaData = results. getMetaData ();
Int cols = resultsMetaData. getColumnCount ();
// Clear the waiting information
MyTextArea. setText ("");
// Display the result
While (results. next ()){
For (int I = 1; I <= cols; I ++ ){
If (I> 1)
MyTextArea. appendText ("\ t ");
Try {
MyTextArea. appendText (results. getString (I ));
}
// Exceptions generated when null values are captured
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 {
// Set the query string
String queryString = "select * from employee ";
JDBCTest myJDBCTest = new JDBCTest ();
// Load the driver
Class. forName ("com. sybase. jdbc2.jdbc. SybDriver"). newInstance ();
// Establish a connection. localhost is the host name, dba is the user name, and SQL is the password.
Connection myConn = DriverManager. getConnection ("jdbc: sybase: Tds: localhost: 2638", "dba", "SQL ");
Statement myStmt = myConn. createStatement ();
// Execute the query
ResultSet myResults = myStmt.exe cuteQuery (queryString );
MyJDBCTest. displayResults (myResults );
// Close all open resources
MyResults. close ();
MyStmt. close ();
MyConn. close ();
}
}