Mysql连接数据库封装类

来源:互联网 发布:重庆办公软件培训班 编辑:程序博客网 时间:2024/04/30 03:43
  1. package com.util;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.DriverManager;  
  5. import java.sql.PreparedStatement;  
  6. import java.sql.ResultSet;  
  7. import java.sql.SQLException;  
  8. import java.sql.Statement;  
  9.   
  10.   
  11. /** 
  12.  * 封装数据常用操作 
  13.  *  
  14.  * @author 刘鹏 
  15.  *  
  16.  */  
  17. public class DbUtil {  
  18.     /** 
  19.      * 取得Connection 
  20.      *  
  21.      * @return 
  22.      */  
  23.     public static final String DBDRIVER="com.mysql.jdbc.Driver";  
  24.     public static final String DBURL="jdbc:mysql://localhost:3306/lp";  
  25.     public static final String DBUSER="root";  
  26.     public static final String DBPASS="mysqladmin";  
  27.       
  28.       
  29.     public static Connection conn = null;  
  30.     public static Statement pstm = null;  
  31.     public static PreparedStatement pstmt = null;  
  32.       
  33.       
  34.       
  35.     /** 
  36.      * 取得数据库连接 
  37.      * @return 
  38.      */  
  39.     public static Connection getConnection(){  
  40.           
  41.   
  42.         try{  
  43.             Class.forName(DBDRIVER);  
  44.               
  45.         }catch(ClassNotFoundException e){  
  46.             e.printStackTrace();  
  47.         }  
  48.         try{  
  49.             conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS);  
  50.         }catch(SQLException e){  
  51.             e.printStackTrace();  
  52.         }  
  53.         return conn;  
  54.     }  
  55.       
  56.       
  57.       
  58.       
  59.     /** 
  60.      * 数据库操作 
  61.      * @return 
  62.      */  
  63.     public static Statement getStatement(Connection con){  
  64.         //con = DbUtil.getConnection();  
  65.         try {  
  66.             if (con != null) {  
  67.                 pstm = con.createStatement();  
  68.             }  
  69.         } catch (SQLException e) {  
  70.             // TODO Auto-generated catch block  
  71.             e.printStackTrace();  
  72.         }  
  73.         return pstm;  
  74.     }  
  75.   
  76.       
  77.       
  78.     /** 
  79.      * 数据库操作 
  80.      * @return 
  81.      */  
  82.     public static PreparedStatement getPstmt(Connection con,String sql) {  
  83.         //con = DbUtil.getConnection();  
  84.         if (con != null) {  
  85.             try {  
  86.                 pstmt = conn.prepareStatement(sql);  
  87.             } catch (SQLException e) {  
  88.                   
  89.                 e.printStackTrace();  
  90.             }  
  91.         }  
  92.         return pstmt;  
  93.     }  
  94.       
  95.   
  96.   
  97.         /** 
  98.      * 查询操作,基于PreparedStatement接口 
  99.      * @param conn 
  100.      * @param sql 
  101.      * @return 
  102.      */  
  103.     public static ResultSet getResultSet(Connection conn,String sql){  
  104.         pstmt = DbUtil.getPreparedStatement(conn, sql);  
  105.         try {  
  106.             rs = pstmt.executeQuery();  
  107.         } catch (SQLException e) {  
  108.               
  109.             e.printStackTrace();  
  110.         }  
  111.         return rs;  
  112.     }  
  113.   
  114.   
  115.     /** 
  116.      * 关闭数据库连接 
  117.      * @param conn 
  118.      */  
  119.     public static void close(Connection conn){  
  120.         if(conn!=null){  
  121.             try{  
  122.                 conn.close();  
  123.             }catch(SQLException e){  
  124.                 e.printStackTrace();  
  125.             }  
  126.         }  
  127.     }  
  128.       
  129.       
  130.     /** 
  131.      * 关闭数据库操作Statement 
  132.      * @param stmt 
  133.      */  
  134.     public static void close(Statement stmt){  
  135.         if(stmt!=null){  
  136.             try{  
  137.                 stmt.close();  
  138.             }catch(SQLException e){  
  139.                 e.printStackTrace();  
  140.                 }  
  141.         }  
  142.     }  
  143.       
  144.       
  145.     /** 
  146.      * 关闭数据库操作PreparedStatement 
  147.      * @param pstmt 
  148.      */  
  149.     public static void close(PreparedStatement pstmt){  
  150.         if(pstmt!=null){  
  151.             try{  
  152.                 pstmt.close();  
  153.             }catch(SQLException e){  
  154.                 e.printStackTrace();  
  155.             }  
  156.         }  
  157.     }  
  158.       
  159.       
  160.       
  161.     /** 
  162.      * 关闭数据库查询 
  163.      * @param rs 
  164.      */  
  165.     public static void close(ResultSet rs){  
  166.         if(rs!=null){  
  167.             try{  
  168.                 rs.close();  
  169.             }catch(SQLException e){  
  170.                 e.printStackTrace();  
  171.             }  
  172.         }  
  173.     }  
  174.       
  175.       
  176.     /** 
  177.      * 主方法用于测试 
  178.      * @param args 
  179.      */  
  180.     public static void main(String[] args) {  
  181.         // TODO Auto-generated method stub  
  182.         System.out.println(DbUtil.getConnection());  
  183.     }  
  184.   

0 0
原创粉丝点击