package org.news.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.news.util.ConfigManager;
/**
* 数据库连接与关闭工具类。
*
* @author 北大青鸟
*/
public class BaseDao {
private static String driver = ConfigManager.getProperty("driver");// 数据库驱动字符串
private static String url = ConfigManager.getProperty("url");// 连接URL字符串
private static String user = ConfigManager.getProperty("user"); // 数据库用户名
private static String password = ConfigManager.getProperty("password"); // 用户密码
protected Connection conn;
static {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 获取数据库连接对象。
*/
public Connection getConnection() {
// 获取连接并捕获异常
try {
if (conn == null || conn.isClosed())
conn = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;// 返回连接对象
}
/**
* 关闭数据库连接。
*
* @param conn
* 数据库连接
* @param stmt
* Statement对象
* @param rs
* 结果集
*/
public void closeAll(Connection conn, Statement stmt, ResultSet rs) {
// 若结果集对象不为空,则关闭
try {
if (rs != null && !rs.isClosed())
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
// 若Statement对象不为空,则关闭
try {
if (stmt != null && !stmt.isClosed())
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
// 若数据库连接对象不为空,则关闭
try {
if (conn != null && !conn.isClosed())
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 增、删、改操作
*
* @param sql
* sql语句
* @param prams
* 参数数组
* @return 执行结果
*/
protected int executeUpdate(String sql, Object... params) {
int result = 0;
conn = this.getConnection();
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
pstmt.setObject(i + 1, params[i]);
}
result = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeAll(null, pstmt, null);
}
return result;
}
/**
* 查询操作
*
* @param sql
* sql语句
* @param params
* 参数数组
* @return 查询结果集
*/
protected ResultSet executeQuery(String sql, Object... params) {
conn = this.getConnection();
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = conn.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
pstmt.setObject(i + 1, params[i]);
}
rs = pstmt.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
news_chapter04.rar (90个子文件)
news_chapter04
test
org
news
test
resources
database.properties 242B
log4j.properties 504B
.settings
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.project.facet.core.xml 252B
org.eclipse.wst.common.component 625B
org.eclipse.wst.jsdt.ui.superType.container 49B
.jsdtscope 500B
org.eclipse.jdt.core.prefs 364B
src
org
news
dao
TopicsDao.java 452B
BaseDao.java 4KB
UserDao.java 174B
NewsDao.java 334B
impl
NewsDaoImpl.java 3KB
TopicsDaoImpl.java 3KB
UserDaoImpl.java 948B
entity
User.java 629B
Topic.java 493B
Comment.java 1KB
News.java 2KB
util
ConfigManager.java 942B
.project 2KB
WebRoot
css
read.css 3KB
admin.css 3KB
main.css 3KB
images
logo1.jpg 2KB
class_type.gif 4KB
friend_t.gif 189B
Picture3.jpg 31KB
nav_leftline.gif 53B
banner.jpg 20KB
friend_ico.gif 3KB
friend_logo.gif 1KB
left.jpg 6KB
topbg.gif 195B
admin_bar.gif 191B
opt_name.gif 3KB
nav_leftdot.gif 82B
title_2.gif 3KB
title_3.gif 4KB
arrow.gif 50B
title_1.gif 4KB
login_sub.gif 987B
head.gif 40KB
logo.jpg 14KB
Picture4.jpg 47KB
Picture1.jpg 51KB
blue_bar.png 942B
login_input.gif 504B
class_bg.gif 6KB
opt_sub.gif 180B
Picture2.jpg 50KB
a_b01.gif 40KB
channel_bg.gif 1KB
main-1.gif 290KB
sidebarbg.gif 5KB
firend_bg.gif 343B
index-elements
index_rightbar.html 538B
index_bottom.html 964B
META-INF
MANIFEST.MF 36B
WEB-INF
web.xml 404B
classes
database.properties 242B
log4j.properties 504B
org
news
dao
UserDao.class 190B
BaseDao.class 3KB
NewsDao.class 317B
TopicsDao.class 417B
impl
UserDaoImpl.class 1KB
NewsDaoImpl.class 3KB
TopicsDaoImpl.class 3KB
test
entity
User.class 992B
Comment.class 2KB
News.class 3KB
Topic.class 813B
util
ConfigManager.class 1KB
lib
mysql-connector-java-5.1.0-bin.jar 553KB
index.jsp 8KB
newspages
console_element
top.jsp 981B
left.html 326B
bottom.html 895B
topic_list.jsp 1KB
news_add.jsp 3KB
admin.jsp 2KB
topic_modify.jsp 1KB
news_read.jsp 7KB
topic_add.jsp 2KB
util
do_news_list.jsp 371B
do_login.jsp 685B
topic_control.jsp 1KB
.mymetadata 305B
.classpath 877B
.myeclipse
共 90 条
- 1
资源评论
乔吕吕
- 粉丝: 1
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功