网站 备案 拍照,网站建设五项基本原则,国外专门做童装的网站有哪些,湖南微信网站营销C3p0数据库的连接方式是目前市场场最为广泛的类型之一 本篇主要你演示C3p0使用文件配置和不使用文件配置的两种操作方式 #######使用文件配置 import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;import com.dbutil.zyz.ConnLink;
… C3p0数据库的连接方式是目前市场场最为广泛的类型之一 本篇主要你演示C3p0使用文件配置和不使用文件配置的两种操作方式 #######使用文件配置 import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;import com.dbutil.zyz.ConnLink;
import com.mchange.v2.c3p0.ComboPooledDataSource;public class C3p0Demo1 {
//首先演示下C3p0不使用配置文件的连接数据库的方式static ConnLink connlinknew ConnLink();static Connection connnull;static PreparedStatement pstmtnull;public static void main(String[] args) throws SQLException {try {//总体步骤配置如下可以作为模板使用//1.创建datasourceComboPooledDataSource dataSourcenew ComboPooledDataSource();//2.设置连接数据的信息dataSource.setJdbcUrl(jdbc:mysql://localhost/test);dataSource.setDriverClass(com.mysql.jdbc.Driver);dataSource.setUser(root);dataSource.setPassword(root);//得到连接对象conndataSource.getConnection();String sqlinsert into blank values(null,?,?);pstmtconn.prepareStatement(sql);pstmt.setString(1, test);pstmt.setInt(2,2000);pstmt.executeUpdate();} catch (Exception e) {e.printStackTrace();}finally{connlink.relese(conn, pstmt);}}
} ########不使用文件配置 import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;import com.mchange.v2.c3p0.ComboPooledDataSource;public class C3p0Demo2 {//下面开始演示使用配置文件的C3p0用法static ConnLink connlinknew ConnLink();static Connection connnull;static PreparedStatement pstmtnull;public static void main(String[] args) throws SQLException {//就new了一个对象。在这种情况下c3p0会直接找到c3p0-config.xml文件//并且在c3p0-config.xml文件中默认的找到 default-config配置try {ComboPooledDataSource dataSourcenew ComboPooledDataSource();//2.得到连接对象conndataSource.getConnection();String sqlinsert into blank values(null,?,?);pstmtconn.prepareStatement(sql);pstmt.setString(1, root);pstmt.setInt(2,20000);pstmt.executeUpdate();} catch (Exception e) {e.printStackTrace();}finally{connlink.relese(conn, pstmt);}}
} 其中配置文件的信息如下文件名称不可以更改 ?xml version1.0 encodingUTF-8?
c3p0-configdefault-configproperty namedriverClasscom.mysql.jdbc.Driver/propertyproperty namejdbcUrljdbc:mysql://localhost/test/propertyproperty nameuserroot/propertyproperty namepasswordroot/propertyproperty nameinitialPoolSize5/propertyproperty namemaxPoolSize20/property/default-confignamed-config nameoracle property namedriverClasscom.mysql.jdbc.Driver/propertyproperty namejdbcUrljdbc:mysql:///web_07/propertyproperty nameuserroot/propertyproperty namepassword123/property/named-config/c3p0-config 注意代码中的释放调用代码如下 import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;public class ConnLink {
//数据库的连接部分public String jdbc_drivercom.mysql.jdbc.Driver;public String jdbc_connjdbc:mysql://localhost:3306/test;public String userroot;public String passroot;//返回连接函数的部分public Connection getConn() throws SQLException, ClassNotFoundException{//1.注册驱动Class.forName(jdbc_driver);//2.获取连接Connection connectionDriverManager.getConnection(jdbc_conn,user,pass);return connection;}//释放连接资源的部分public void relese(Connection conn,PreparedStatement pstmt) throws SQLException{if(pstmt!null) pstmt.close();if(conn!null) conn.close();}
} #####数据库变化如下 转载于:https://www.cnblogs.com/byczyz/p/11349052.html