Methods for connecting Jdbc to Sybase databases

Source: Internet
Author: User

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. * "%> <HTML> <HEAD> <TITLE> JSP test SYBASE jconnect-5_2 JDBC database interface </TITLE> <meta name =" Generator "CONTENT =" EditPlus2.11 "> <META NAME = "Author" CONTENT = "naxin"> </HEAD> <BODY> <center> JSP test SYBASE jconnect-5_2 JDBC database interface </center> <BR> <table border = 3 align = center> <% 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 ("<TR> <TD>" + rs. getString ("emp_lname") + "</TD>"); out. print ("<TD>" + rs. getString ("dept_id") + "</TD>"); out. print ("<TD>" + rs. getString ("street") + "</TD>"); out. print ("<TD>" + rs. getString ("city") + "</TD>"); out. print ("<TD>" + rs. getString ("state") + "</TD> </TR>") ;}%> </table> <BR> <HR> <% out. print ("succeeded in database operation, congratulations"); %> <% rs. close (); stmt. close (); conn. close (); %> </BODY> </HTML>


2. Use JSP and Java Bean methods:
JSP Code: <% @ page contentType = "text/html; charset = GB2312" %> <% @ page import = "java. SQL. * "%> <HTML> <HEAD> <TITLE> </TITLE> <meta name =" Generator "CONTENT =" EditPlus2.11 "> <meta name =" Author "CONTENT =" naxin "> </HEAD> <BODY> <jsp: useBean id = "sybase" scope = "page" class = "test. sybconn "/> <% 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") + "| <br>");} rs. close () ;%> <HR> 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 ", you can change it to what you need. // run in 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 (500,500); add (myTextArea); resize (); show (); myTextArea. appendText ("database query in progress, please wait ...... \ n ");} void displayResults (ResultSet results) throws SQLException {// obtain the query result first. ResultSetMetaData resultsMetaData = results. getMetaData (); int cols = resultsMetaData. getColumnCount (); // clears the waiting information for 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);} // catch exception (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 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 username, and SQL is the password Connection myConn = DriverManager. getConnection ("jdbc: sybase: Tds: localhost: 2638", "dba", "SQL"); Statement myStmt = myConn. createStatement (); // execute ResultSet myResults = myStmt.exe cuteQuery (queryString); myJDBCTest. displayResults (myResults); // close all open resources myResults. close (); myStmt. close (); myConn. close ();}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.