数据库中表 leavemessage 相关字段如下:
userid varchar(50)
pubtitle varchar(50)
pubcontent text
pubdate datetime
===============
package entity
import java.util.Date;
public class LeaveMessage{
private String userid;
private String pubtitle;
private String pubcontent;
private Date pubdate=new Date();
以下由Eclipse自动生成set、get方法
.
.
.}
===============
package dao
import java.util.*;
import java.sql.*;
import entity.
public class LeaveMessageDao{
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
int i=0;
public Connection getConnection(){
...
return conn  //这都正常不写了
}
public int addLeaveMessage(LeaveMessage lm){
String sql="insert into leavemessage (userid ,pubtitle ,pubcontent ,pubdate )values(?,?,?,?)";
conn=getConnection();
try{
ps=conn.PrepareStatement(sql);
ps.setString(1,lm.getString("userid"));
ps.setString(2,lm.getString("pubtitle"));
ps.setString(3,lm.getString("pubcontent"));
ps.setDate(4,lm.new Date());i=ps.executeQuery();
}catch(Exception e){
e.getMessage();
}
return i;}}
===============
user.jsp
<%@ page language="java" pageEncoding="gbk"%>
<html>
<head>
<title>用户留言</title>
</head>
<body>
<form method="post" action="douser.jsp">
<tr>
<td>userid</td>
<td><input type="text" name="userid" /></td>
</tr>
<td>pubtitle</td>
<td><input type="text" name="pubtitle" /></td>
</tr>
</tr>
<td>pubcontent</td>
<td><input type="textarea" name="pubcontent" /></td>
</tr>
</tr>
<td><input type="submit" value="提交" /></td>
<td><input type="reset" value="重置" /></td>
</tr>
</form>
</body>
</html>
==============
douser.jsp
<%@ page import="entity.LeaveMessage,dao.LeaveMessageDao"%>
<%
request.setCharacterEncoding("gbk");
String s_userid=request.getParameter("userid");
String s_pubtitle=request.getParameter("pubtitle");
String s_pubcontent=request.getParameter("pubcontent");
LeaveMessage lm=new LeaveMessage();
LeaveMessageDao lmdao=new LeaveMessageDao();
lm.setUserid(s_userid);
lm.setPubtitle(s_pubtitle);
lm.setPubcontent(s_pubcontent);
int i=lmdao.addLeaveMessage();
if(i>0){
response.sendRedirect(index.jsp);
}else{
response.sendRedirect(user.jsp);}
%>

解决方案 »

  1.   

    LeaveMessage lm=new LeaveMessage();
    已经生命过了,另外已经封装了 ,有set(),get()方法的
    lm.?就是给他赋值啊 哎
      

  2.   


    这个写法在到dao地下LeaveMessageDao方法ps.setDate(4,lm.new Date());出现问题
    报错误:"ps.setDate(int,Date);对于(int,Date)不适用"明明他里面参数就是一个整型和一个Date型 但是我穿进去相应的参数就是报这个错误,感觉他报错本身就有问题,我要是以字符的形势传个日期倒也没问题
    但是我后期要做日期范围内查询 字符到怎么查范围啊?
    你能告诉我怎么在jsp中向数据库中插入日期吗?
      

  3.   

    try{
    ps=conn.PrepareStatement(sql);
    ps.setString(1,lm.getString("userid"));
    ps.setString(2,lm.getString("pubtitle"));
    ps.setString(3,lm.getString("pubcontent"));
    ps.setDate(4,lm.new Date());
    i=ps.executeQuery();
    }catch(Exception e){
    e.getMessage();
    }里面应该是更新语句ps.executeUpdate(),怎么用查询语句ps.executeQuery()呢,呵呵!建议,更新一般没有必要返回值,查询返回的类型最好还是包装成自己的数据类型!
      

  4.   

    ps.setDate(int,Date),这里的Date是java.sql.Date类型的,不能插入一个java.util.Date类型的Date对象,
    可以这样转换一下:ps.setDate(i,new java.sql.Date(new java.util.Date().getTime()))
      

  5.   

    什么数据库?
    如果你用new Date,还不如给字段默认当前时间呢
      

  6.   

    ps.setDate(4,lm.new Date());?为啥事lm.new Date()这种写法ps.setDate(4, new Date());吧