Method One (recommended): Connect using an XML file
Xml
<?XML version= "1.0" encoding= "UTF-8"?><C3p0-config> <Default-config> < Propertyname= "Driverclass">Com.mysql.jdbc.Driver</ Property> < Propertyname= "Jdbcurl">Jdbc:mysql://localhost:3306/mydb?characterencoding=gbk</ Property> < Propertyname= "User">Root</ Property> < Propertyname= "Password"></ Property> </Default-config></C3p0-config>
class file
Public Static void throws exception{ new classpathxmlapplicationcontext ("Beans.xml"); = (DataSource) context.getbean ("DataSource"); = ds.getconnection (); Conn.close (); }
Method Two: Connect the database directly in the class file
public static void main00 (string[] args) throws Exception {combopooleddatasource ds = new
Combopooleddatasource (); \ \ Set the properties of the connected database Ds.setdriverclass ( "Com.mysql.jdbc.Driver"
"Jdbc:mysql://localhost:3306/mydb" "root" "" 5 20 = ds.getconnection (); Conn.close (); }
C3P0 two ways to connect to a database