c3p0连接池连接数据库

jar包地址:http://download.csdn.net/download/qq_30946681/10177264

使用代码如下:



package c3p0连接池;
import java.beans.PropertyVetoException;
import com.mchange.v2.c3p0.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class C3p0 {

	public static void main(String[] args) {

		ComboPooledDataSource cpds = new ComboPooledDataSource();
		try {
			cpds.setDriverClass( "com.mysql.jdbc.Driver" );//驱动
		} catch (PropertyVetoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} //loads the jdbc driver            
		cpds.setJdbcUrl( "jdbc:mysql://localhost:3306/cs" );
		cpds.setUser("root");              //数据库用户                    
		cpds.setPassword("123456"); //密码
		int i = 0;
	    String sql = "insert into guan (name,num) values(?,?)";
	    PreparedStatement pstmt;
	    try {
	    Connection con = cpds.getConnection();   //获取连接
	        pstmt = (PreparedStatement) con.prepareStatement(sql);
	        pstmt.setString(1, "mingcehng");
	        pstmt.setInt(2, 3);
	        i = pstmt.executeUpdate();
	        pstmt.close();
	        con.close();
	    } catch (SQLException e) {
	        e.printStackTrace();
	    }
	    System.out.println(i);
		
	}

}


你可能感兴趣的:(Java)