Several methods of JDBC even Sybase database _jsp programming

Source: Internet
Author: User
Tags dba generator stmt sybase sybase database
1. test SYBASE jconnect-5_2 JDBC Database interface with one JSP file :

<%@ page contenttype= "text/html;charset=gb2312"%><%@ page import= "java.sql.*"%>TestSYBASE jconnect-5_2 JDBCDatabase Interface</title><meta name= "generator" content= "EditPlus2.11" ><meta name= "Author" content= "Naxin" > TestSYBASE jconnect-5_2 JDBCDatabase Interface</center><br><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 rs=stmt.executequery (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> "); o Ut.print ("<TD>" +rs.getstring ("City") + "</TD>") out.print ("<TD>" +rs.getstring ("state") + "</ Td></tr> ");} %> </table><br>database operation successful, congratulations!");%> <%rs.close (); Stmt.close (); Conn.close (); %> </BODY></HTML>


2. reuse the 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 ">the Code:p ackage test;import java.sql.*;p ublic 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. use JDBC (sybae jconnect-5_2) to query examples of graphical Java programs for data in Sybase ASA7.0 :
//// a simple use ofJDBC (sybae jconnect-5_2)QuerySybase ASA7.0the graphical data inJavaProgram Examples// Execution ofSQLstatement is"SELECT * from Employee",You can change what you want..// run in the form of: c:\> java jdbctest//import java.awt.*; import java.sql.*;//in useJDBCbefore, you must introduceJAVAof theSQLPackageclass Jdbctest extends Frame {TextArea mytextarea;public jdbctest () {//set the display interface of the programSuper ("a simple use ofJDBC (jconnect-5_2)QuerySybase ASA7.0the graphical data inJavaProgram Examples"); SetLayout (new FlowLayout ()); Mytextarea = new TextArea (30,80); add (Mytextarea); resize (500,500); show (); Mytextarea.appendtext ("in the database query, please wait... \ n ");} void DisplayResults (ResultSet results) throws SQLException {//first get information about the results of the queryResultSetMetaData resultsmetadata = Results.getmetadata (); int cols = Resultsmetadata.getcolumncount ();//will wait for information to clearMytextarea.settext ("");Show ResultsWhile (Results.next ()) {for (int i=1;i<=cols;i++) {if (i>1) mytextarea.appendtext ("\ t"); try{ Mytextarea.appendtext (results.getstring (i));} // exceptions that occur when a null value is caughtcatch (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 query stringString querystring = "SELECT * FROM Employee"; Jdbctest myjdbctest = new Jdbctest ();//Load Driverclass.forname ("Com.sybase.jdbc2.jdbc.SybDriver"). Newinstance ()//Establish a connection, localhostas host name, DBAas the user name,SQLas a passwordConnection myconn = drivermanager.getconnection ("jdbc:sybase:tds:localhost:2638", "DBA", "SQL"); Statement mystmt = Myconn.createstatement ();//Execute QueryResultSet myresults = Mystmt.executequery (querystring); Myjdbctest.displayresults (myresults);to close all open resources myresults.close (); Mystmt.close (); Myconn.close ();}} 

-->

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.