In the past, we got the connection object by DriverManager this class.
Now we get connection through DataSource,
The rest is the same thing,
First, DataSource gets the connection code.
Before that, test the contents of the Db.properties configuration file in the first place
Jdbc.datasource=com.mchange.v2.c3p0.combopooleddatasource
Jdbc.driverclass=com.mysql.jdbc.driver
Jdbc.jdbcurl=jdbc:mysql://localhost:3306/test
jdbc.user=root
jdbc.password=
jdbc.maxpoolsize=100
jdbc.minpoolsize=2
jdbc.initialpoolsize=2
jdbc.acquireincrement=2
jdbc.maxstatements=1000
jdbc.maxidletime=300
jdbc.checkouttimeout=5000
The connection's explanation is all on the code annotation,
Package org.test;
Import java.sql.Connection;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import java.util.Properties;
Import Javax.sql.DataSource;
Import Org.apache.commons.beanutils.BeanUtils;
public class Jdbcdatasourcetest {private static DataSource DataSource;
private static Connection Connection;
private static PreparedStatement PS;
private static ResultSet Rs;
public static void Main (string[] args) throws Exception {Properties Properties = new properties (); Loads data from the specified configuration file into the properties Properties.load (JdbcDataSourceTest.class.getClassLoader (). getResourceAsStream ("
Db.properties ")); /** * Properties One of the records is like this * Jdbc.datasource=com.mchange.v2.c3p0.combopooleddatasource * will be preceded by JDBC. Remove the reload in a proper
Ties/Properties Dbproperties = new properties ();
For (Object Key:properties.keySet ()) {String temp = (string) key;
if (Temp.startswith ("jdbc."))
{String name = temp.substring (5); Dbproperties.puT (name, Properties.getproperty (temp));
}//List all properties in Dbproperties dbproperties.list (System.out);
System.out.println ("=============================================="); Loads its object dataSource = (dataSource) class.forname (Properties.getproperty ("Jdbc.datasource")) from the vendor-supplied DataSource implementation.
Newinstance ();
Add the contents of the Dbproperties property to the DataSource object Beanutils.populate (DataSource, dbproperties);
Connection = Datasource.getconnection ();
PS = connection.preparestatement ("SELECT * from Test");
rs = Ps.executequery ();
while (Rs.next ()) {System.out.println (rs.getstring (1));
}
}
}
And then comes the code that used to get connection through DriverManager.
Package org.test;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
public class Jdbctest
{
private static Connection Connection;
private static PreparedStatement PS;
private static ResultSet Rs ;
Static
{
try
{
class.forname ("Com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException e)
{
e.printstacktrace ();
}
}
public static void Main (string[] args) throws Exception
{
connection = drivermanager.getconnection ("JDBC: Mysql://localhost:3306/test "," Root "," "");
PS = connection.preparestatement ("SELECT * from Test");
rs = Ps.executequery ();
while (Rs.next ())
{
System.out.println (rs.getstring (1));
}
}
}