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);
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Java Web开发详解:XML+DTD+XML Schema+XSLT+Servlet3.0+JSP2.2深入剖析与实例应用/...
共614个文件
java:144个
class:123个
xml:102个
5星 · 超过95%的资源 需积分: 16 531 下载量 138 浏览量
2013-03-06
11:36:39
上传
评论 6
收藏 3.31MB RAR 举报
温馨提示
鑫编写的《Java Web开发详解——XML+DTD+XML Schema+XSLT+Servlet3.0+JSP2.2深入剖析与实例应用》共分4篇,从XML、XML Schema、XSLT、Servlet、JSP和应用的角度向读者展示了Java Web开发中各种技术的应用,循序渐进地引导读者快速掌握Java Web开发。《Java Web开发详解——XML+DTD+XML Schema+XSLT+Servlet3.0+JSP2.2深入剖析与实例应用》内容全面,涵盖了从事Java Web开发所应掌握的所有知识,同时还讲解了最新的Servlet 3.0和JSP 2.2规范新增的特性。在知识的讲解上,《Java Web开发详解——XML+DTD+XML Schema+XSLT+Servlet3.0+JSP2.2深入剖析与实例应用》采用理论与实践相结合的方式,从程序运行的内部机制进行分析讲解,并通过大量的实例和实验来验证并运用本书的知识。 《Java Web开发详解——XML+DTD+XML Schema+XSLT+Servlet3.0+JSP2.2深入剖析与实例应用》语言生动、通俗易懂、讲解细致,大部分章节都提供了多个例子,而且很多例子都是目前Web开发中经常使用的功能,具有相当高的实用价值。《Java Web开发详解——XML+DTD+XML Schema+XSLT+Servlet3.0+JSP2.2深入剖析与实例应用》不仅可以作为Java Web开发的学习用书,还可以作为从事Java Web开发的程序员的参考用书和必备手册。 此外,《Java Web开发详解——XML+DTD+XML Schema+XSLT+Servlet3.0+JSP2.2深入剖析与实例应用》的配套光盘
资源推荐
资源详情
资源评论
收起资源包目录
Java Web开发详解:XML+DTD+XML Schema+XSLT+Servlet3.0+JSP2.2深入剖析与实例应用/孙鑫编著 (614个子文件)
GuestbookDB.class 6KB
DonwloadServlet.class 6KB
ControllerServlet.class 6KB
AdminDisplayMessageProcessor.class 5KB
XsltServlet.class 5KB
ListServlet.class 5KB
BookDBBean.class 5KB
TradeServlet.class 5KB
RegisterProcessor.class 5KB
GetDBInfoServlet.class 5KB
ImageHandlerServlet.class 5KB
DisplayMessageProcessor.class 5KB
GetDBInfoServlet2.class 5KB
RegisterPageParser.class 4KB
UploadServlet.class 4KB
DBExceptionServlet.class 4KB
RandomCodeServlet.class 3KB
RandomCodeServlet.class 3KB
Sax2Xml.class 3KB
TokenProcessor.class 3KB
TokenProcessor.class 3KB
SayMessageProcessor.class 3KB
LogonFilter.class 3KB
GuestbookFilter.class 3KB
CreateAccountServlet.class 3KB
AdminLogonProcessor.class 3KB
CreateDBServlet.class 3KB
LogonProcessor.class 3KB
ImageHandlerServlet.class 3KB
ImageHandlerServlet.class 3KB
DBExceptionServlet.class 3KB
FibonacciServlet.class 3KB
RandomCodeServlet.class 3KB
LoginServlet2.class 2KB
OnlineUserServlet.class 2KB
OutputInfoServlet.class 2KB
TokenProcessor.class 2KB
DOMPrinter.class 2KB
LoginServlet.class 2KB
AdminDeleteMessageProcessor.class 2KB
GreetServlet2.class 2KB
ControllerServlet.class 2KB
HandlerServlet.class 2KB
HandlerServlet.class 2KB
ImageHandlerServlet.class 2KB
MyServletContainerInitializer.class 2KB
MyServletContainerInitializer.class 2KB
CartBean.class 2KB
LoginCheckServlet.class 2KB
LoginCheckServlet.class 2KB
GreetServlet.class 2KB
ErrorProcessor.class 2KB
LogoutServlet2.class 2KB
FibonacciServlet$FibonacciTask.class 2KB
JDOMConvert.class 2KB
FibonacciServlet$1.class 2KB
SetLocaleFilter.class 2KB
HandlerServlet.class 2KB
CounterServlet.class 2KB
SetCharacterEncodingFilter.class 2KB
PortalServlet.class 2KB
BookBean.class 2KB
HttpErrorHandlerServlet.class 2KB
SuccessProcessor.class 2KB
OutputSessionInfo.class 2KB
CompressionFilter.class 1KB
IterateTag.class 1KB
WelcomeServlet.class 1KB
LoginCheckServlet.class 1KB
LogoutServlet.class 1KB
MaxExSimpleTag.class 1KB
User.class 1KB
MyServletContextListener.class 1KB
ExceptionHandlerServlet2.class 1KB
PicZoom.class 1KB
PicZoom.class 1KB
HelloTag.class 1KB
WelcomeServlet.class 1KB
LogoutServlet.class 1KB
LoginServlet2.class 1KB
LoginChkServlet.class 1KB
CharacterConvert.class 1KB
MyRequestWrapper.class 1KB
LoginServlet.class 1KB
Message.class 1KB
MyServletContextListener.class 1KB
CompressionResponseWrapper.class 1KB
UserList.class 1KB
GreetTag.class 1KB
ExceptionServlet.class 1KB
SimpleFilter.class 1KB
HelloWorldServlet.class 1KB
ExceptionHandlerServlet.class 1KB
UserBean.class 1KB
WelcomeSimpleTag.class 1KB
MyResponseWrapper.class 1023B
TestServlet.class 1018B
MaxTag.class 1017B
CartItemBean.class 987B
Test.class 955B
共 614 条
- 1
- 2
- 3
- 4
- 5
- 6
- 7
acm365
- 粉丝: 91
- 资源: 71
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页