数据库中表 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);}
%>
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);}
%>
解决方案 »
- java 判断某个方法执行是否超时
- actionform一段代码报错,请高手赐教。
- 有谁用过db40 帮忙看看这个错误
- Spring中父类的依赖可自动注入到子类中吗?
- Rowset 怎么转换成webrowset 啊
- <html:select>怎么用,请指教!!!万分感谢各位大侠!救小弟一命..
- JSP中有没有像ASP.net中的DataGrid控件类似的东东?
- 数据库采用连接池的WEB应用程序必须放在TOMCAT_HOME/webapps里面才能运行么?
- 两台计算机建立Socket连接,抛该例外什么意思?
- 更换了Struts 1.1后,JBuilder 9的错误??????急急
- java.sql.SQLException: Io 异常: The Network Adapter could not establish the conn e
- struts提示路径错误运行不起来
已经生命过了,另外已经封装了 ,有set(),get()方法的
lm.?就是给他赋值啊 哎
这个写法在到dao地下LeaveMessageDao方法ps.setDate(4,lm.new Date());出现问题
报错误:"ps.setDate(int,Date);对于(int,Date)不适用"明明他里面参数就是一个整型和一个Date型 但是我穿进去相应的参数就是报这个错误,感觉他报错本身就有问题,我要是以字符的形势传个日期倒也没问题
但是我后期要做日期范围内查询 字符到怎么查范围啊?
你能告诉我怎么在jsp中向数据库中插入日期吗?
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()呢,呵呵!建议,更新一般没有必要返回值,查询返回的类型最好还是包装成自己的数据类型!
可以这样转换一下:ps.setDate(i,new java.sql.Date(new java.util.Date().getTime()))
如果你用new Date,还不如给字段默认当前时间呢