無為

          無為則可為,無為則至深!

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            190 Posts :: 291 Stories :: 258 Comments :: 0 Trackbacks

          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 = (ConnectionPoolDataSourceinitCtx.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 resultSetthrows 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"));
            }

          }

                     
                 


          凡是有該標志的文章,都是該blog博主Caoer(草兒)原創,凡是索引、收藏
          、轉載請注明來處和原文作者。非常感謝。

          posted on 2007-09-28 14:10 草兒 閱讀(1336) 評論(3)  編輯  收藏 所屬分類: java

          Feedback

          # re: DB連接池管理編程舉例 2007-09-28 15:00 千里冰封
          多點中文說明會更好一些  回復  更多評論
            

          # re: DB連接池管理編程舉例 2007-09-28 18:30 beijixuem
          有點暈呀 請博主以后多寫一點注釋  回復  更多評論
            

          # re: DB連接池管理編程舉例 2007-10-20 20:20 Qsam
          實際開發中 的數據源是在配制文件寫的吧。。就是不需要createContext() 方法。請指點。  回復  更多評論
            

          主站蜘蛛池模板: 喀喇沁旗| 闵行区| 伽师县| 平昌县| 景宁| 雅江县| 望城县| 新绛县| 清新县| 普陀区| 安徽省| 咸丰县| 哈巴河县| 古丈县| 资阳市| 榕江县| 桃源县| 崇州市| 天等县| 封开县| 建水县| 樟树市| 清流县| 冀州市| 东乡| 盐池县| 德保县| 无为县| 上虞市| 扶风县| 鄢陵县| 邢台市| 广安市| 巴林右旗| 佛冈县| 贵定县| 乌苏市| 海兴县| 浦城县| 伊川县| 信阳市|