package org.sunxin.guestbook.beans;
import java.io.*;
import java.sql.*;
import javax.naming.*;
import javax.sql.*;
import org.sunxin.guestbook.*;
public class GuestbookDB implements Serializable
{
private DataSource ds=null;
public GuestbookDB()
{
try
{
Context ctx = new InitialContext();
ds = (DataSource) ctx.lookup("java:comp/env/jdbc/guestbook");
}
catch (NamingException ne)
{
System.err.println(ne.toString());
}
}
/**
* 得到数据库连接。
*/
public Connection getConnection() throws SQLException
{
return ds.getConnection();
}
/**
* 关闭连接对象。
*/
protected void closeConnection(Connection conn)
{
if(conn!=null)
{
try
{
conn.close();
conn=null;
}
catch (SQLException ex)
{
System.err.println(ex.toString());
}
}
}
/**
* 关闭Statement对象。
*/
protected void closeStatement(Statement stmt)
{
if(stmt!=null)
{
try
{
stmt.close();
stmt=null;
}
catch (SQLException ex)
{
System.err.println(ex.toString());
}
}
}
/**
* 关闭PreparedStatement对象。
*/
protected void closePreparedStatement(PreparedStatement pstmt)
{
if(pstmt!=null)
{
try
{
pstmt.close();
pstmt=null;
}
catch (SQLException ex)
{
System.err.println(ex.toString());
}
}
}
/**
* 关闭ResultSet对象。
*/
protected void closeResultSet(ResultSet rs)
{
if(rs!=null)
{
try
{
rs.close();
rs=null;
}
catch (SQLException ex)
{
System.err.println(ex.toString());
}
}
}
/**
* 注册用户。
*/
public void registerUser(User user) throws UserException
{
Connection conn=null;
ResultSet rs=null;
PreparedStatement pstmt=null;
try
{
conn = getConnection();
pstmt = conn.prepareStatement(
"select username from users where username=?");
pstmt.setString(1,user.getUsername());
rs=pstmt.executeQuery();
if(rs.next())
throw new UserException("用户名已经存在");
pstmt = conn.prepareStatement(
"insert into users values(?,?,?,?)");
pstmt.setString(1, user.getUsername());
pstmt.setString(2, user.getPassword());
pstmt.setString(3, user.getEmail());
pstmt.setString(4, user.getStyle());
pstmt.executeUpdate();
}
catch(SQLException se)
{
System.err.println(se.toString());
throw new UserException("用户注册失败!");
}
finally
{
closeResultSet(rs);
closePreparedStatement(pstmt);
closeConnection(conn);
}
}
/**
* 对登录用户进行验证。
*/
public String validateUser(User user) throws UserException
{
Connection conn=null;
ResultSet rs = null;
PreparedStatement pstmt = null;
try
{
conn = getConnection();
pstmt = conn.prepareStatement(
"select style from users where username=? and password=?");
pstmt.setString(1, user.getUsername());
pstmt.setString(2, user.getPassword());
rs = pstmt.executeQuery();
if (rs.next())
return rs.getString(1);
else
return "";
}
catch (SQLException se)
{
System.err.println(se.toString());
throw new UserException("服务器忙,无法验证用户,请联系管理员。");
}
finally
{
closeResultSet(rs);
closePreparedStatement(pstmt);
closeConnection(conn);
}
}
/**
* 保存留言。
*/
public void saveMessage(Message msg) throws GuestbookException
{
Connection conn = null;
PreparedStatement pstmt = null;
try
{
conn = getConnection();
pstmt = conn.prepareStatement(
"insert into gst(gst_user,gst_title,gst_content,gst_ip) values(?,?,?,?)");
pstmt.setString(1, msg.getUsername());
pstmt.setString(2, msg.getTitle());
pstmt.setString(3, msg.getContent());
pstmt.setString(4, msg.getIp());
pstmt.executeUpdate();
}
catch (SQLException se)
{
System.err.println(se.toString());
throw new GuestbookException("保存留言失败!");
}
finally
{
closePreparedStatement(pstmt);
closeConnection(conn);
}
}
/**
* 获取所有留言。
* @return ResultSet
*/
public ResultSet getMessages() throws GuestbookException
{
Connection conn = null;
Statement stmt = null;
ResultSet rs=null;
try
{
conn = getConnection();
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery("select * from gst order by gst_time desc");
return rs;
}
catch (SQLException se)
{
System.err.println(se.toString());
throw new GuestbookException("获取留言操作失败,请联系管理员。");
}
}
/**
* 对管理员登录进行验证。
*/
public boolean validateManager(User user) throws UserException
{
Connection conn=null;
ResultSet rs = null;
PreparedStatement pstmt = null;
try
{
conn = getConnection();
pstmt = conn.prepareStatement(
"select username from managers where username=? and password=?");
pstmt.setString(1, user.getUsername());
pstmt.setString(2, user.getPassword());
rs = pstmt.executeQuery();
if (rs.next())
return true;
else
return false;
}
catch (SQLException se)
{
System.err.println(se.toString());
throw new UserException("服务器忙,无法验证用户。");
}
finally
{
closeResultSet(rs);
closePreparedStatement(pstmt);
closeConnection(conn);
}
}
/**
* 删除指定的留言。
*/
public void deleteMessage(int id) throws GuestbookException
{
Connection conn = null;
PreparedStatement pstmt = null;
try
{
conn = getConnection();
pstmt = conn.prepareStatement(
"delete from gst where gst_id = ?");
pstmt.setInt(1, id);
pstmt.executeUpdate();
}
catch (SQLException se)
{
System.err.println(se.toString());
throw new GuestbookException("删除留言失败!");
}
finally
{
closePreparedStatement(pstmt);
closeConnection(conn);
}
}
}

acm365
- 粉丝: 91
最新资源
- 高速公路通信系统集成应用.doc
- 高速走丝气中电火花线切割精加工编程设计学士学位论文.doc
- 高职院校图书馆特色服务研究与实践-基于网络的数字化参考咨询服务研究与实践学院课题课题研究结题报告-毕设.doc
- 黑龙江垦丰种业基础网络与安全项目全新实施方案.docx
- 黑龙江省村城电子商务有限公司营销策略与改进本科毕业论文.doc
- 鹿木乡中学网站设计与开发学士学位论文.doc
- 麦克士在中国营销战略浅析电子商务毕业(设计)论文.doc
- 魅力的个人网站毕业(论文)设计论文.doc
- 餐饮行业网站建设方案.doc
- 餐饮旅游资讯网站的设计与实现毕业设计(论文)说明书.doc
- 风扇摇头机构--机械设计制造及其自动化机械原理课程设计任务书.doc
- 飞行器实时仿真算法研究大学生-毕设论文.doc
- 高级宾馆综合布线课程设计--大型综合布线系统设计.doc
- 高科技产业孵化中心及软件实训研发中心建设项目可行性研究报告.doc
- 高楼大厦gsm网络信号的优化实现毕业(论文)设计论文.doc
- 高楼大厦GSM网络信号的优化实现毕业设计论文.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



- 1
- 2
- 3
- 4
- 5
- 6
前往页