jdbc连接工具类
2017-04-13 18:10
274 查看
package com.crm.util.vc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* 数据库连接与关闭工具类。
* @author guoqiang
*/
public class DBHelper {
private static String driver =
"com.microsoft.sqlserver.jdbc.SQLServerDriver";// 数据库驱动字符串
//本机
private static String url =
"jdbc:sqlserver://localhost:1433;DatabaseName=oldmanv10";// 连接URL字符串
private static String user = "sa"; // 数据库用户名
private static String password = "123"; // 用户密码
protected Connection conn;
protected PreparedStatement pstmt;
protected ResultSet rs;
/**
* 获取数据库连接对象。
*/
public Connection getConnection() {
Connection conn = null;// 数据连接对象
// 获取连接并捕获异常
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
// Context ctx=new InitialContext();
// DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/EasyBuy");
// conn=ds.getConnection();
} catch (Exception e) {
e.printStackTrace();// 异常处理
}
return conn;// 返回连接对象
}
/**
* 关闭数据库连接。
* @param conn 数据库连接
* @param stmt Statement对象
* @param rs 结果集
*/
public void closeAll(Connection conn, Statement stmt, ResultSet rs) {
// 若结果集对象不为空,则关闭
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// 若Statement对象不为空,则关闭
if (stmt != null) {
try {
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// 若数据库连接对象不为空,则关闭
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 增、删、改操作
* @param sql sql语句
* @param prams 参数数组
* @return 执行结果
*/
public int exceuteUpdate(String sql,Object...prams){
int result=0;
conn=this.getConnection();
try {
pstmt=conn.prepareStatement(sql);
if(prams!=null){
for(int i=0;i<prams.length;i++){
pstmt.setObject(i+1, prams[i]);
}
}
result=pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally{
closeAll(conn, pstmt, rs);
}
return result;
}
/**
* 查
*/
public ResultSet exceuteQuery(String sql,Object...prams){
conn=this.getConnection();
try {
pstmt=conn.prepareStatement(sql);
if(prams!=null){
for(int i=0;i<prams.length;i++){
pstmt.setObject(i+1, prams[i]);
}
}
rs=pstmt.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* 数据库连接与关闭工具类。
* @author guoqiang
*/
public class DBHelper {
private static String driver =
"com.microsoft.sqlserver.jdbc.SQLServerDriver";// 数据库驱动字符串
//本机
private static String url =
"jdbc:sqlserver://localhost:1433;DatabaseName=oldmanv10";// 连接URL字符串
private static String user = "sa"; // 数据库用户名
private static String password = "123"; // 用户密码
protected Connection conn;
protected PreparedStatement pstmt;
protected ResultSet rs;
/**
* 获取数据库连接对象。
*/
public Connection getConnection() {
Connection conn = null;// 数据连接对象
// 获取连接并捕获异常
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
// Context ctx=new InitialContext();
// DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/EasyBuy");
// conn=ds.getConnection();
} catch (Exception e) {
e.printStackTrace();// 异常处理
}
return conn;// 返回连接对象
}
/**
* 关闭数据库连接。
* @param conn 数据库连接
* @param stmt Statement对象
* @param rs 结果集
*/
public void closeAll(Connection conn, Statement stmt, ResultSet rs) {
// 若结果集对象不为空,则关闭
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// 若Statement对象不为空,则关闭
if (stmt != null) {
try {
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// 若数据库连接对象不为空,则关闭
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 增、删、改操作
* @param sql sql语句
* @param prams 参数数组
* @return 执行结果
*/
public int exceuteUpdate(String sql,Object...prams){
int result=0;
conn=this.getConnection();
try {
pstmt=conn.prepareStatement(sql);
if(prams!=null){
for(int i=0;i<prams.length;i++){
pstmt.setObject(i+1, prams[i]);
}
}
result=pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally{
closeAll(conn, pstmt, rs);
}
return result;
}
/**
* 查
*/
public ResultSet exceuteQuery(String sql,Object...prams){
conn=this.getConnection();
try {
pstmt=conn.prepareStatement(sql);
if(prams!=null){
for(int i=0;i<prams.length;i++){
pstmt.setObject(i+1, prams[i]);
}
}
rs=pstmt.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
}
相关文章推荐
- jdbc连接mysql连接数据库工具类
- JDBC连接数据库工具类及连接Access数据库示例
- JDBC中 mysql数据库的连接工具类 Java登录 及增删改查 整理 附带:Navicat Premium 11.0.12中文破解版.zip(下载)mysql数据库工具
- 两个JDBC连接数据库的工具类
- JDBC连接数据库(Mysql、抽取工具类)
- JDBC之数据库连接的java配置工具类dbUtils
- JDBC 实例--JDBC通过工具类DBUtil连接到数据库,让我们不再恐惧操作数据库
- JDBC第一篇--【介绍JDBC、使用JDBC连接数据库、简单的工具类】
- JDBC获取连接的三种工具类封装方式(v1.0)
- jdbc,关闭连接工具类
- 通过使用集合Properties完成JDBC的连接工具类
- JDBC动态SQL语句连接orcale数据库的工具类
- JDBC连接数据库工具类及连接Access数据库示例
- JDBC数据库连接工具类
- JDBC第一篇--【介绍JDBC、使用JDBC连接数据库、简单的工具类】
- 封装一个JDBC的建立连接和关闭连接的工具类
- JDBC获取连接、关闭连接的简单工具类2
- JDBC连接数据库工具类
- 使用JDBC链接数据库之连接数据库通用工具类
- JDBC连接数据库工具类(mysql、oracle、sqlserver)