• JDBC


    java_JDBC

    public abstract class DBCP_Utiles<T> {
        /**
         * 带有配置文件的DBCP的使用:
         * <p>
         * 核心类:BasicDataSourceFactory(工厂)
         * public static DataSource createDataSource(new FileInputStream("xxx.properties"));
         */
    
    //	private static BasicDataSource ds=new BasicDataSource();
    
        private static BasicDataSource ds = null;
    
        static {
            Properties ps = new Properties();
            try {
                //直接获取流的方式:工程根目录
    //			ps.load(new FileInputStream("dbcp.properties"));
                //类加载器获取配置文件:src目录
                ps.load(DBCP_Utiles01.class.getClassLoader().getResourceAsStream("dbcp.properties"));
    
                ds = (BasicDataSource) BasicDataSourceFactory.createDataSource(ps);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                throw new RuntimeException("加载错误");
            }
        }
    
        public static Connection getConnection() throws Exception {
            return ds.getConnection();
        }
    
        public static void closePoor(Connection conn, Statement st, ResultSet rs) {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    System.out.println(e);
                }
            }
            if (st != null) {
                try {
                    st.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    System.out.println(e);
                }
            }
            if (st != null) {
                try {
                    st.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    System.out.println(e);
                }
            }
        }
        //sql增删改
        public int exec_update(String sql, Object[] params) throws Exception {
            Connection connection = getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                preparedStatement.setObject((i + 1), params[i]);
            }
            int num = preparedStatement.executeUpdate();
            return num;
        }
        //sql查询
        public ArrayList<T> exec_query(String sql, Object[] params) throws Exception {
            ArrayList<T> list = new ArrayList();
            Connection connection = getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            if (params != null) {
                for (int i = 0; i < params.length; i++) {
                    preparedStatement.setObject((i + 1), params[i]);
                }
            }
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                T t = this.getBean(resultSet);
                list.add(t);
            }
            return list;
        }
    
        //获取bean
        public abstract T getBean(ResultSet rs) throws SQLException;
    }
    
  • 相关阅读:
    五个Metro UI 风格的网页设计
    cocos2dx学习资料(转)
    Python性能鸡汤
    7款很棒的 HTML5 视频播放器
    优秀的后台管理界面设计案例分享
    Task的理解(转)
    超棒的20款javascript工具提示条(tooltips)类库(转)
    Xcode 证书生成、设置、应用(转)
    分享17个漂亮的电子商务网站(转)
    强烈免费25款商务logo设计模板(转)
  • 原文地址:https://www.cnblogs.com/2393920029-qq/p/15330686.html
Copyright © 2020-2023  润新知