Jdbc接続Syboaseデータベースのいくつかの方法


1.JSPファイル一つでSYBASE jconnect-5_をテストします。2 JDBCデータベースインターフェース:
<%@ page contentType="text/html;charset=GB2312" %><%@ page import="java.sql.*" %><HTML><HEAD><TITLE> JSP  SYBASE jconnect-5_2 JDBC      </TITLE><META NAME="Generator" CONTENT="EditPlus2.11"><META NAME="Author" CONTENT="naxin"></HEAD><BODY><center>JSP  SYBASE jconnect-5_2 JDBC     </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>");out.print("<TD>"+rs.getString("city")+"</TD>");out.print("<TD>"+rs.getString("state")+"</TD></TR>");}%> </table><BR><HR><%out.print("");%> <%rs.close(); stmt.close(); conn.close(); %>  </BODY></HTML>
2.JSPとJava Beanを使う方法:
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   :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)Syboase ASA 7.0におけるデータのグラフィカル化Javaプログラムの例を問い合わせる:
////          JDBC(SYBAE jconnect-5_2)  Sybase ASA7.0       Java    //       SQL   " select * from employee "  ,         .//         : c:\> java JDBCTest  //import java.awt.*;      import java.sql.*;   //    JDBCJAVA 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("\t");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 ,dbasql 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();}}