小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

DB連接池管理編程舉例

 最強火槍手 2009-02-20
DB連接池管理編程舉例 import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.ConnectionPoolDataSource; import javax.sql.PooledConnection; public class MainClass { public static void main(String[] args) { Connection connection = null; Statement statement = null; ResultSet resultSet = null; try { connection = getConnection(); // 操作連接 statement = connection.createStatement(); String selectEmployeesSQL = "SELECT * FROM employees"; resultSet = statement.executeQuery(selectEmployeesSQL); while (resultSet.next()) { printEmployee(resultSet); } } catch (Exception e) { e.printStackTrace(); } finally { if (resultSet != null) { try { resultSet.close(); } catch (SQLException e) { } // nothing we can do } if (statement != null) { try { statement.close(); } catch (SQLException e) { } // nothing we can do } if (connection != null) { try { connection.close(); } catch (SQLException e) { } // nothing we can do } } } private static Connection getConnection() throws NamingException, SQLException { InitialContext initCtx = createContext(); String jndiName = "HrDS"; ConnectionPoolDataSource dataSource = (ConnectionPoolDataSource) initCtx.lookup(jndiName); PooledConnection pooledConnection = dataSource.getPooledConnection(); return pooledConnection.getConnection(); // 從池中得到連接 } private static InitialContext createContext() throws NamingException { Properties env = new Properties(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory"); env.put(Context.PROVIDER_URL, "rmi://localhost:1099"); InitialContext context = new InitialContext(env); return context; } private static void printEmployee(ResultSet resultSet) throws SQLException { System.out.print(resultSet.getInt("employee_id")+", "); System.out.print(resultSet.getString("last_name")+", "); System.out.print(resultSet.getString("first_name")+", "); System.out.println(resultSet.getString("email")); } } 凡是有該標(biāo)志的文章,都是該blog博主Caoer(草兒)原創(chuàng),凡是索引、收藏 、轉(zhuǎn)載請注明來處和原文作者。非常感謝。

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多