dbbridge.java

来自「用java实现的一个bbs的portal」· Java 代码 · 共 94 行

JAVA
94
字号
/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: Jul 18, 2003
 * Time: 3:47:17 PM
 * To change this template use Options | File Templates.
 */
package DBConnection;

import java.sql.*;

public class DBBridge {

    private ConnPool  connPool = null ;
    private Connection  conn = null;
    private Statement  stmt = null;

    public DBBridge(){
        try{
            setConnectionSwitch("on");
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }

    public void closeDB() throws SQLException {
        if( connPool!=null ) {
            connPool.returnConnection() ;
            connPool=null ;
        }else{
            System.err.println("The Connection has not been established !");
        }
        stmt=null ;
        conn=null ;
    }

    public void openDB() throws SQLException {
        connPool=ConnPool.getInstance() ;
        conn=connPool.getConnection() ;
        //stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
        stmt=conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
    }


    public void setConnectionSwitch( String on_off ) {
        try {
            if( on_off.equalsIgnoreCase( "ON" ) )
                openDB() ;
            else if( on_off.equalsIgnoreCase( "OFF" ) )
                closeDB() ;
        }
        catch( SQLException ex ) {
            ex.printStackTrace();
        }
    }

    public Statement getStmt() {
        return stmt;
    }

    public static void main(String args[]){

    }

}

/*
public boolean openConnector(){
if (conn != null)
return true;
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://162.105.203.35:1433;DatabaseName=BBS";
String user="sa";
String password="bbs";
conn= DriverManager.getConnection(url,user,password);
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
}catch(Exception ex){
ex.printStackTrace();
return false;
}
return true;
}

public void closeConnector(){
try{
stmt.close();
conn.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
*/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?