一、通过Driver接口直接连接

/** * 通过Driver获取Connection * @return */public Connection getConnectionByDriver() throws Exception{    String driverClass = "com.mysql.jdbc.Driver";    String url = "jdbc:mysql:///hdz";    String user = "root";    String password = "123456";    Driver driver = new com.mysql.jdbc.Driver();    Properties info = new Properties();    info.setProperty("driverClass", driverClass);    info.setProperty("user", user);    info.setProperty("password", password);    Connection connection = driver.connect(url, info);    return connection;}

二、通过DriverManager直接连接

/** * 通过DriverManager获取Connection  * @return * @throws Exception */public Connection getConnectionByDriverManager() throws Exception{    String url = "jdbc:mysql:///hdz";    String user = "root";    String password = "123456";    Class.forName("com.mysql.jdbc.Driver");    Connection connection = DriverManager.getConnection(url, user, password);    return connection;}
/**    * 通过把参数写在配置文件的方式获取Connection    * @return    * @throws Exception*/public Connection getConectionByProperties() throws Exception{	InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");	Properties info = new Properties();	info.load(inputStream);	String url = info.getProperty("url");	Class.forName("com.mysql.jdbc.Driver");	Connection connection = DriverManager.getConnection(url, info);	return connection;}url=jdbc:mysql:///hdzuser=rootpassword=123456

三、通过DBCP数据源连接

@Testpublic void testDbcp() throws Exception {    final BasicDataSource basicDataSource = new BasicDataSource();    basicDataSource.setDriverClassName("com.mysql.jdbc.Driver");    basicDataSource.setUrl("jdbc:mysql:///hdz");    basicDataSource.setUsername("root");    basicDataSource.setPassword("123456");    basicDataSource.setInitialSize(2);    basicDataSource.setMaxActive(2);    basicDataSource.setMinIdle(2);    basicDataSource.setMaxWait(2000);    Connection connection1 = basicDataSource.getConnection();    System.out.println(connection1);    Connection connection2 = basicDataSource.getConnection();    System.out.println(connection2);    new Thread(){        @Override        public void run() {            Connection connection3;            try {                connection3 = basicDataSource.getConnection();                System.out.println(connection3);            } catch (SQLException e) {                e.printStackTrace();            }        }    }.start();    Thread.sleep(3000);    connection2.close();}

或者通过配置文件,BasicDatasourceFactory工厂方式

private DbcpDataSource() {    Properties info = new Properties();    InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("dbcp.properties");    try {        info.load(inputStream);        dataSource = BasicDataSourceFactory.createDataSource(info);    } catch (IOException e) {        e.printStackTrace();    } catch (Exception e) {        e.printStackTrace();    }}dbcp.propertiesdriverClassName=com.mysql.jdbc.Driverurl=jdbc:mysql:///hdzusername=rootpassword=123456initialSize=5maxActive=10minIdle=5maxWait=5000

四、通过C3P0数据源连接

public class C3p0DataSourceUtils {private DataSource dataSource = null;private static C3p0DataSourceUtils instance = new C3p0DataSourceUtils();private C3p0DataSourceUtils(){    dataSource = new ComboPooledDataSource("intergalactoApp");}public static C3p0DataSourceUtils newInstance(){    return instance;}public Connection getConnection() {    try {        return dataSource.getConnection();    } catch (SQLException e) {        e.printStackTrace();    }    return null;    }}
c3p0-config.xml
  
  
     
1    
1    
1    
1    
1000    
    
10     
5
com.mysql.jdbc.Driver
jdbc:mysql:///hdz
root
123456