package com.yum.amp.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBUtil {
//private Connection conn;
private static final String DRIVER = "net.sourceforge.jtds.jdbc.Driver";
private static final String URL = "jdbc:jtds:sqlserver://127.0.0.1:1433/test";
private static final String USERNAME = "sa";
private static final String PASSWORD = "123456";
// public DBUtil() {
// try{
// //加载驱动
// Class.forName(DRIVER);
// //连接数据库
// this.conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
// } catch(ClassNotFoundException cnfe) {
// cnfe.printStackTrace();
// } catch(SQLException e) {
// e.printStackTrace();
// }
// }
//
// private Statement getStatement() {
// Statement stmt = null;
// try {
// stmt = conn.createStatement();
// } catch (SQLException e) {
// e.printStackTrace();
// }
// return stmt;
// }
//
// private PreparedStatement getPreparedStatement(String sql) {
// PreparedStatement pstmt = null;
// try {
// pstmt = conn.prepareStatement(sql);
// } catch (SQLException e) {
// e.printStackTrace();
// }
// return pstmt;
// }
static {
try {
//加载数据库驱动
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//获取数据库连接对象
public static Connection getConn() {
Connection conn = null;
try {
//"jdbc:oracle:thin:@localhost:1521:你的数据库名字", "用户名","密码"
conn = DriverManager.getConnection(URL, USERNAME,PASSWORD);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
//获取语句执行对象
public static Statement getStatement(Connection conn) {
Statement stmt = null;
try {
stmt = conn.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
return stmt;
}
//获取预处理语句执行对象
public static PreparedStatement getPreparedStatement(Connection conn,String sql) {
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return pstmt;
}
//获取结果集对象
public static ResultSet getResultSet(PreparedStatement pstmt) {
ResultSet res = null;
try {
res = pstmt.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return res;
}
//获取结果集对象
public static ResultSet getResultSet(Statement stmt, String sql) {
ResultSet res = null;
try {
res = stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return res;
}
//关闭资源方法
public static void close(Connection conn, Statement stmt, ResultSet res) {
close(res);
close(stmt);
close(conn);
}
//封装方法关闭语句对象
private static void close(Statement stmt) {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt = null;
}
}
//封装方法关闭结果集对象
private static void close(ResultSet res) {
if (res != null) {
try {
res.close();
} catch (SQLException e) {
e.printStackTrace();
}
res = null;
}
}
//封装方法关闭数据库连接对象
private static void close(Connection conn) {
try {
if (conn != null && !conn.isClosed()) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
jdbc
最新推荐文章于 2024-08-15 01:26:58 发布