Jdbc连Sybase数据库的几种方法
程序员文章站
2024-02-24 10:07:25
1.单用一个jsp文件测试sybase jconnect-5_2 jdbc数据库接口: <%@ page contenttype="text/html;charset...
1.单用一个jsp文件测试sybase jconnect-5_2 jdbc数据库接口:
2.再用jsp和java bean的方法:
3.利用jdbc(sybae jconnect-5_2)查询sybase asa7.0中数据的图形化java程序范例:
//// 一个简单的利用jdbc(sybae jconnect-5_2)查询sybase asa7.0中数据的图形化java程序范例// 执行的sql语句是" select * from employee " ,可以改成自己所需的.// 运行方式为: c:\> java jdbctest //import java.awt.*; import java.sql.*; // 在使用jdbc之前,必须引入java的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("数据库查询中,请等待......\n");}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("\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 {//设定查询字串string querystring = "select * from employee";jdbctest myjdbctest = new jdbctest();//加载驱动程序class.forname("com.sybase.jdbc2.jdbc.sybdriver").newinstance();//建立连接,localhost为主机名,dba为用户名,sql为密码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();}}
<%@ 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)查询sybase asa7.0中数据的图形化java程序范例: