java实现数据库连接的工具类

2020-09-03 10:11:45 浏览数 (1)

第一种 (带事务)

代码语言:javascript复制
package com.china.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


public class BaseDao {

    protected Connection conn = null;
    
    
    /**
     * 创建数据库连接
     * @throws ClassNotFoundException
     * @throws SQLException
     */
    protected void openconnection() throws ClassNotFoundException,SQLException{        
        try {
            if( conn == null || conn.isClosed() ){
                Class.forName("oracle.jdbc.driver.OracleDriver");
                conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","xx","xx");
                //mysql
                 //Class.forName("com.mysql.jdbc.Driver");
                 // conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/xe", "root", "root");
            }            
        } catch (ClassNotFoundException e) {            
        
            throw e;
        } catch (SQLException e) {    
            
            throw e;
        }
    }    
    
    
    /**
     * 手动提交事务
     * @throws Exception
     */
    public void beginTransaction() throws Exception{
    
        this.openconnection();        
        conn.setAutoCommit(false);        
    }
    /**
     * 事务回滚
     * @throws Exception
     */
    public void rollback() throws Exception{
        
        if(conn != null){            
                conn.rollback();            
        }        
    }
    /**
     * 事务提交
     * @throws Exception
     */
    public void commit() throws Exception{
        if(conn != null){            
            conn.commit();                
        }    
    }

    /**
     * 关闭数据库
     * @throws Exception
     */
    public void closeResource() throws Exception{        
        if(conn != null){
            conn.close();
        }            
        
    }
    
        
    
    
}

第二种(mysql和oracle无事务)推荐!

代码语言:javascript复制
package com.smn.toolsbean;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DB {
    private final String url = "jdbc:mysql://127.0.0.1:3306/blog";
    private final String userName = "root";
    private final String password = "toor";
    private Connection con = null;
    private Statement stm=null;
    
    /* 通过构造方法加载数据库驱动 */
    public DB(){
        try {
            Class.forName("com.mysql.jdbc.Driver"); 
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("加载数据库驱动失败!");
        }        
    }
    /* 创建数据库连接 */
    public void createCon() {
        try {
            con = DriverManager.getConnection(url, userName, password);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("获取数据库连接失败!");
        }
    }
    /* 获取Statement对象 */
    public void getStm(){
           createCon();
        try {
            stm=con.createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("创建Statement对象失败!");
        }
    }
    /** 
     * @功能 对数据库的增加、修改和删除的操作
     * @参数 sql为要执行的SQL语句
     * @返回值 boolean型值 
     */
    public boolean executeUpdate(String sql) {
        System.out.println(sql);
        boolean mark=false;
        try {
            getStm();
            int iCount = stm.executeUpdate(sql);
            if(iCount>0)                
                mark=true;            
            else
                mark=false;
        } catch (Exception e) {
            e.printStackTrace();
            mark=false;
        }
        return mark;
    }
    /* 查询数据库 */
    public ResultSet executeQuery(String sql) {
        ResultSet rs=null;
        try {
            getStm();
            try {
                rs = stm.executeQuery(sql);
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("查询数据库失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();          
        }
        return rs;
    }
    /* 关闭数据库的操作 */
    public void closed() {
        if(stm!=null)
            try {
                stm.close();
            } catch (SQLException e) {
                e.printStackTrace();
                System.out.println("关闭stm对象失败!");
            }
        if(con!=null)
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
                System.out.println("关闭con对象失败!");
            }
    }
}

0 人点赞