package dbTest; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class JDBCTest { public static void main(String[] args) { String driver = "com.mysql.jdbc.Driver"; String dbName = "nnm5"; String passwrod = "OSSDB123"; String userName = "root"; String url = "jdbc:mysql://localhost:13306/" + dbName; String sql = "select ircnetnodeid, purpose_id,ircnetypeid from rcnetnode limit 1"; Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { Class.forName(driver); //JDBC获取连接 //Connection conn = DriverManager.getConnection(url, userName,passwrod); //Druid连接池获取连接 conn = DruidManager.getInstance().getConnection(); ps = conn.prepareStatement(sql); rs = ps.executeQuery(); while (rs.next()) { System.out.println("ircnetnodeid : " + rs.getInt(1) + " purpose_id : " + rs.getString(2) + " ircnetypeid : " + rs.getString(3)); } } catch (Exception e) { e.printStackTrace(); }finally{ // 关闭记录集 if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } // 关闭声明 if (ps != null) { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } // 关闭链接对象 if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }
上面的是传统的JDBC的方式,先下面是Druid数据库连接池的使用方式。
package dbTest; import java.sql.Connection; import java.sql.SQLException; import com.alibaba.druid.pool.DruidDataSource; public class DruidManager { private DruidManager() {} private static DruidManager single=null; private DruidDataSource dataSource; public synchronized static DruidManager getInstance() { if (single == null) { single = new DruidManager(); single.initPool(); } return single; } private void initPool() { dataSource = new DruidDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUsername("root"); dataSource.setPassword("OSSDB123"); dataSource.setUrl("jdbc:mysql://127.0.0.1:13306/nnm5"); dataSource.setInitialSize(5); dataSource.setMinIdle(1); dataSource.setMaxActive(10); // 启用监控统计功能 try { dataSource.setFilters("stat"); } catch (SQLException e) { thorw new ExceptionInInitializerError(e); }// for mysql dataSource.setPoolPreparedStatements(false); } //要考虑多线程的情况 public Connection getConnection(){ Connection connection = null; try { synchronized(dataSource){
connection = dataSource.getConnection(); }
} catch (SQLException e) { throw new Exception(e);
} return connection; } }
Druid是阿里巴巴提供的开源的数据库连接池,已经被很多的项目所采用。
需要下载druid.jar,项目地址:
https://github.com/AlibabaTech/druid/wiki
上面将配置信息直接写到程序中的方法并不好,可以写成配置文件druiddb.xml,去从配置文件中来读取配置。
driverClassName = com.mysql.jdbc.Driverurl =com.mysql.jdbc.Driver
uer = root
password = OSSDB123
initialSize =5
再修改上面的DruidManager类,使之从配置文件中加载参数:
Properties p = new Properties(); InputStream is = DuridManager,class.getClassLoader().loadResourceAsStream("duriddb.xml"); p.load(is);
dataSource = new DruidDataSource(p);
启用Web监控统计功能需要在Web应用的web.xml中加入这个Servlet声明
<servlet>
<servlet-name>DruidStatView</servlet-name>
<servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DruidStatView</servlet-name>
<url-pattern>/druid/*</url-pattern>
<servlet-mapping>
通过 http://ip:port/druid/ 地址访问即可
Spring的配置:
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="url" value="${jdbc_url}" /> <property name="username" value="${jdbc_username}" /> <property name="password" value="${jdbc_password}" /> <!-- 初始化连接大小 --> <property name="initialSize" value="0" /> <!-- 连接池最大使用连接数量 --> <property name="maxActive" value="20" /> <!-- 连接池最大空闲 --> <property name="maxIdle" value="20" /> <!-- 连接池最小空闲 --> <property name="minIdle" value="0" /> <!-- 获取连接最大等待时间 --> <property name="maxWait" value="60000" /> <!-- <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="33" /> --> <property name="validationQuery" value="${validationQuery}" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <property name="testWhileIdle" value="true" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="25200000" /> <!-- 打开removeAbandoned功能 --> <property name="removeAbandoned" value="true" /> <!-- 1800秒,也就是30分钟 --> <property name="removeAbandonedTimeout" value="1800" /> <!-- 关闭abanded连接时输出错误日志 --> <property name="logAbandoned" value="true" /> <!-- 监控数据库 --> <!-- <property name="filters" value="stat" /> --> <property name="filters" value="mergeStat" /> </bean>