我是靠谱客的博主 魔幻手机,最近开发中收集的这篇文章主要介绍Log4j与单例模式连接数据库通用类,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/*
 * @ClassName: SqlHelper
 */
public class SqlHelper {
    /**
     * 设置单例
     * 单例模式可以节省资源.
     */
    public static SqlHelper instance = new SqlHelper();
 
    /**
     * 设置单例的一个实例方法
     *
     * @return
     */
    public static SqlHelper getInstance() {
       if (instance == null) {
           //锁定当前类
           synchronized (SqlHelper.class) {
              instance = new SqlHelper();
           }
       }
       return instance;
    }
    /**
     * 得到MySql连接
     * @return
     */
    public static Connection getMySqlConnection() {
       Connection conn = null;
       String url = "jdbc:mysql://127.0.0.1:3306/ssh?useUnicode=true&characterEncoding=utf-8";
       String user = "root";
       String password = "root";
       String driver = "com.mysql.jdbc.Driver";
       try {
           Class.forName(driver);
           conn = DriverManager.getConnection(url, user,password);
       } catch (Exception e) {
           System.out.println("连接数据库出现异常" + e.getMessage());
       }
       return conn;
    }
 
    /**
     * 得到MSSQL连接
     * @return
     */
    public static Connection getMSSQLConnection() {
       String url = "jdbc:sqlserver://localhost:1433;DatabaseName=test";
       String user = "sa";
       String password = "sa";
       String Driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
       Connection conn = null;
       try {
           Class.forName(Driver);
           conn = DriverManager.getConnection(url, user,password);
       } catch (Exception e) {
           System.out.println("连接数据库出现异常" + e.getMessage());
       }
       return conn;
    }
 
    /**
     * 得到Oracle连接
     * @return
     */
    public static Connection getOracleConnection() {
       String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
       String user = "scott";
       String password = "scott";
       String Driver = "oracle.jdbc.driver.OracleDriver";
       Connection conn = null;
       try {
           Class.forName(Driver);
           conn = DriverManager.getConnection(url, user,password);
       } catch (Exception e) {
           System.out.println("连接数据库出现异常" + e.getMessage());
       }
       return conn;
    }
 
    /***
     * 释放资源...
     * @param rs
     * @param st
     * @param conn
     */
    public static void Relesae(ResultSet rs, Statement st, Connection conn) {
       try {
           if (rs != null)
              rs.close();
           if (st != null)
              st.close();
           if (conn != null)
              conn.close();
       } catch (SQLException e) {
           e.printStackTrace();
       }
    }
}
 ===============================================需要那个DataBase就开那个数据库===================================
 
#MySQL DataBase Connection Config
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
username=root
password=root
 
#SqlServer DataBaseConnection Config
 
#driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
#url=jdbc:sqlserver://localhost:1433;DatabaseName=jdbc
#username=sa
#password=sa
 
#Oracle DataBase ConnectionConfig
#driver=oracle.jdbc.driver.OracleDriver
#url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
#username=scott
#password=scott 
===================================================================================================
 
package utility;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import org.apache.log4j.PropertyConfigurator;
/**
 * 需要一个log4j.jar包
 * 扩展性稍微好点.
 */
public class ConfigHelper {
protected static Connection conn = null;
protected  static Properties pro = null;
protected  static InputStream is = null;

/**得到配置的key
* @param key
* @return
*/
public static String getConfigKey(String key) {
String strVal = "";
try {
is = new FileInputStream(new File(
"src/application/jdbc_config.properties"));
pro = new Properties();
pro.load(is);

PropertyConfigurator.configure(pro);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
strVal = (String) pro.getProperty(key);
return strVal;
}
/**
* 得到连接
* @return
*/
public static Connection getConnection() {
String driver = getConfigKey("driver");
String url = getConfigKey("url");
String username = getConfigKey("username");
String password = getConfigKey("password");
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
/**
*  释放资源...
* @param rs
* @param stmt
* @param conn
*/
public void  ReleaseSoucre(ResultSet rs,Statement stmt, Connection conn){
try {
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

 

最后

以上就是魔幻手机为你收集整理的Log4j与单例模式连接数据库通用类的全部内容,希望文章能够帮你解决Log4j与单例模式连接数据库通用类所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(36)

评论列表共有 0 条评论

立即
投稿
返回
顶部