数据库连接[JDBC]

方法一:

ExpandedBlockStart.gif JDBC Source
package com.ly.util;

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

/**
 * 功能描述:连接数据库
 * 
 * 
@author  TONY
 *
 
*/
public  class ConnectDB {

     private  static  final String MYSQL = "jdbc:mysql://";

     private  static  final String ORACLE = "jdbc:oracle:thin:@";

     private  static  final String SQLSERVER = "jdbc:microsoft:sqlserver://";
     private ConnectDB() {
    }

     public  static Connection getConnection(String DBType, String url,
            String user, String password)  throws SQLException {
         if ("mysql".equalsIgnoreCase(DBType))
             return getMySqlConn(url, user, password);
         if ("oracle".equalsIgnoreCase(DBType))
             return getOracleConn(url, user, password);
         if ("sqlserver".equals(DBType)){
             return getSqlServerConn(url, user, password);
        }
         return  null;
    }

     public  static  void closeConn(Connection conn) {
         if (conn !=  null) {
             try {
                conn.close();
            }  catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

     private  static Connection getMySqlConn(String url, String user,
            String password)  throws SQLException {
        Connection conn =  null;
         try {
            Class.forName("com.mysql.jdbc.Driver"); //  加载驱动MYSQL
        }  catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        conn = DriverManager.getConnection(MYSQL + url, user, password);

         return conn;
    }

     private  static Connection getOracleConn(String url, String user,
            String password)  throws SQLException {
        Connection conn =  null;
         try {
            Class.forName("oracle.jdbc.driver.OracleDriver"); //  加载驱动ORACLE
        }  catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        conn = DriverManager.getConnection(ORACLE + url, user, password);

         return conn;
    }
    
     private  static Connection getSqlServerConn(String url, String user,
            String password)  throws SQLException {
        Connection conn =  null;
         try {
            Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); //  加载驱动MSSQL
        }  catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        conn = DriverManager.getConnection(SQLSERVER + url, user, password);

         return conn;
    }
     public  static  void main(String[] args) {
         try {
            Connection conn = getConnection("ORACLE", "127.0.0.1:1521:TEST", "tiger",
                    "dragon");
             if (conn ==  null) {
                System.out.println("Connection the database is failled !");
            }  else {
                System.out.println("Connection the database is success !");
                System.out.println(conn.toString());
            }
        }  catch (SQLException e) {
            e.printStackTrace();
        }

    }

}

 

方法二:

 

ExpandedBlockStart.gif Java类
package jdbcTest;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URISyntaxException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public  class ConnectionDB {
     //  数据库及server配置文件路径
     private  static  final String ACTIONPATH = "jdbc.properties";
     private  static String db_driverClassName =  null;
     private  static String db_url =  null;
     private  static String db_username =  null;
     private  static String db_password =  null;

     /**
     * 初使化参数
     
*/
     static{
        Properties prop =  new Properties();
        String path= null;
        FileInputStream fis =  null;
         try {
            path = ConnectionDB. class.getClassLoader().getResource("").toURI().getPath();
            fis =  new FileInputStream( new File(path + ACTIONPATH));
            prop.load(fis);
            db_driverClassName = prop.getProperty("driverClassName");
            db_url = prop.getProperty("url");
            db_username = prop.getProperty("username");
            db_password = prop.getProperty("password");
        }  catch (FileNotFoundException e) {
            e.printStackTrace();
        }  catch (IOException e) {
            e.printStackTrace();
        }  catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }
    
     /**
     * 
     * 
@return  得到一个数据库连接
     * 
@throws  SQLException
     
*/
     public  static Connection getConnection()  throws SQLException {
        Connection conn =  null;
         try {
            Class.forName(db_driverClassName).newInstance(); //  加载驱动
        }  catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.out.println("Oracle驱动没找到");
        }  catch (InstantiationException e) {
            e.printStackTrace();
        }  catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        conn = DriverManager.getConnection(db_url, db_username, db_password);

         return conn;
    }
    
     /**
     * 
     * 
@return  关闭数据库连接
     * 
@throws  SQLException
     
*/
     public  static  void closeConn(Connection conn) {
         if (conn !=  null) {
             try {
                conn.close();
            }  catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

     public  static  void main(String[] args) {
         try {
            Connection conn = getConnection();
             if (conn ==  null) {
                System.out.println("Connection the database is failled !");
            }  else {
                System.out.println("Connection the database is success !");
                System.out.println(conn.toString());
            }
        }  catch (SQLException e) {
            e.printStackTrace();
        }

    }

    
    
}

 

ExpandedBlockStart.gif jdbc.propertis
#数据库驱动 
driverClassName=oracle.jdbc.driver.OracleDriver

#数据库远程连接 
url=jdbc:oracle:thin:@10.1.3.205:1521:ora10g

#数据库用户名 
username=sims20_js

#数据库密码 
password=sims20_js

 

转载于:https://www.cnblogs.com/Jeanferly/archive/2012/03/27/2419335.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值