日期:2014-05-16  浏览次数:20633 次

项目中连接数据库的工具类

  在项目有时会用到不同数据库,项目写了一个连接不同数据库(包括mysql,SQL server, oracle ,access)的工具类:

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ConnectionDbUtils {

    /**
     * 获取数据库连接对象(sql server)
     * 
     * @param server
     *            服务器
     * @param database
     *            数据库名
     * @param user_id
     *            用户名
     * @param password
     *            密码
     * @return Connection
     * @throws ClassNotFoundException
     * @throws SQLException
     */
    public static Connection getSqlServerConnection(String server, String database, String user_id, String password)
            throws ClassNotFoundException, SQLException {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        Connection con = DriverManager.getConnection("jdbc:sqlserver://" + server + ":1433;DatabaseName=" + database,
                user_id, password);
        return con;
    }
    
    
    /**
     * 获取数据库连接对象(MySql)
     * 
     * @param server
     *            服务器
     * @param database
     *            数据库名
     * @param user_id
     *            用户名
     * @param password
     *            密码
     * @return Connection
     * @throws ClassNotFoundException
     * @throws SQLException
     */
    public static Connection getMySqlConnection(String server, String database, String user_id, String password)
            throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
        return DriverManager.getConnection("jdbc:mysql://"+server+":3306/" + database,
        		user_id, password);
    }

    /**
     * JDBC连接oracle
     * @param server IP
     * @param database 数据库
     * @param user_id 用户名
     * @param password 密码
     * @param sql 
     * @return
     * @throws ClassNotFoundException
     * @throws SQLException
     */
    public static Connection getOracleConnection(String server, String database, String user_id, String password)
            throws ClassNotFoundException, SQLException {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@" + server + ":1521:" + database, user_id,
                password);
        return conn;
    }

    /**
     * JDBC连接Access
     * @param database 数据库路径
     * @param user_id 用户名
     * @param password 密码
     * @param sql 
     * @return
     * @throws ClassNotFoundException
     * @throws SQLException
     * @throws IllegalAccessException 
     * @throws InstantiationException 
     */
    public static Connection getAccessConnection(String database, String user_id, String password)
            throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {
        System.out.println("==============" + database);
        Class.forName("com.hxtt.sql.access.AccessDriver").newInstance();
        Connection conn = DriverManager.getConnection("jdbc:Access:///" + database, user_id, password);
        System.out.println("连接成功");
        return conn;
    }

    /**
     * Access 查询数据
     * @param database
     * @param user_id
     * @param password
     * @param strSql
     * @return
     * @throws Exception
     */
    public static List<Map<String, Object>> queryAccessData(String database, String user_id, String password,
            String strSql) throws Exception {
		File file = new File(database);
		if (file.exists()){
			if (file.canW