package Dao;
import java.sql.*;
public class BaseDao {
// 建立驱动器
private static final String DRIVER_CLASS = com.microsoft.sqlserver.jdbc.SQLServerDriver.class.getName();
// 数据库连接地址 1433
private static final String URL = "jdbc:sqlserver://localhost:1433;DataBaseName=exam";
// 用户名
private static final String DATABASE_USER = "sa";
// 密码
private static final String DATABASE_PASSWORD = "sa";
/**
* 得到数据库连接
*/
public Connection getConn() throws ClassNotFoundException, SQLException {
Class.forName(DRIVER_CLASS); //注册驱动
Connection con = DriverManager.getConnection(URL, DATABASE_USER,DATABASE_PASSWORD);
return con; //返回连接
}
/**
* 释放资源
*
* @throws SQLException
*/
public void closeAll(Connection conn, PreparedStatement pstmt, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 执行SQL语句,可以进行增、删、改的操作,不能执行查询
*
* @throws SQLException
* @throws SQLException
* @throws ClassNotFoundException
*/
public int executeSQL(String preparedSql, String[] param) {
Connection conn = null;
PreparedStatement pstmt = null;
int num = 0;
// 处理SQL,执行SQL
try {
conn = this.getConn();
pstmt = conn.prepareStatement(preparedSql);
if (param != null) {
for (int i = 0; i < param.length; i++) {
pstmt.setString(i + 1, param[i]);//设置参数
}
}
num = pstmt.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.closeAll(conn, pstmt, null);
}
return num;
}
}
ACCP5.0 S2结业考试机试题.rar
需积分: 9 143 浏览量
2009-08-13
15:34:40
上传
评论
收藏 741KB RAR 举报
LinChangPeng9
- 粉丝: 2
- 资源: 6
最新资源
- 服务器概述服务器概述服务器概述服务器概述.txt
- 华中农业大学python实验题.txt
- 海康威视相机采图交叉编译示例程序,c++
- DETR-基于Tensorflow实现DETR目标检测算法-附流程教程+项目源码-优质项目实战.zip
- 3d激光slam地图发布程序,3d地图点云处理,c++程序
- 送给妈妈的一束鲜花.zip(母亲节祝福HTML源码)
- 稀疏化DETR-基于Pytorch实现稀疏化DETR-SparseDETR-附流程教程+项目源码-优质项目实战.zip
- 人工分类:SLTM的微博评论二分类数据集
- (自适应手机端)响应式房产合同知识产权网站pbootcms模板 企业管理类网站源码下载.zip
- (自适应手机端)响应式动力刀座pbootcms网站模板 五金机械设备类网站源码下载.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈