頁:
[1]
2
JSP 如何呼叫 JavaBean 連結 MSSQL
在 WEB 實務 應用上是透過 jTDS (可從網站上免費下載) 之 Class 元件, 應用 JSP 如何呼叫 JavaBean 連結 MSSQL應用 JSP 如何呼叫 JavaBean 連結 MSSQL伊莉討論區伊莉討論區經由 JavaBean (實際例子如下)呼叫 MS SQL Server。提供有需要實務應作的各位分享:
// ------------------------
// ConnectionBean.java
// ------------------------
package com.taglib ;
import java.sql.*;
import javax.sql.*;
import javax.servlet.http.*;
public class ConnectionBean implements HttpSessionBindingListener {
private Connection connection;
private Statement statement;
private CallableStatement cstatement;
private PreparedStatement pstatement;
private static String alias = "npccart" ;
private static String driver="net.sourceforge.jtds.jdbc.Driver";
private static String login="your login name";
private static String password="your login password";
private static String url = "proxool." + alias + ":" + driver + ":" + dbURL ;
public ConnectionBean() {
openConnection() ;
}
public ConnectionBean(String driver, String url, String username,
String password) {
this.driver = driver ;
this.dbURL = url ;
this.login = username ;
this.password = password ;
openConnection() ;
}
private void openConnection()
{
java.util.Properties info = new java.util.Properties() ;
info.setProperty("proxool.maximum-connection-count", "30");
info.setProperty("user", login);
info.setProperty("password", password);
try {
Class.forName( "org.logicalcobwebs.proxool.ProxoolDriver" );
connection = DriverManager.getConnection( url, info );
statement = connection.createStatement();
}
catch (ClassNotFoundException e) {
System.err.println("ConnectionBean: driver unavailable");
connection = null;
}
catch (SQLException e) {
System.err.println("ConnectionBean: driver not loaded");
connection = null;
}
finally {
try {
// Check to see we actually got a connection before we
// attempt to close it.
if ( statement != null )
statement.close() ;
if ( connection != null )
connection.close() ;
}
catch (SQLException es) {
System.err.println("ConnectionBean: driver not loaded");
connection = null;
}
}
}
public Connection getConnection() {
return connection;
}
public void setPrepare(String sql) throws SQLException {
cstatement = null ;
try {
if (connection == null || connection.isClosed()) {
connection = DriverManager.getConnection( "proxool." + alias );
}
}
catch (SQLException e) { connection = null; }
pstatement = connection.prepareStatement( sql ) ;
}
public void callProc(String sql) throws SQLException {
pstatement = null ;
try {
if (connection == null || connection.isClosed()) {
connection = DriverManager.getConnection( "proxool." + alias );
}
}
catch (SQLException e) { connection = null; }
cstatement = connection.prepareCall( sql ) ;
}
public void setInt(int nos, int istr) throws SQLException {
if ( pstatement == null )
cstatement.setInt( nos, istr ) ;
else
pstatement.setInt( nos, istr ) ;
}
public void setString(int nos, String str) throws SQLException {
if ( pstatement == null )
cstatement.setString( nos, str ) ;
else
pstatement.setString( nos, str ) ;
}
public String getString(int nos) throws SQLException {
return cstatement.getString( nos ) ;
}
public float getFloat(int nos) throws SQLException {
return cstatement.getFloat( nos ) ;
}
public double getDouble(int nos) throws SQLException {
return cstatement.getDouble( nos ) ;
}
public void setRegister(int nos, int code) throws SQLException {
cstatement.registerOutParameter( nos, code ) ;
}
public void commit() throws SQLException {
connection.commit();
}
public void rollback() throws SQLException {
connection.rollback();
}
public void setAutoCommit(boolean autoCommit)
throws SQLException {
if (connection == null || connection.isClosed()) {
connection = DriverManager.getConnection( "proxool." + alias );
statement = connection.createStatement();
}
connection.setAutoCommit(autoCommit);
}
public ResultSet executeQuery(String sql) throws SQLException {
try {
if (connection == null || connection.isClosed()) {
connection = DriverManager.getConnection( "proxool." + alias );
statement = connection.createStatement();
}
}
catch (SQLException e) { connection = null; }
return statement.executeQuery(sql);
}
public ResultSet executeQuery() throws SQLException {
if ( pstatement == null )
return cstatement.executeQuery();
else
return pstatement.executeQuery();
}
// CREATE TABLE table_name ( column_name data_type, ... ) ;
// DROP TABLE table_name ;
// ALTER TABLE table_name ADD column_name data_type ;
// INSERT INTO table_name column_name,... VALUES(...,...) ;
public int executeUpdate(String sql) throws SQLException {
try {
if (connection == null || connection.isClosed()) {
connection = DriverManager.getConnection( "proxool." + alias );
statement = connection.createStatement();
}
}
catch (SQLException e) { connection = null; }
return statement.executeUpdate(sql);
}
public int executeUpdate() throws SQLException {
if ( pstatement == null )
return cstatement.executeUpdate();
else
return pstatement.executeUpdate();
}
public void valueBound(HttpSessionBindingEvent event) {
System.err.println("ConnectionBean: in the valueBound method");
try {
if (connection == null || connection.isClosed()) {
connection = DriverManager.getConnection( "proxool." + alias );
statement = connection.createStatement();
}
}
catch (SQLException e) { connection = null; }
}
public void valueUnbound(HttpSessionBindingEvent event) {
try {
if ( pstatement != null )
pstatement.close() ;
if ( cstatement != null )
cstatement.close() ;
if ( statement != null )
statement.close() ;
if ( !connection.isClosed() )
connection.close();
}
catch (SQLException e) { }
finally {
connection = null;
}
}
public void close()
{
try {
if ( pstatement != null )
pstatement.close() ;
if ( cstatement != null )
cstatement.close() ;
if ( statement != null )
statement.close() ;
if ( !connection.isClosed() )
connection.close();
}
catch (SQLException e) { }
finally {
connection = null;
}
}
public boolean isClosed() throws SQLException
{
return connection.isClosed() ;
}
protected void finalize() {
try {
if ( pstatement != null )
pstatement.close() ;
if ( cstatement != null )
cstatement.close() ;
if ( statement != null )
statement.close() ;
if ( !connection.isClosed() )
connection.close();
}
catch (SQLException e) { }
}
} 應用 JSP 如何呼叫 JavaBean 連結 MSSQL應用 JSP 如何呼叫 JavaBean 連結 MSSQL伊莉討論區伊莉討論區...<div class='locked'><em>瀏覽完整內容,請先 <a href='member.php?mod=register'>註冊</a> 或 <a href='javascript:;' onclick="lsSubmit()">登入會員</a></em></div><div></div> 這是啥東東!!!!!!!!!! 這是啥東東!!!!!!!!!! 這是啥東東!!!!!!!!!! 這是啥東東!!!!!!!!!!<br><br><br><br><br><div></div> 這是啥東東!!!!!!!!!! 這是啥東東!!!!!!!!!! 這是啥東東!!!!!!!!!! 這是啥東東!!!!!!!!!! 這是啥東東!!!!!!!!!!<br><br><br><br><br><div></div> 這是啥東東!!!!!!!!!! 這是啥東東!!!!!!!!!! 這是啥東東!!!!!!!!!! 這是啥東東!!!!!!!!!! 這是啥東東!!!!!!!!!!<br><br><br><br><br><div></div>
頁:
[1]
2