" rs.getstring ("state") " td> tr>");
}
%>
TABLE>
<% out.print ("Database Success, Congratulations");%>
<%
Rs.close ();
Stmt.close ();
CONN.CLOSE ();
%>
Body>
Html>
2. Use JSP and Java Beans:
JSP code:
<% @ Page ContentType = "Text / HTML; Charset = GB2312"%> <% @ Page Import = "Java.sql. *"%>
title>
HEAD>
<%
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.executequery (SQL);
Return RS;
}
}
3.
Query the graphical Java program of data in Sybase ASA7.0 using JDBC (Sybae JConnect-5_2):
//
// A simple use of JDBC (Sybae JConnect-5_2) querying the graphical Java program example of data in Sybase ASA7.0
// The SQL statement that is executed is "Select * from Employee", which can be changed to yourself .// operation mode is: C: /> Java JDBCTEST
//
Import java.awt. *;
Import java.sql. *; // must introduce Java's SQL package before using JDBC
Class JDBCTEST EXTENDS FRAME {
Textarea mytextarea;
Public JDBCTEST () {
// Set the display interface of the program
Super ("A simple use of JDBC (JCONNECT-5_2) querying the graphical Java program example of data in Sybase ASA7.0");
SetLayout (New FlowLayout ());
Mytextarea = New Textarea (30, 80);
Add (MyTextArea);
Resize (500, 500);
SHOW ();
MytextArea.AppendText ("Database query, please wait ... / N");
}
Void DisplayResults (ResultSet Results) throws sqlexception {
// First get information about the results of the query
ResultSetMetadata ResultsMetAdata = Results.getMetadata ();
INT cols = resultmetadata.getcolumncount ();
// Waiting for information to clear
mytextArea.setText ("");
//show result
WHILE (results.next ()) {
For (INT i = 1; i <= cols; i ) {
IF (i> 1)
MytextArea.AppendText ("/ t");
Try {
MytextArea.AppendText (Results.getstring (i));
}
// An abnormality generated when capturing null values
Catch (nullpointersexception 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, SQL is a password
Connection myconn = drivermanager.getConnection ("JDBC: Sybase: TDS: localhost: 2638", "dba", "sql");
Statement myStmt = myconn.createstatement ();
// Execute Query
ResultSet myresults = mystmt.executequery (querystring);
Myjdbctest.displayResults (MyResults); // Close all open resources
MyResults.close ();
MyStmt.close ();
MyConn.close ();
}
}
转载请注明原文地址:https://www.9cbs.com/read-28604.html
|