java學習

          java學習

           

          jdbc

          public class DBHelp<T> {
           
           private static String driver;
           private static String url;
           private static String username;
           private static String password;
           private static BasicDataSource dataSource;
           
           static{
            Properties properties = new Properties();
            
            try {
             //讀取到src目錄中存放的db.properties配置文件
             properties.load(DBHelp.class.getClassLoader().getResourceAsStream("db.properties"));
             driver = properties.getProperty("driver");
             url = properties.getProperty("url");
             username = properties.getProperty("username");
             password = properties.getProperty("password");
            } catch (IOException e) {
             e.printStackTrace();
            }
            
            
            dataSource = new BasicDataSource();
            
            dataSource.setDriverClassName(driver);
            dataSource.setUrl(url);
            dataSource.setUsername(username);
            dataSource.setPassword(password);
            dataSource.setInitialSize(5);
            dataSource.setMaxWait(5000);
            dataSource.setMaxActive(20);
            dataSource.setMinIdle(10);
            
           }
           public Connection getConnection(){
            try {
             Connection conn = dataSource.getConnection();
             
             return conn;
            } catch (SQLException e) {
             e.printStackTrace();
            }
            return null;
           }
           public List<T> executeForList(RowMapper<T> rowMapper,String sql) {
            Connection conn = null;
            PreparedStatement stat = null;
            ResultSet rs = null;
            List<T> list = new ArrayList<T>();
            
            try {
             conn = getConnection();
             stat = conn.prepareStatement(sql);
             rs = stat.executeQuery();
             while(rs.next()) {
              list.add(rowMapper.mapperRow(rs));
             }
             System.out.println("SQL"+sql);
            } catch (SQLException e) {
             e.printStackTrace();
            }finally{
             close(rs, stat, conn);
            }
            return list;
           }
           
           public List<T> executeForList(String sql,RowMapper<T> rowMapper,Object... args) {
            Connection conn = null;
            PreparedStatement stat = null;
            ResultSet rs = null;
            List<T> list = new ArrayList<T>();
            
            try {
             conn = getConnection();
             stat = conn.prepareStatement(sql);
             for (int i = 0; i < args.length; i++) {
              stat.setObject(i+1,args[i]);
             }
             rs = stat.executeQuery();
             while(rs.next()) {
              list.add(rowMapper.mapperRow(rs));
             }
             System.out.println("SQL"+sql);
            } catch (SQLException e) {
             e.printStackTrace();
            }finally{
             close(rs, stat, conn);
            }
            return list;
           }
           
           public T executeForObject(String sql,RowMapper<T> rowMapper,Object... args) {
            Connection conn = null;
            PreparedStatement stat = null;
            ResultSet rs = null;
            T obj = null;
            
            try {
             conn = getConnection();
             stat = conn.prepareStatement(sql);
             for (int i = 0; i < args.length; i++) {
              stat.setObject(i+1, args[i]);
             }
             rs = stat.executeQuery();
             if(rs.next()) {
              obj = rowMapper.mapperRow(rs);
             }
             System.out.println("SQL"+sql);
            } catch (SQLException e) {
             e.printStackTrace();
            }finally{
             close(rs, stat, conn);
            }
            return obj;
            
           }
           public int executeForCount(String sql,Object... args){
            Connection conn = null;
            PreparedStatement stat = null;
            ResultSet rs = null;
            int count = 0;
            
            try {
             conn = getConnection();
             stat = conn.prepareStatement(sql);
             rs = stat.executeQuery();
             
             for(int i = 0;i < args.length;i++){
              stat.setObject(i+1, args[i]);
             }
             if(rs.next()) {
              count = rs.getInt(1);
             }
             System.out.println("SQL"+sql);
            } catch (SQLException e) {
             e.printStackTrace();
            }finally{
             close(rs, stat, conn);
            }
            return count;
           }
           
           public void executeUpdate(String sql,Object... args) {
            Connection conn = null;
            PreparedStatement stat = null;
            
            
            try {
             conn = getConnection();
             stat = conn.prepareStatement(sql);
             
             for(int i = 0;i < args.length;i++){
              stat.setObject(i+1, args[i]);
             }
             
             stat.executeUpdate();
             System.out.println("SQL"+sql);
            } catch (SQLException e) {
             e.printStackTrace();
            }finally{
             close(stat,conn);
            }
            
           }
           
           public void close(PreparedStatement stat,Connection conn) {
            close(null,stat,conn);
           }
           public void close(ResultSet rs,PreparedStatement stat,Connection conn) {
            try {
             if(rs != null){
              rs.close();
             }
            } catch (SQLException e) {
             e.printStackTrace();
            }finally{
             try {
              if(stat != null) {
               stat.close();
              }
             } catch (SQLException e) {
              e.printStackTrace();
             }finally{
              try {
               if(conn != null) {
                conn.close();
               }
              } catch (SQLException e) {
               e.printStackTrace();
              }
             }
            }
           }
           
           
          }

          public interface RowMapper<T> {
           public T mapperRow(ResultSet rs) throws SQLException;
          }

          public class TourDao {
           private DBHelp<Tour> db = new DBHelp<Tour>();
           public Tour findByName(String name){
            String sql = "SELECT id,tourname FROM t_tour WHERE tourname=?";
            Tour t = db.executeForObject(sql, new TourRowMapper(), name);
            return t;
           }
           public void insertSale(String name){
            String sql = "INSERT INTO t_tour (tourname) VALUE(?)";
            db.executeUpdate(sql, name);
           }
           public class TourRowMapper implements RowMapper<Tour>{

            public Tour mapperRow(ResultSet rs) throws SQLException {
             Tour t = new Tour();
             t.setId(rs.getInt("id"));
             t.setTourname(rs.getString("tourname"));
             
             return t;
            }
            
           }
          }

          posted on 2013-05-28 19:53 楊軍威 閱讀(192) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 吕梁市| 张家港市| 甘孜县| 乐至县| 富顺县| 桐乡市| 双流县| 和静县| 乳源| 巨野县| 昌图县| 福建省| 大冶市| 兴安县| 沅陵县| 田阳县| 苍溪县| 涪陵区| 安丘市| 夏津县| 孝感市| 龙岩市| 沈阳市| 乐都县| 聊城市| 卓资县| 江口县| 五原县| 夏河县| 渑池县| 黑山县| 连州市| 宜兰县| 鄢陵县| 山阳县| 平定县| 铜山县| 天台县| 夏津县| 武陟县| 沅江市|