数据库连接语句

  • 格式:txt
  • 大小:4.46 KB
  • 文档页数:2

jsp数据库连接语句

package com.cdjj.s2t046.upstill.zhangxianfu.dbconnetion;

import java.sql.*;

/**
* @功能:数据库连接类
* @作者:zxfonline
* @版本:v1.0
* @创建时间:2009-7-14
* @创建地点:北大青鸟成都锦江中心
*/
public class DBHelper {

/**
* JDBC驱动对象
*/
private static final String DRIVER_CLASS = "com.microsoft.sqlserver.jdbc.SQLServerDriver";

/**
* 功能: 数据库连接对象
*/
private static Connection conn = null;

/**
* 功能:SQL语句操作对象
*/
private static Statement stmt = null;

/**
* 功能:数据集
*/
private static ResultSet rs = null;

/**
* 功能:存储过程执行对象
*/
private static CallableStatement callStmt = null;

/**
* 功能:定义连接参数(URL)
*/
private static final String URL = "jdbc:sqlserver://localhost:1434;databaseName=bbsDB";

/**
* 功能:定义连接参数(帐号)
*/
private static final String LOGIN_NAME = "sa";

/**
* 功能:定义连接参数(密码)
*/
private static final String LOGIN_PASSWORD = "sqlpass";

/**
* 功能:获取数据库连接
*
* @throws Exception
*
*/
private static void getConn() throws Exception {

try {
// 显示创建的SQL数据库JDBC驱动对象
Class.forName(DRIVER_CLASS);
// 获取连接对象
conn = DriverManager.getConnection(URL, LOGIN_NAME, LOGIN_PASSWORD);
} catch (RuntimeException e) {
throw e;
}
}

/**
* 功能:关闭结果集和数据连接对象
*
* @param isCloseRS
* @throws Exception
*/
public static void close(boolean isCloseRS) throws Exception {
try {
// 关闭结果集
if (isCloseRS) {
if (rs != null) {
rs.close();
rs = null;
}
}
// 关闭数据操作对象
if (stmt != null) {
stmt.close();
stmt = null;
}
// 关闭数据连接对象
if (conn != null) {
conn.close();
conn = null;
}
} catch (RuntimeException e) {
throw e;
}
}

/**
* 功能:关闭结果集存储过程执行对象以及数据连接对象
*
* @param rs
* @param callStmt
* @throws Exception
*/
public static void close(ResultSet rs, CallableStatement callStmt)
throws Exception {
try {
// 关闭结果集
if (rs != null) {
rs.close();
rs = null;
}
if (callStmt != null) {
callStmt.close();
callStmt = null;
}
// 关闭数据连接对象
if (conn != null) {
conn.close();
conn = null;
}
} catch (RuntimeException e) {
throw e;
}
}

/**
* 功能:获取存储过程执行对象CallableStatement
* @param sql
* @return
* @throws Exception
*/
public static CallableStatement getCallableStatement(String sql)
throws Exception {
try {
getConn();
callStmt = conn.prepareCall(sql);
return callStmt;
} catch (Exception e) {
throw e;
}
}

/**
*

功能:获取PreparedStatement
*
* @param preSql
* @return
* @throws Exception
*/
public static PreparedStatement getPreparedStatedment(String preSql)
throws Exception {
try {
// 获取连接
getConn();
// 创建PreparedStatement
PreparedStatement preStmt = conn.prepareStatement(preSql);
// 返回PreparedStatement
return preStmt;
} catch (Exception e) {
throw e;
}
}

/**
* 功能:关闭结果集、预处理语句执行对象
*
*/
public static void close(ResultSet rs, PreparedStatement preStmt)
throws Exception {
try {
// 关闭结果集
if (rs != null) {
rs.close();
rs = null;
}
// 关闭PreparedStatement
if (preStmt != null) {
preStmt.close();
preStmt = null;
}
// 关闭数据链接对象
if (conn != null) {
conn.close();
conn = null;
}
} catch (Exception e) {
throw e;
}
}

/**
* 功能:执行查询操作
*
* @param sql
* @return
* @throws Exception
*/
public static ResultSet execQuery(String sql) throws Exception {
try {
// 获取数据库连接
getConn();
// 创建语句获取操作对象
stmt = conn.createStatement();
// 执行查询语句
rs = stmt.executeQuery(sql);
// 返回查询结果
return rs;
} catch (RuntimeException e) {
throw e;
}
}

/**
* 功能:执行增、删、改操作
*
* @param sql
* @return
* @throws Exception
*/
public static boolean execUpdate(String sql) throws Exception {
try {
// 获取数据库连接
getConn();
// 获取操作对象
stmt = conn.createStatement();
// 执行sql语句
int rs = stmt.executeUpdate(sql);
// 返回执行情况
return rs > 0;
} catch (Exception e) {
throw e;
} finally {
// 关闭数据库
close(false);
}
}

}


下载文档原格式

  / 2
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。