jdbc数据库连接及优化
2015-10-18 11:44
375 查看
java数据库连接模板: static void template()throws Exception { Connection conn=null; Statement stmt=null; ResultSet rs=null; try { //注册驱动 Class.forName("com.mysql.jdbc.Driver()"); //创建连接 String url="jdbc:mysql//localhost:3306//jdbc"; String user="root"; String password=""; conn=DriverManager.getConnection(url,user,password); //执行语句 stmt=conn.createStatement(); //处理结果 while(rs.next()) { System.out.println(rs.getObject(1)+"\t"+rs.getObject(2)+"\t"+rs.getObject(3)+"\t"+rs.getObject(4)); } } finally { try { if(rs!=null) rs.close(); } finally { if(stmt!=null) stmt.close(); } finally { if(conn!=null) conn.close(); } } }
对数据库进行优化:
先创建一个jdbc工具类进行优化
try
{
if(rs!=null)
rs.close();
}
finally
{
if(stmt!=null)
stmt.close();
}
finally
{
if(conn!=null)
conn.close();
}
在另一个文件里
public class name {
public static void main(String[]args)
{
} static void template()throws Exception { Connection conn=null; Statement stmt=null; ResultSet rs=null; try { //注册驱动 Class.forName("com.mysql.jdbc.Driver()"); //创建连接 String url="jdbc:mysql//localhost:3306//jdbc"; String user="root"; String password=""; conn=jdbcUtils.getConnetiion(); //执行语句 stmt=conn.createStatement(); //处理结果 while(rs.next()) { System.out.println(rs.getObject(1)+"\t"+rs.getObject(2)+"\t"+rs.getObject(3)+"\t"+rs.getObject(4)); } } finally { jdbcUtils.free(rs, stmt, conn); }
相关文章推荐
- MYSQL主从数据库同步备份配置的方法
- mysql Access denied for user 'ODBC'@'localhost'(using password: NO)解决办法
- 【SQL】分组再聚合&ratio_to_report函数
- jsp 配置MySQL服务器 以及数据的插入和读取
- 数据库选型之亿级数据量并发访问(MySQL集群)
- 数据库选型之亿级数据量并发访问(MySQL集群)
- 解决此问题:Oracle 删除用户时报 “必须指定 CASCADE 以删除 'SE'”,
- MySql存储过程之逻辑判断和条件控制
- 完全卸载Oracle11g数据库
- JDBC连接数据库
- centOS下mongodb的安装
- MySQL用户远程访问权限开通
- Redis、memcache
- Oracle数据泵的使用
- SQL的主键和外键的作用
- ORACLE功能GREATEST功能说明具体实例
- oracle表空间创建
- sql统计查询——求不同类别的平均值
- mysql中如何判断是否支持分区
- Redis 集群教程