package com.lxf.bean.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* 数据库连接类
*
* @author 刘向峰
*
*/
public class DbConnection {
//oracle连接写法
private static String diver = "oracle.jdbc.driver.OracleDriver";
private static String url = "jdbc:oracle:thin:@localhost:1521:orcl";
private static String username = "lxf";
private static String password = "lxf";
static {
try {
//加载驱动
Class.forName(diver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 获取连接
*
* @return connection
*/
public static Connection getConn() {
Connection conn = null;
try {
conn = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
/**
* 关闭资源
*
* @param connection
* @param statement
* @param resultSet
*/
public static void close(Connection connection, Statement statement,
ResultSet resultSet) {
try {
if (connection != null) {
connection.close();
connection = null;
}
if (statement != null) {
statement.close();
statement = null;
}
if (resultSet != null) {
resultSet.close();
resultSet = null;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
时间: 2024-11-08 23:09:43