欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

发个jdbc工具类

程序员文章站 2022-05-19 12:03:16
...

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
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 JdbcUtil
{
private static String db_driver;
private static String db_url;
private static String db_userName;
private static String db_passWord;

static
{
Properties pro=new Properties();
try {
pro.load(new FileInputStream((JdbcUtil.class.getResource("/").getPath()+"/jdbc.properties")));
db_driver=pro.getProperty("db_driver");
db_url=pro.getProperty("db_url");
db_userName=pro.getProperty("db_userName");
db_passWord=pro.getProperty("db_passWord");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public static Connection getConnection()
{
Connection conn=null;

try {
Class.forName(db_driver);

conn=DriverManager.getConnection(db_url, db_userName, db_passWord);


} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}

return conn;
}

public static void close(Connection conn,PreparedStatement ps,ResultSet rs)
{
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();
}
}
}

public static void close(Connection conn)
{
if(conn!=null)
{
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}



}


db_driver=oracle.jdbc.driver.OracleDriver
db_url=jdbc:oracle:thin:@10.66.6.10:1521:dot
db_userName=fms2
db_passWord=1qaz2wsx