当前位置: 代码迷 >> Java Web开发 >> 献上100积分,求解一个非常困惑的有关问题(jsp+javabean+sqlserver2008)
  详细解决方案

献上100积分,求解一个非常困惑的有关问题(jsp+javabean+sqlserver2008)

热度:332   发布时间:2016-04-17 00:09:30.0
献上100积分,求解一个非常困惑的问题(jsp+javabean+sqlserver2008)
jsp初学不久,总共两部分代码A和B(B代码在另一个帖子,其实A,B代码都一样),A部分代码如下:
index.jsp
HTML code
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><html><form name="form1" action="addMessage.jsp" method="get"><table><tr> <td>姓名:</td> <td><input type="text" name="name"/></td></tr><tr> <td>邮箱:</td> <td><input type="text" name="email"/></td></tr><tr> <td>主题:</td> <td><input type="text" name="title"/></td></tr><tr> <td>留言:</td> <td><textarea name="content"></textarea></td></tr><tr> <td><input type="submit" value="提交留言"></td> <td><input type="reset" value="重新填写"></td></tr></table></form></html>
//一个简单的表单提交
addMessage.jsp
HTML code
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ page import="model1.*" %><jsp:useBean id="message" scope="page" class="model1.Message" ><jsp:setProperty name="message" property="*"/></jsp:useBean><jsp:useBean id="messageBean" scope="page" class="model1.MessageBean" /><html><body><%    messageBean.setMessage(message);    messageBean.addMessage();    out.println("success");%></body></html>
//引用javabean实现连接数据库并添加数据
Message.java
Java code
package model1;public class Message {    private String name;    private String email;    private String title;    private String content;    private java.sql.Date date;    public void setName(String name)        {    this.name=name;    }    public void setEmail(String email)        {    this.name=email;    }    public void setTitle(String title)        {    this.name=title;    }    public void setContent(String content)        {    this.name=content;    }    public String getName()        {    return this.name;    }    public String getEmail()        {    return this.email;    }    public String getTitle()        {    return this.title;    }    public String getContent()        {    return this.content;    }    public void setDate(java.sql.Date date)        {    this.date=date;    }    public java.sql.Date getDate()    {    return this.date;    } }
//简单属性
MessageBean.java
Java code
package model1;import java.sql.*;public class MessageBean {    private Connection con;    Message msg;    public MessageBean()    {        String CLASSFORNAME="com.microsoft.sqlserver.jdbc.SQLServerDriver";        String SERVANDDB="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=myjsp";        String USER="sa";        String PWD="123456";        try{            Class.forName(CLASSFORNAME);            con=DriverManager.getConnection(SERVANDDB,USER,PWD);        }        catch(Exception e){            e.printStackTrace();        }    }    public void setMessage(Message msg){        this.msg=msg;    }    public void addMessage()throws Exception    {        String reg="INSERT INTO TABLE2 values(?,?)";        try{            PreparedStatement stm=con.prepareStatement(reg);            stm.setString(1,msg.getName());            if((msg.getEmail()).length()==0)stm.setString(2,null);            else stm.setString(2,msg.getEmail());            stm.setString(3,msg.getTitle());            stm.setString(4,msg.getContent());            stm.setDate(5,new java.sql.Date(new java.util.Date().getTime()));            try{                stm.executeQuery();            }            catch(Exception e){}            con.close();        }        catch(Exception e){            e.printStackTrace();            throw e;        }    }    }
  相关解决方案