资源简介

用JSP、Servlet和JavaBean开发的一个简单的留言系统

资源截图

代码片段和文件信息

package db;
import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import model.LyTable;
import model.User;
public class DB {
Connection ct;
PreparedStatement pstmt;
// 在构造函数中建立与数据库的连接,这样在建立DB对象时就连接了数据库
public DB(){
try {
/*
Class.forName(“com.microsoft.sqlserver.jdbc.SQLServerDriver“);
ct=DriverManager.getConnection
(“jdbc:sqlserver://localhost:1433;databaseName=javaee““sa““123456“);
*/
Class.forName(“org.gjt.mm.mysql.Driver“);
ct=DriverManager.getConnection
(“jdbc:mysql://127.0.0.1:3306/jsp?autoReconnect=true&useUnicode=true&characterEncoding=gbk““root““123456“);
} catch (Exception e) {
e.printStackTrace();
}
}
// 根据username和password查询用户,查到就返回该对象,没有就返回null
public User checkUser(String usernameString password){
try{
pstmt=ct.prepareStatement(“select * from userTable where username=? and password=?“);
pstmt.setString(1 username);
pstmt.setString(2 password);
ResultSet rs=pstmt.executeQuery();
User user=new User();
while(rs.next()){
user.setId(rs.getInt(1));
user.setUsername(rs.getString(2));
user.setPassword(rs.getString(3));
return user;
}
return null;
}catch(Exception e){
e.printStackTrace();
return null;
}
}
// 根据userId得到用户姓名
public String  getUserName(int userId){
try{
pstmt=ct.prepareStatement(“select * from userTable where id=?“);
pstmt.setInt(1 userId);
ResultSet rs=pstmt.executeQuery();
while(rs.next()){
return rs.getString(“username“);
}
return null;
}catch(Exception e){
e.printStackTrace();
return null;
}
}
// 查询留言信息,返回一个ArrayList
public ArrayList findLyInfo(){
try{
ArrayList al=new ArrayList();
pstmt=ct.prepareStatement(“select * from lyTable“);
ResultSet rs=pstmt.executeQuery();
while(rs.next()){
LyTable ly=new LyTable();
ly.setId(rs.getInt(1));
ly.setUserId(rs.getInt(2));
ly.setDate(rs.getDate(3));
ly.settitle(rs.getString(4));
ly.setContent(rs.getString(5));
al.add(ly);
}
return al;
}catch(Exception e){
e.printStackTrace();
return null;
}
}
//增加留言
public boolean addInfo(LyTable ly){
try {
pstmt=ct.prepareStatement(“insert into lyTable(useriddatetitlecontent) values(????)“);
pstmt.setInt(1 ly.getUserId());
pstmt.setDate(2 ly.getDate());
pstmt.setString(3 ly.gettitle());
pstmt.setString(4 ly.getContent());
pstmt.executeUpdate();
return true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}

}
//增加用户
public boolean insertUser(String usernameString pwd){
try {
pstmt=ct.prepareStatement(“insert into userTable(usernamepassword) values(??)“);
pstmt.setString(1 username);
pstmt.setString(2 pwd);
pstmt.executeUpdate();
return true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.p

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        445  2011-05-29 21:31  Jspservlet\.classpath

     文件        303  2011-05-29 20:56  Jspservlet\.mymetadata

     文件       1283  2011-05-29 21:06  Jspservlet\.project

     文件        330  2011-05-29 20:56  Jspservlet\.settings\org.eclipse.jdt.core.prefs

     文件       3137  2011-06-02 19:12  Jspservlet\src\db\DB.java

     文件        735  2011-05-29 20:57  Jspservlet\src\model\LyTable.java

     文件        472  2011-05-29 20:58  Jspservlet\src\model\User.java

     文件       1344  2011-05-29 21:45  Jspservlet\src\servlet\AddServlet.java

     文件       1912  2011-05-29 21:42  Jspservlet\src\servlet\MainServlet.java

     文件        870  2011-05-29 21:51  Jspservlet\src\servlet\RegisterServlet.java

     文件        834  2011-05-29 20:56  Jspservlet\WebRoot\index.jsp

     文件        638  2011-06-02 15:44  Jspservlet\WebRoot\liuyan.jsp

     文件        594  2011-05-29 21:01  Jspservlet\WebRoot\login.jsp

     文件        906  2011-05-29 21:44  Jspservlet\WebRoot\main.jsp

     文件         36  2011-05-29 20:56  Jspservlet\WebRoot\meta-INF\MANIFEST.MF

     文件        523  2011-05-29 21:50  Jspservlet\WebRoot\register.jsp

     文件        222  2011-05-29 21:46  Jspservlet\WebRoot\success.jsp

     文件       3660  2011-06-02 19:12  Jspservlet\WebRoot\WEB-INF\classes\db\DB.class

     文件       1324  2011-05-29 21:31  Jspservlet\WebRoot\WEB-INF\classes\model\LyTable.class

     文件        898  2011-05-29 21:31  Jspservlet\WebRoot\WEB-INF\classes\model\User.class

     文件       1851  2011-05-29 21:45  Jspservlet\WebRoot\WEB-INF\classes\servlet\AddServlet.class

     文件       1871  2011-05-29 21:42  Jspservlet\WebRoot\WEB-INF\classes\servlet\MainServlet.class

     文件       1270  2011-05-29 21:51  Jspservlet\WebRoot\WEB-INF\classes\servlet\RegisterServlet.class

     文件       1211  2011-06-02 19:07  Jspservlet\WebRoot\WEB-INF\web.xml

     目录          0  2011-05-29 21:31  Jspservlet\WebRoot\WEB-INF\classes\db

     目录          0  2011-05-29 21:31  Jspservlet\WebRoot\WEB-INF\classes\model

     目录          0  2011-05-29 21:51  Jspservlet\WebRoot\WEB-INF\classes\servlet

     目录          0  2011-05-29 21:38  Jspservlet\WebRoot\WEB-INF\classes

     目录          0  2011-05-29 20:56  Jspservlet\WebRoot\WEB-INF\lib

     目录          0  2011-05-29 21:08  Jspservlet\src\db

............此处省略12个文件信息

评论

共有 条评论