程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> DB2數據庫 >> DB2教程 >> 封裝DB

封裝DB

編輯:DB2教程

Java代碼 
package com.zm.bbs; 
 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 
 
public class DB { 
    public static Connection getConn(){ 
        Connection conn=null; 
        try { 
            Class.forName("com.mysql.jdbc.Driver"); 
        } catch (ClassNotFoundException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } 
        try { 
            conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/bbs","root","root"); 
        } catch (SQLException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } 
        return conn; 
    } 
     
    public static Statement getStmt(Connection conn){ 
        Statement stmt=null; 
        try { 
            stmt=conn.createStatement(); 
        } catch (SQLException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } 
        return stmt; 
    } 
    public static ResultSet getRs(Statement stmt,String sql){ 
        ResultSet rs=null; 
        try { 
            rs=stmt.executeQuery(sql); 
        } catch (SQLException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } 
        return rs; 
    } 
     
    public static void close(ResultSet rs){ 
        if(rs!=null){ 
            try { 
                rs.close(); 
            } catch (SQLException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
            } 
            rs=null; 
        } 
    } 
     
    public static void close(Statement stmt){ 
        if(stmt!=null){ 
            try { 
                stmt.close(); 
            } catch (SQLException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
            } 
            stmt=null; 
        } 
    } 
     
    public static void close(Connection conn){ 
        if(conn!=null){ 
            try { 
                conn.close(); 
            } catch (SQLException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
            } 
            conn=null; 
        } 
    } 
}

  作者“不做浮躁的人!!!”
 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved