jdbc


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;
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值