继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

JDBC获取数据库连接方式

quincyye
关注TA
已关注
手记 235
粉丝 168
获赞 3550

Oracle
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:DBNAME";
String user="abc";
String password="123";
Connection conn= DriverManager.getConnection(url,user,password);
Sql Server
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=DBNAME";
String user="abc";
String password="123";
Connection conn= DriverManager.getConnection(url,user,password);
DB2
Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance();
String url="jdbc:db2://localhost:5000/DBNAME";
String user="abc";
String password="123";
Connection conn= DriverManager.getConnection(url,user,password);
Informix
Class.forName("com.informix.jdbc.IfxDriver").newInstance();
String url ="jdbc:informix-sqli://127.0.0.1:1533/DBNAME:INFORMIXSERVER=myserver;user=abc;password=123";
Connection conn= DriverManager.getConnection(url);
SyBase
Class.forName("com.sybase.jdbc.SybDriver").newInstance();
String url =" jdbc:sybase:Tds:localhost:5007/DBNAME";
Properties sysProps = System.getProperties();
SysProps.put("user","abc");
SysProps.put("password","123");
Connection conn= DriverManager.getConnection(url, SysProps);
MySql
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url ="jdbc:mysql://localhost/DBNAME?user=abc&password=123&useUnicode=true&characterEncoding=8859_1" ;
Connection conn= DriverManager.getConnection(url);
PostgreSQL
Class.forName("org.postgresql.Driver").newInstance();
String url ="jdbc:postgresql://localhost/DBNAME" ;
String user="abc";
String password="123";
Connection conn= DriverManager.getConnection(url,user,password);

/*
ConnectionUtil .java


*/

package com.cai.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties ;
public class ConnectionUtil {
/**

  • 1
    *在方法中固化连接参数
  • @return 数据库连接
    */
    public Connection getConnection(){
    Connection conn = null ;
    try{
    Class.forName("com.mysql.jdbc.Driver") ;

    conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/stud", "root", "root") ;

    return conn ;

    }catch(Exception e){
    e.printStackTrace() ;
    }
    return null ;
    }

    /**

  • 2
    *通过方法参数方式传递连接参数
  • @return 数据库连接
    */
    public Connection getConnection(String driver, String url, String user, String password){

    Connection conn = null ;

    try{
    Class.forName(driver) ;
    conn = DriverManager.getConnection(url, user, password) ;

    return conn ;

    }catch(Exception e){
    e.printStackTrace();
    }

    return null ;
    }

    /**

  • 3
    *通过properties配置文件的方式灵活配置连接参数,properties中的属性名固化
  • @return 数据库连接
    */
    public Connection openConnection(){

    Connection conn = null ;
    String driver = "" ;
    String url = "" ;
    String user = "" ;
    String password = "" ;
    Properties props = new Properties() ;
    try{
    props.load(this.getClass().getClassLoader().getResourceAsStream("DBConfig.properties")) ;
    driver = props.getProperty("driver") ;
    url = props.getProperty("url") ;
    user = props.getProperty("user") ;
    password = props.getProperty("password") ;

    Class.forName(driver) ;
    conn = DriverManager.getConnection(url, user, password) ;

    return conn ;
    }catch(Exception e){
    e.printStackTrace() ;
    }

    return null ;
    }

    public static void main(String []args){
    ConnectionUtil cu = new ConnectionUtil() ;

    System.out.println("1、---->" + cu.getConnection()) ;
    System.out.println("2、---->" + cu.getConnection("com.mysql.jdbc.Driver",
    "jdbc:mysql://localhost:3306/stud", "root", "root")) ;
    System.out.println("3、---->" + cu.openConnection()) ;

    }

}

打开App,阅读手记
2人推荐
发表评论
随时随地看视频慕课网APP