zoukankan      html  css  js  c++  java
  • Java c3po

    1、准备通用类

    (引用:c3p0-0.9.1.2.jar)

    package nankang.test;
    
    import java.sql.Connection;
    
    import com.mchange.v2.c3p0.ComboPooledDataSource;
    
    public class ConnectionFactory {
    
        private static ComboPooledDataSource ds = null;
    
        static {
            try {
                String driver = "oracle.jdbc.driver.OracleDriver";
                String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
                String user = "phonesurvey";
                String password = "world";
    
                ds = new ComboPooledDataSource();
                ds.setDriverClass(driver);
                ds.setJdbcUrl(url);
                ds.setUser(user);
                ds.setPassword(password);
    
                ds.setMaxPoolSize(200);
                ds.setMinPoolSize(20);
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
        public static synchronized Connection getConnection(){
            Connection conn = null;
            try{
                conn = ds.getConnection();
            }catch(Exception e){
                e.printStackTrace();
            }
            return conn;
        }
        
        
    }

    2、调用

    package nankang.test;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    
    public class Main {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
    
            Connection conn = null;
            PreparedStatement pstm = null;
            ResultSet rs = null;
    
            try {
                conn = ConnectionFactory.getConnection();
    
                String sql = "select * from Agent";
                pstm = conn.prepareStatement(sql);
                rs = pstm.executeQuery();
                while (rs.next()) {
                    String agentId = rs.getString("AgentId");
    
                    System.out.println(agentId);
                }
    
                System.out.println("成功");
            } catch (Exception e) {
                System.out.println(String.format("失败,%s", e.getMessage()));
            } finally {
                if (rs != null) {
                    try {
                        rs.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                if (pstm != null) {
                    try {
                        pstm.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
    }
  • 相关阅读:
    20170809--JS操作Select备忘
    20160711--C# 委托的三种调用示例(同步调用 异步调用 异步回调)【转载】
    C# 内存建表备忘
    富文本编辑器 CKeditor 配置使用
    20160520—JS打分控件
    20160513--js 弹出窗口带有iframe控件 备忘
    chart 简单应用
    mvc 简单整理
    ObjectDatasourse 的绑定及显示
    GridView 详述
  • 原文地址:https://www.cnblogs.com/sshoub/p/4085594.html
Copyright © 2011-2022 走看看