?? 0190.htm
字號:
<html>
<head>
<title>新時代軟件教程:操作系統 主頁制作 服務器 設計軟件 網絡技術 編程語言 文字編輯</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style>
<!--
body, table {font-size: 9pt; font-family: 宋體}
a {text-decoration:none}
a:hover {color: red;text-decoration:underline}
.1 {background-color: rgb(245,245,245)}
-->
</style>
</head>
<p align="center"><script src="../../1.js"></script></a>
<p align="center"><big><strong>jdbc-odbc連接的bean代碼</strong></big></p>
<div align="right">---摘自互聯網</div>
<br>// substitute your own class path<br>
package com.jspcafe.db;<br>
<br>
// standard classes to import when using jdbc<br>
import java.sql.*;<br>
import java.io.*;<br>
<br>
public class odbcBean {<br>
<br>
// db is our connection object<br>
Connection db = null;<br>
<br>
// stmt will hold our jdbc statements<br>
// i.e. sql and stored procedures<br>
Statement stmt = null;<br>
<br>
// result will hold the recordset<br>
ResultSet result = null;<br>
<br>
// default constructor<br>
public odbcBean() {<br>
}<br>
<br>
/*********************************************<br>
* @dsn String for ODBC Datasource name<br>
* @uid String for ODBC Datasource user name<br>
* @pwn String for ODBC Datasource password<br>
*/<br>
public void OpenConn(<br>
String dsn, <br>
String uid, <br>
String pwd) throws Exception {<br>
<br>
try {<br>
dsn = "jdbc:odbc:" + dsn;<br>
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");<br>
db = DriverManager.getConnection(dsn, uid, pwd);<br>
} catch (Exception e) {<br>
e.printStackTrace();<br>
}<br>
}<br>
<br>
<br>
/*********************************************<br>
* @sproc String for sql statement or stored<br>
* procedure<br>
*/<br>
public ResultSet getResults(String sproc) <br>
throws Exception {<br>
<br>
stmt = db.createStatement();<br>
result = stmt.executeQuery(sproc);<br>
return result;<br>
}<br>
<br>
/*********************************************<br>
* @sproc String for sql statement or stored<br>
* procedure<br>
*/<br>
public void execute(String sproc) <br>
throws Exception {<br>
<br>
stmt = db.createStatement();<br>
stmt.execute(sproc);<br>
}<br>
<br>
// Don't forget to clean up!<br>
public void CloseStmt() <br>
throws Exception {<br>
<br>
stmt.close();<br>
stmt = null;<br>
}<br>
<br>
// Don't forget to clean up!<br>
public void CloseConn() <br>
throws Exception {<br>
<br>
db.close();<br>
db = null;<br>
}<br>
<br>
}<br>
<br>
OK, that's great, now what do I do with it? How the heck do I use this stupid bean in my JSP page? Like this: <br>
<br>
<br>
<%@ page import="java.sql.*" %><br>
<br>
<html><br>
<body><br>
<br>
<jsp:useBean id="db" <br>
class="com.jspcafe.db.OdbcBean" <br>
scope="request" /><br>
<br>
<%<br>
// Open your database connection<br>
db.OpenConn("myDsn", "myUsername", "myPassword");<br>
<br>
// Create your sql statement<br>
String sql = "MY SQL STATEMENT";<br>
<br>
// Get your recordset<br>
ResultSet rs = db.getResults(sql);<br>
<br>
/*******************************************<br>
* Do whatever you need to do with your<br>
* recordset.<br>
*/<br>
<br>
// Clean up!<br>
rs = null;<br>
db.CloseStmt();<br>
db.CloseConn();<br>
%><br>
<br>
</body><br>
</html>
</table>
<p align="center"><script src="../../2.js"></script></a>
</body>
</html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -