一、通过Driver接口直接连接

/***通过Driver获取Connection*@return*/publicConnectiongetConnectionByDriver()throwsException{StringdriverClass="com.mysql.jdbc.Driver";Stringurl="jdbc:mysql:///hdz";Stringuser="root";Stringpassword="123456";Driverdriver=newcom.mysql.jdbc.Driver();Propertiesinfo=newProperties();info.setProperty("driverClass",driverClass);info.setProperty("user",user);info.setProperty("password",password);Connectionconnection=driver.connect(url,info);returnconnection;}

二、通过DriverManager直接连接

/***通过DriverManager获取Connection*@return*@throwsException*/publicConnectiongetConnectionByDriverManager()throwsException{Stringurl="jdbc:mysql:///hdz";Stringuser="root";Stringpassword="123456";Class.forName("com.mysql.jdbc.Driver");Connectionconnection=DriverManager.getConnection(url,user,password);returnconnection;}

/***通过把参数写在配置文件的方式获取Connection*@return*@throwsException*/publicConnectiongetConectionByProperties()throwsException{InputStreaminputStream=this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");Propertiesinfo=newProperties();info.load(inputStream);Stringurl=info.getProperty("url");Class.forName("com.mysql.jdbc.Driver");Connectionconnection=DriverManager.getConnection(url,info);returnconnection;}url=jdbc:mysql:///hdzuser=rootpassword=123456

三、通过DBCP数据源连接

@TestpublicvoidtestDbcp()throwsException{finalBasicDataSourcebasicDataSource=newBasicDataSource();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);Connectionconnection1=basicDataSource.getConnection();System.out.println(connection1);Connectionconnection2=basicDataSource.getConnection();System.out.println(connection2);newThread(){@Overridepublicvoidrun(){Connectionconnection3;try{connection3=basicDataSource.getConnection();System.out.println(connection3);}catch(SQLExceptione){e.printStackTrace();}}}.start();Thread.sleep(3000);connection2.close();}

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

privateDbcpDataSource(){Propertiesinfo=newProperties();InputStreaminputStream=this.getClass().getClassLoader().getResourceAsStream("dbcp.properties");try{info.load(inputStream);dataSource=BasicDataSourceFactory.createDataSource(info);}catch(IOExceptione){e.printStackTrace();}catch(Exceptione){e.printStackTrace();}}dbcp.propertiesdriverClassName=com.mysql.jdbc.Driverurl=jdbc:mysql:///hdzusername=rootpassword=123456initialSize=5maxActive=10minIdle=5maxWait=5000

四、通过C3P0数据源连接

publicclassC3p0DataSourceUtils{privateDataSourcedataSource=null;privatestaticC3p0DataSourceUtilsinstance=newC3p0DataSourceUtils();privateC3p0DataSourceUtils(){dataSource=newComboPooledDataSource("intergalactoApp");}publicstaticC3p0DataSourceUtilsnewInstance(){returninstance;}publicConnectiongetConnection(){try{returndataSource.getConnection();}catch(SQLExceptione){e.printStackTrace();}returnnull;}}

c3p0-config.xml<?xmlversion="1.0"encoding="UTF-8"?><c3p0-config><!--Thisappismassive!--><named-configname="intergalactoApp"><propertyname="acquireIncrement">1</property><propertyname="initialPoolSize">1</property><propertyname="minPoolSize">1</property><propertyname="maxPoolSize">1</property><propertyname="maxIdleTimeExcessConnections">1000</property><!--intergalactoAppadoptsadifferentapproachtoconfiguringstatementcaching--><propertyname="maxStatements">10</property><propertyname="maxStatementsPerConnection">5</property><propertyname="driverClass">com.mysql.jdbc.Driver</property><propertyname="jdbcUrl">jdbc:mysql:///hdz</property><propertyname="user">root</property><propertyname="password">123456</property></named-config></c3p0-config>