?? connectionmanager.java
字號:
//package com.redfox.sql;/** * <p>Title: ConnectionManager</p> * <p>Description: ConnectionManager</p> * <p>Copyright: Copyright (c) 2004</p> * <p>Company: Powerise. Chao.Liu</p> * @author not attributable * @version 1.0 */import java.sql.Connection;import java.sql.SQLException;import java.sql.Statement;import java.sql.ResultSet;import java.sql.SQLException;import javax.sql.PooledConnection;import oracle.jdbc.pool.*;public class ConnectionManager { private PooledConnection pooledConnection = null; private OracleConnectionPoolDataSource poolDataSource = null; private static ConnectionManager connectionManager = null; private ConnectionManager(String url, String userName, String password) throws SQLException { poolDataSource = new OracleConnectionPoolDataSource(); poolDataSource.setURL(url); poolDataSource.setUser(userName); poolDataSource.setPassword(password); pooledConnection = poolDataSource.getPooledConnection(); } /** * Get ConnectionManager, when the connectionManager object has been * created. or create and return. * @param url * @param userName * @param password * @return ConnectionManager * @throws SQLException */ public static ConnectionManager getConnectionManager(String url, String userName, String password) throws SQLException { if (connectionManager == null) { connectionManager = new ConnectionManager(url, userName, password); } return connectionManager; } /** * Create a new Connection Manager with new attributes. * @param url * @param userName * @param password * @return ConnectionManager * @throws SQLException */ public static ConnectionManager getNewConnectionManager(String url, String userName, String password) throws SQLException { connectionManager = new ConnectionManager(url, userName, password); return connectionManager; } /** * Get a SQL Connection * @return java.sql.Connection * @throws SQLException */ public Connection getConnection() throws SQLException { return pooledConnection.getConnection(); } public static void main(String[] args) throws SQLException { // usage: ConnectionManager connectionManager = null; Connection conn = null; try{ connectionManager = ConnectionManager.getConnectionManager( "jdbc:oracle:thin:@192.168.0.192:1521:test", "scott", "tiger"); conn = connectionManager.getConnection(); Statement state = conn.createStatement(); ResultSet result = state.executeQuery("SELECT * FROM demo"); while (result.next()) { System.out.print(result.getString(1)); System.out.print(result.getString(2)); //System.out.print(result.getString(3)); //System.out.print(result.getString(4)); //System.out.println(result.getString(5)); } result.close(); state.close(); } catch(SQLException ex){ throw ex; } finally{ try{ if(conn != null){ //曐\u8BC1connection揑\u5173\u95ED丅斲\u5219\u8FDE愙抮夛晄抐揑\u5219壛怴揑\u8FDE愙丅 conn.close(); } } catch(SQLException ex){}; } }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -