2016-04-22jdbc工具类优化,外部资源加载,类加载器,Properties
Posted 暴走灬青春
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2016-04-22jdbc工具类优化,外部资源加载,类加载器,Properties相关的知识,希望对你有一定的参考价值。
package com.heima.utils;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
public class JdbcUtils
static String url;
static String user;
static String password;
static String driverClass;
static
//创建加载外部内容的对象 保存以key value 格式
Properties props = new Properties();
try
//通过类加载器的方式获得外部文件的流 配置文件必须放在src目录下
InputStream in =
JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties");
//加载外部文件的内容
props.load(in);
catch (IOException e1)
// TODO Auto-generated catch block
e1.printStackTrace();
try
driverClass = props.getProperty("driverClass");
Class.forName(driverClass);
catch (ClassNotFoundException e)
// TODO Auto-generated catch block
e.printStackTrace();
url = props.getProperty("url");
user = props.getProperty("user");
password = props.getProperty("password");
public static Connection getConn() throws SQLException
//获得连接
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
public static void release(ResultSet rs,PreparedStatement stmt,Connection conn)
try
if(rs != null)
rs.close();
rs = null;
catch (Exception e2)
release(stmt,conn);
public static void release(PreparedStatement stmt,Connection conn)
try
if( stmt != null)
stmt.close();
stmt = null;
catch (Exception e2)
try
if( conn != null)
conn.close();
conn = null;
catch (Exception e2)
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mytest
user=root
password=123456
以上是关于2016-04-22jdbc工具类优化,外部资源加载,类加载器,Properties的主要内容,如果未能解决你的问题,请参考以下文章