Jdbc连Sybase数据库的几种方法_JSP技巧_黑客防线网安服务器维护基地--Powered by WWW.RONGSEN.COM.CN

Jdbc连Sybase数据库的几种方法

作者:黑客防线网安JSP教程基地 来源:黑客防线网安JSP教程基地 浏览次数:0

本篇关键词:方法数据库String%>
黑客防线网安网讯:1.单用一个JSP文件测试SYBASE jconnect-5_2 JDBC数据库接口:   <%@ page contentType="text/html;charset=GB2312" %><%@ page import="java.sql.*" %><HTML><HEAD><TITLE> JSP测试SYBASE...
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.再用JSPJava 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程序范例:
  

//
//   一个简单的利用JDBC(SYBAE jconnect-5_2)查询Sybase ASA7.0中数据的图形化Java程序范例
//    执行的SQL语句是" select * from employee "  ,可以改成自己所需的.
//    运行方式为: c:> java JDBCTest  
//

import java.awt.*;      
import java.sql.*;   // 在使用JDBC之前必须引入JAVASQLclass 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("	");
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为主机名,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();
}
}

		  

  <!--内容结束//-->
    黑客防线网安服务器维护方案本篇连接:http://www.rongsen.com.cn/show-16451-1.html
网站维护教程更新时间:2012-04-07 00:22:34  【打印此页】  【关闭
我要申请本站N点 | 黑客防线官网 |  
专业服务器维护及网站维护手工安全搭建环境,网站安全加固服务。黑客防线网安服务器维护基地招商进行中!QQ:29769479

footer  footer  footer  footer