当前位置:首页 > 开发 > 编程语言 > 编程 > 正文

【精典】数据库相关相关

发表于: 2015-07-14   作者:gengzg   来源:转载   浏览:
摘要: package C3P0; import java.sql.Connection; import java.sql.SQLException; import java.beans.PropertyVetoException; import com.mchange.v2.c3p0.ComboPooledDataSource; public class DBPool{
package C3P0; 
import java.sql.Connection; 
import java.sql.SQLException; 
import java.beans.PropertyVetoException; 
import com.mchange.v2.c3p0.ComboPooledDataSource; 
public class DBPool{       
   private static DBPool dbPool;       
   private ComboPooledDataSource dataSource;     
 
   static {       
           dbPool = new DBPool();       
   }       
    
   public DBPool(){       
           try {       
                 dataSource = new ComboPooledDataSource();       
                 dataSource.setUser("id");       
                 dataSource.setPassword("pw");       
                 dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/test? 
autoReconnect=true&useUnicode=true&characterEncoding=GB2312"); 
                 dataSource.setDriverClass("com.mysql.jdbc.Driver"); 
                 dataSource.setInitialPoolSize(2); 
                 dataSource.setMinPoolSize(1); 
                 dataSource.setMaxPoolSize(10); 
                 dataSource.setMaxStatements(50); 
                 dataSource.setMaxIdleTime(60);       
           } catch (PropertyVetoException e) {       
               throw new RuntimeException(e);       
           }       
   }       
 
   public final static DBPool getInstance(){       
           return dbPool;       
   }       
 
   public final Connection getConnection(){       
           try {       
               return dataSource.getConnection();       
           }   catch (SQLException e)   {       
               throw new RuntimeException("无法从数据源获取连接",e);       
           }       
   }     
    
   public static void main(String[] args) throws SQLException { 
        Connection con = null; 
        try { 
        con = DBPool.getInstance().getConnection(); 
        } catch (Exception e){ 
        } finally { 
        if (con != null) 
        con.close(); 
        } 
        } 
 
}

c3p0方法:
  配置文件:c3p0-config.xml
  <?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<named-config name="userApp">
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>
<property name="user">root</property>
<property name="password">123456</property>
<property name="acquireIncrement">5</property>
<property name="initialPoolSize">10</property>
<property name="minPoolSize">10</property>
<property name="maxPoolSize">20</property><!-- intergalactoApp adopts a different approach to 
configuring statement caching -->
<property name="maxStatements">0</property>
<property name="maxStatementsPerConnection">5</property>
<!-- he's important, but there's only one of him -->
<user-overrides user="master-of-the-universe">
<property name="acquireIncrement">1</property>
<property name="initialPoolSize">1</property>
<property name="minPoolSize">1</property>
<property name="maxPoolSize">5</property>
<property name="maxStatementsPerConnection">50</property>
</user-overrides>
</named-config>
</c3p0-config>
连接数据库:
  package cn.langzi.jdbc.c3p0;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class DbConnection {
private static DataSource dataSource;
static{
dataSource = new ComboPooledDataSource("userApp");
}
public static Connection getConnectioon() throws SQLException{
return dataSource.getConnection();
}
}


public final class ConnectionManager {
 private static ConnectionManager instance;
 
 public ComboPooledDataSource ds;
 private static String c3p0Properties = "c3p0.properties";
 
 private ConnectionManager() throws Exception {
  Properties p = new Properties();
  p.load(this.getClass().getResourceAsStream(c3p0Properties));
  ds = new ComboPooledDataSource();
  ds.setUser(p.getProperty("user"));
  ds.setPassword(p.getProperty("user"));
  ds.setJdbcUrl(p.getProperty("user"));
  ds.setDriverClass(p.getProperty("user"));
  ds.setInitialPoolSize(Integer.parseInt(p.getProperty("initialPoolSize")));
  ds.setMinPoolSize(Integer.parseInt(p.getProperty("minPoolSize")));
  ds.setMaxPoolSize(Integer.parseInt(p.getProperty("maxPoolSize")));
  ds.setMaxStatements(Integer.parseInt(p.getProperty("maxStatements")));
  ds.setMaxIdleTime(Integer.parseInt(p.getProperty("maxIdleTime")));
 }
 
 public static final ConnectionManager getInstance() {
  if (instance == null) {
   try {
    instance = new ConnectionManager();
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
  return instance;
 }
 
 public synchronized final Connection getConnection() {
  try {
   return ds.getConnection();
  } catch (SQLException e) {
   e.printStackTrace();
  }
  return null;
 }
 
 protected void finalize() throws Throwable {
  DataSources.destroy(ds); // 关闭datasource
  super.finalize();
 }
}

 

【精典】数据库相关相关

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
经常会用到日历控件,现在就整理一下,好看即好用的日历控件 第一种方案 1.建立一个JS文件:Calenda
1、Checkpoint Process (CKPT): The checkpoint process (CKPT) is responsible for signaling DBW
1、数据库连接时:jdbc:mysql://localhost:3306/......,输入IP地址、localhost、127.0.0.1任何一种
批处理batch,执行多个SQL语句。 try{ stmt=conn.createStatement(); conn.setAutoCommit(false);//
1、在mysql命令框输入 \.文件名.sql 即可执行文档,所以可以将一些创建表啊什么的语句放到一个sql文
http://blog.csdn.net/pipisorry/article/details/45727309 django数据库错误相关问题 问题:字段修
1、创建一个新的数据库: sqlite3 文件名 先建立一个 Db 目录,并在 Db 目录中创建一个 test.db 数
记得看过有人说,现在的软件基本上都是连着数据库的,如果一个软件没有跟数据库相连,基本没什么作
今天在用Power Designer反向数据库时,遇到两个问题: 1、使用jdbc连接MySql,提示Could not Initial
PhosSNP for Systematic Analysis of Genetic Polymorphisms That Influence Protein Phosphorylati
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号