首页 新闻 会员 周边 捐助

求助:用MVC做登录加留言

0
悬赏园豆:50 [待解决问题]

    本人是信管专业的,最近学的JAVA要做个小作业。由于我学的不深,所以选择用MVC完成一个可以登录且可以留言并可以显示留言的网页。但是在JAVA部分老是出错,现在怎么找也找不到错误,所以跑来求助大伙了!本人第一次发帖求助,希望有人来帮忙~很急啊~~在这里先谢谢大家了!!

    下面是我的源代码,还有编译的错误,大家帮忙看看是怎么回事啊~~

Partner.java

 1 package my.big.vo ;
 2 import java.util.* ;
 3 public class Partner {
 4     private String pname ;
 5     private String pid ;
 6     private String call ;
 7     private String phone ;
 8     private String qq ;
 9     private List errors ;
10 
11     public String getPname() {
12         return this.pname ;
13     }
14     public String getPid() {
15         return this.pid ;
16     }
17     public String getCall() {
18         return this.call ;
19     }
20     public String getPhone() {
21         return this.phone ;
22     }
23     public String getQq() {
24         return this.qq ;
25     }
26     public List getErrors() {
27         return this.errors ;
28     }
29 
30     public void setPname(String pname) {
31         this.pname = pname ;
32     }
33     public void setPid(String pid) {
34         this.pid = pid ;
35     }
36     public void setCall(String call) {
37         this.call = call ;
38     }
39     public void setPhone(String phone) {
40         this.phone = phone ;
41     }
42     public void setQq(String qq) {
43         this.qq = qq ;
44     }
45     public void setErrors(List errors) {
46         this.errors = errors ;
47     }
48     
49     public boolean isEmpty() {   
50        boolean iserror=false;
51         if(this.pid==null || "".equals(this.pid)) {
52              
53            iserror=true;
54             this.errors.add("用户名不能为空!");
55         }
56         if(this.qq==null || "".equals(this.qq)) {
57               
58             iserror=true;
59             this.errors.add("密码不能为空!");
60         }
61         return iserror;
62     }
63 }

 

Message.java

 1 package my.big.vo ;
 2 import java.util.* ;
 3 public class Message {
 4     private String pname ;
 5     private String pubdate ;
 6     private String content ;
 7     private List errors ;
 8 
 9     public String getPname() {
10         return this.pname ;
11     }
12     public String getPubdate() {
13         return this.pubdate ;
14     }
15     public String getContent() {
16         return this.content ;
17     }
18     public List getErrors() {
19         return this.errors ;
20     }
21 
22     public void setPname(String pname) {
23         this.pname = pname ;
24     }
25     public void setPubdate(String pubdate) {
26         this.pubdate = pubdate ;
27     }
28     public void setContent(String content) {
29         this.content = content ;
30     }
31     public void setErrors(List errors) {
32         this.errors = errors ;
33     }
34     
35     public boolean isEmpty() {   
36        boolean iserror=false;
37         if(this.content==null || "".equals(this.content)) {
38            iserror=true;
39            this.errors.add("内容不能为空!");
40         }
41         return iserror;
42     }
43 }

 

DatabaseConnection.java

 1 package my.big.dbc ;
 2 import java.sql.* ;
 3 public class DatabaseConnection{
 4     private static final String DBDRIVER = "com.microsoft.jdbc.sqlserver.SQLServerDriver" ;
 5     private static final String DBURL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=bcgm" ;
 6     private static final String DBUSER = "sa" ;
 7     private static final String DBPASSWORD = "woaiputao" ;
 8     private Connection conn  ;
 9 
10     public DatabaseConnection() throws Exception {
11         try
12         {
13             Class.forName(DBDRIVER) ;
14             this.conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ;
15         }
16         catch (Exception e)
17         {
18             throw e ;
19         }
20     }
21 
22     public Connection getConnection(){
23         return this.conn ;
24     }
25 
26     public void close() throws Exception {
27         if (this.conn != null)
28         {
29             try
30             {
31                 this.conn.close() ;
32             }
33             catch (Exception e)
34             {
35                 throw e ;
36             }
37         }
38     }
39 }

 

IPartnerDAO.java

1 package my.big.dao ;
2 import java.util.* ;
3 import my.big.vo.* ;
4 public interface IPartnerDAO {
5 
6     public boolean findLogin(Partner partner) throws Exception ;
7 
8     public List<Partner> findAll(String keyWord) throws Exception ;
9 }

 

IMessageDAO.java

1 package my.big.dao ;
2 import java.util.* ;
3 import my.big.vo.* ;
4 public interface IMessageDAO {
5 
6     public boolean addMessage(Message message) throws Exception ;
7 
8 }

 

PartnerDAOImpl.java

 1 package my.big.dao.impl ;
 2 import java.util.* ;
 3 import java.sql.* ;
 4 import my.big.dao.* ;
 5 import my.big.vo.* ;
 6 public class PartnerDAOImpl implements IPartnerDAO {
 7     private Connection conn = null ;
 8     private PreparedStatement pstmt = null ;
 9     
10     public PartnerDAOImpl(Connection conn) {
11         this.conn = conn ;
12     }
13 
14     public boolean findLogin(Partner partner) throws Exception {
15         boolean flag = false ;
16         try{
17                 String sql = "SELECT call FROM partner WHERE pid=? AND qq=?" ;
18                 this.pstmt = this.conn.prepareStatement(sql) ;
19                 this.pstmt.setString(1,partner.getPid()) ;
20                 this.pstmt.setString(2,partner.getQq()) ;
21                 ResultSet rs = this.pstmt.executeQuery() ;
22                 if (rs.next()) {
23                     partner.setCall(rs.getString(1)) ;
24                     flag = true ;
25                 }
26                 if (this.pstmt != null) {
27                     this.pstmt.close() ;
28                 }
29             }catch (Exception e){
30                 throw e ;
31             }
32 
33         return flag ;
34     }
35 
36     public List<Partner> findAll(String keyWord) throws Exception{
37         List<Partner> all = new ArrayList<Partner>() ;
38         try{
39                 String sql = "SELECT pname,pid,call,phone,qq FROM partner WHERE pid LIKE ? OR qq LIKE ?" ;
40                 this.pstmt = this.conn.prepareStatement(sql) ;
41                 this.pstmt.setString(1,"%" + keyWord + "%") ;
42                 this.pstmt.setString(2,"%" + keyWord + "%") ;
43                 ResultSet rs = this.pstmt.executeQuery() ;
44                 Partner partner = null ;
45                 while (rs.next())
46                 {
47                     partner = new Partner() ;
48                     partner.setPname(rs.getString(1)) ;
49                     partner.setPid(rs.getString(2)) ;
50                     partner.setCall(rs.getString(3)) ;
51                     partner.setPhone(rs.getString(4)) ;
52                     partner.setQq(rs.getString(5)) ;
53                     all.add(partner) ;
54                 }
55                 this.pstmt.close() ;
56             }catch (Exception e){
57                 throw e ;
58             }
59 
60         return all ;
61     }
62 }

 

MessageDAOImpl.java

 1 package my.big.dao.impl ;
 2 import java.util.* ;
 3 import java.sql.* ;
 4 import my.big.dao.* ;
 5 import my.big.vo.* ;
 6 public class MessageDAOImpl implements IMessageDAO {
 7     private Connection conn = null ;
 8     private PreparedStatement pstmt = null ;
 9     
10     public MessageDAOImpl(Connection conn) {
11         this.conn = conn ;
12     }
13 
14     public boolean addMessage(Message message) throws Exception {
15         boolean flag = false ;
16         try{
17                 String sql = "SELECT pname FROM message WHERE pubdate=? AND content=?" ;
18                 this.pstmt = this.conn.prepareStatement(sql) ;
19                 this.pstmt.setString(1,message.getPubdate()) ;
20                 this.pstmt.setString(2,message.getContent()) ;
21                 ResultSet rs = this.pstmt.executeQuery() ;
22                 if (rs.next()) {
23                     message.setPubdate(rs.getString(1)) ;
24                     flag = true ;
25                 }
26                 if (this.pstmt != null) {
27                     this.pstmt.close() ;
28                 }
29             }catch (Exception e){
30                 throw e ;
31             }
32 
33         return flag ;
34     }
35 }

 

PartnerDAOProxy.java

 1 package my.big.dao.proxy ;
 2 import java.util.* ;
 3 import my.big.dao.* ;
 4 import my.big.dao.impl.* ;
 5 import my.big.dbc.* ;
 6 import my.big.vo.* ;
 7 public class PartnerDAOProxy implements IPartnerDAO {
 8     private DatabaseConnection dbc = null ;
 9     private IPartnerDAO dao = null ;
10     public PartnerDAOProxy(){
11         try
12         {
13             this.dbc = new DatabaseConnection() ;
14         }
15         catch (Exception e)
16         {
17             e.printStackTrace() ;
18         }
19         this.dao = new PartnerDAOImpl(this.dbc.getConnection()) ;
20     }
21 
22     public boolean findLogin(Partner partner) throws Exception {
23         boolean flag = false ;
24         try
25         {
26             flag = this.dao.findLogin(partner) ;
27         }
28         catch (Exception e)
29         {
30             throw e ;
31         } finally {
32             this.dbc.close() ;
33         }
34         return flag ;
35     }
36 
37     public List<Partner> findAll(String keyWord) throws Exception{
38         List<Partner> all = null ;
39         try
40         {
41             all = this.dao.findAll(keyWord) ;
42         }
43         catch (Exception e)
44         {
45             throw e ;
46         }
47         finally
48         {
49             this.dbc.close() ;
50         }
51 
52         return all ;
53     }
54 }

 

MessageDAOProxy.java

 1 package my.big.dao.proxy ;
 2 import java.util.* ;
 3 import my.big.dao.* ;
 4 import my.big.dao.impl.* ;
 5 import my.big.dbc.* ;
 6 import my.big.vo.* ;
 7 public class MessageDAOProxy implements IMessageDAO {
 8     private DatabaseConnection dbc = null ;
 9     private IMessageDAO dao = null ;
10     public MessageDAOProxy(){
11         try
12         {
13             this.dbc = new DatabaseConnection() ;
14         }
15         catch (Exception e)
16         {
17             e.printStackTrace() ;
18         }
19         this.dao = new MessageDAOImpl(this.dbc.getConnection()) ;
20     }
21 
22     public boolean addMessage(Message message) throws Exception {
23         boolean flag = false ;
24         try
25         {
26             flag = this.dao.addMessage(message) ;
27         }
28         catch (Exception e)
29         {
30             throw e ;
31         } finally {
32             this.dbc.close() ;
33         }
34         return flag ;
35     }
36 
37 }

 

DAOFactory.java

 1 package my.big.factory ;
 2 import my.big.dao.* ;
 3 import my.big.dao.proxy.* ;
 4 public class DAOFactory {
 5     public static IPartnerDAO getIPartnerDAOInstance() {
 6         return new PartnerDAOProxy() ;
 7     }
 8 
 9     public static IMessageDAO getIMessageDAOInstance() {
10         return new MessageDAOProxy() ;
11     }
12 }

 

CheckPartner.java

 1 package my.big.servlet ;
 2 import java.util.* ;
 3 import java.io.* ;
 4 import javax.servlet.* ;
 5 import javax.servlet.http.* ;
 6 import my.big.factory.* ;
 7 import my.big.vo.* ;
 8 public class CheckPartner extends HttpServlet {
 9     
10     public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
11         String path = "login.jsp" ;
12         
13         String pid = request.getParameter("pid");
14         String qq = request.getParameter("qq");
15         String call = request.getParameter("call");
16         List errors=new ArrayList();
17         Partner partner=new Partner();
18         partner.setPid(pid);
19         partner.setQq(qq);
20         partner.setCall(call);
21         partner.setErrors(errors);
22 
23         if(!partner.isEmpty()) {
24             try{   
25                 if(DAOFactory.getIPartnerDAOInstance().findLogin(partner)){
26                     path="welcome.jsp";
27                     request.getSession().setAttribute("pid",partner.getPid());
28                     request.getSession().setAttribute("call",partner.getCall());
29                  }
30                  else { 
31                    errors.add("用户名或密码错误!");
32                  }
33              } catch(Exception e) {  
34                  System.out.println(e);
35              } 
36         }
37         request.setAttribute("errors", errors);
38         request.setAttribute("partner", partner);
39         request.getRequestDispatcher(path).forward(request, response);
40     }
41 
42     public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
43         this.doGet(request,response) ;
44     }
45 }

 

AddMessage.java

 1 package my.big.servlet ;
 2 import java.util.* ;
 3 import java.io.* ;
 4 import javax.servlet.* ;
 5 import javax.servlet.http.* ;
 6 import my.big.factory.* ;
 7 import my.big.vo.* ;
 8 public class AddMessage extends HttpServlet {
 9     
10     public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
11         String path = "home.jsp" ;
12         String pubdate = request.getPubdate("pubdate");
13         String content = request.getContent("content");
14         String pname = request.getPname("pname");
15         List errors=new ArrayList();
16         Message message=new Message();
17         message.setPubdate(pubdate);
18         message.setContent(content);
19         message.setPname(pname);
20         message.setErrors(errors);
21 
22         if(!message.isEmpty()) {
23             try{   
24                 if(DAOFactory.getIMessageDAOInstance().addMessage(message)){
25                     path="addtrue.jsp";
26                     request.getSession().setAttribute("pname",message.getPname());
27                     request.getSession().setAttribute("pubdate",message.getPubdate());
28                     request.getSession().setAttribute("content",message.getContent());
29                  }
30                  else { 
31                    errors.add("程序未知错误!");
32                  }
33              } catch(Exception e) {  
34                  System.out.println(e);
35              } 
36         }
37         request.setAttribute("errors", errors);
38         request.setAttribute("message", message);
39         request.getRequestDispatcher(path).forward(request, response);
40     }
41 
42     public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
43         this.doGet(request,response) ;
44     }
45 }

 

这是我出错我地方:(晕,这里怎么上传本地图片的?)

Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.

C:\Documents and Settings\本草纲目>E:

E:\>cd bcgm\web-inf\classes

E:\bcgm\WEB-INF\classes>javac -d . *.java
AddMessage.java:12: 错误: 找不到符号
String pubdate = request.getPubdate("pubdate");
^
符号: 方法 getPubdate(String)
位置: 类型为HttpServletRequest的变量 request
AddMessage.java:13: 错误: 找不到符号
String content = request.getContent("content");
^
符号: 方法 getContent(String)
位置: 类型为HttpServletRequest的变量 request
AddMessage.java:14: 错误: 找不到符号
String pname = request.getPname("pname");
^
符号: 方法 getPname(String)
位置: 类型为HttpServletRequest的变量 request
注: 某些输入文件使用了未经检查或不安全的操作。
注: 有关详细信息, 请使用 -Xlint:unchecked 重新编译。
3 个错误

 

我的JSP就先不上传了。因为上面的编译已经出错了,但是我不知道怎么修改。大家帮帮忙啊~~

顺便补充一下,我想实现的是,有一个登录页面,登录成功后转跳到主界面,主界面有类似与QQ空间发表说说的那样,可以发表说说,发表框下面是显示说说的内容,可以显示用户名时间和说说内容,用户我已经弄好了,是我宿舍的八个人。

我向请教下,我上面的代码能实现么?说说的内容是如何处理的?我对数据库不太熟悉。说说的内容是如何保存的?又是如何按照时间先后顺序显示的?大家给个思路吧~或者有空的话帮解决下~

 

补上我的数据库表:

CREATE TABLE partner(
pname VARCHAR(30),
pid VARCHAR(12) PRIMARY KEY,
call VARCHAR(20),
phone VARCHAR(11),
qq VARCHAR(10),
) ;

CREATE TABLE message(
pname VARCHAR(30),
pubdate smalldatetime PRIMARY KEY,
content ntext,
) ;

这样子可以实现吗?

谢谢大家了!!

本草纲目的主页 本草纲目 | 初学一级 | 园豆:152
提问于:2012-06-05 21:27
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册