- //初始应用
import java.beans.PropertyVetoException; - import java.sql.Connection;
- import java.sql.SQLException;
- import cn.wobushisheng.Setting;
- import com.nikandaosmle.htm.HtmConfig;
- import com.mchange.v2.c3p0.ComboPooledDataSource;
- import com.mchange.v2.c3p0.DataSources;
- public class DBFactoryPool {
- ComboPooledDataSource cpds = new ComboPooledDataSource();
- HtmConfig config = (HtmConfig) Setting.appconfig;
- String driverURL = config.jdbcDriver;
- String connURL = config.jdbcUrl;
- String user = config.jdbcUser;
- String passwd = config.dbcPaswd;
- String maxidle = config.jdbcMaxIdel;
- public Connection getConnection() {
- Connection conn = null;
- try {
- cpds.setDriverClass(driverURL);
- cpds.setJdbcUrl(connURL);
- cpds.setUser(user);
- cpds.setPassword(passwd);
- cpds.setMaxIdleTime(Integer.valueOf(maxidle));
- cpds.setMaxStatements(0);
- conn = cpds.getConnection();
- } catch (PropertyVetoException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return conn;
- }
- public void destory() {
- try {
- DataSources.destroy(cpds);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
上面是这个连接类,下面是具体。
- import java.sql.Connection;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class M_UserManagerImp implements M_UserManager {
- public void save(Operator operator) {
- Connection conn = null;
- Statement sta = null;
- DBFactoryPool dbpool;
- dbpool = new DBFactoryPool();
- try {
- conn = dbpool.getConnection();
- sta = conn.createStatement();
- // 标签没INSERT
- StringBuffer sql = new StringBuffer(
- "insert into m_user_info(USER_ID,USER_NAME,USER_ROLE,PASSWORD) values(");
- sql.append("'").append(operator.getLoginid()).append("',");
- sql.append("'").append(operator.getName()).append("',");
- sql.append(operator.getRoleno()).append(",");
- sql.append("'").append(operator.getPasswd()).append("')");
- sta.executeUpdate(sql.toString());
- conn.commit();
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- dbpool.destory();
- }
- }
- }