package com.Jdbc.Until;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
import java.io.InputStream;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;
public class JdbcUntil {
//url
public static String url = null;
//用户名
public static String user = null;
//密码
public static String password =null;
//注册类名
public static String registClass =null;
public static Connection connect = null;
public static Statement statement = null;
//结果集
public static ResultSet resultSet = null;
//类加载调用
static{
try {
InputStream in = JdbcUntil.class.getClassLoader().getResourceAsStream("dbcfg.properties");
Properties pro = new Properties();
pro.load(in);
registClass = pro.getProperty("registClass");
url = pro.getProperty("url");
user = pro.getProperty("user");
password = pro.getProperty("password");
//注册驱动类
Class.forName(registClass);
} catch (Exception e) {
// TODO Auto-generated catch block
throw new ExceptionInInitializerError();
}
}
//获取 Connection
public static Connection getJdbcConnection() throws Exception
{
connect = DriverManager.getConnection(url, user, password);
return connect;
}
//释放资源方法
public static void realse(Connection connect,Statement statement,ResultSet result)
{
if(connect != null)
{
try {
connect.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
connect = null;
}
if(statement != null)
{
try {
statement.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
statement = null;
}
if(result != null)
{
try {
result.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
result = null;
}
}
}
调用:
Connection connted = JdbcUntil.getJdbcConnection();
Statement stat = connted.createStatement();
ResultSet result = stat.executeQuery();