package cn.com.ruanchuang;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import org.apache.commons.dbutils.QueryRunner;
import cn.com.gestbook2.StringUtil;
public class addMessage extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int result = 0;
String sql = "insert into guestbook(id,name,email,phone,content,time) values(guestbook_id_seq.NEXTVAL,?,?,?,?,?)";
request.setCharacterEncoding("utf-8");
String name = request.getParameter("name");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("html");
out.println("<head><title>add Message</title></head>");
out.println("<body>");
if (StringUtil.validateNull(name)) {
out.println("对不起,姓名不能为空,请重新输入!");
out.println("<a href='/guestbook3/addMessage.jsp'>添加留言内容</a>");
}
try {
Context context = new InitialContext();
DataSource ds = (DataSource) context.lookup("java:/comp/env/jdbc/mysql");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String param[] = {StringUtil.filterHtml(name),StringUtil.filterHtml(request.getParameter("email")),StringUtil.filterHtml(request.getParameter("phone")),request.getParameter("content"),sdf.format(new java.util.Date())};
QueryRunner qr = new QueryRunner(ds);
result = qr.update(sql, param);
} catch (SQLException e) {
e.printStackTrace();
} catch (NamingException e) {
e.printStackTrace();
}
if(result==0){
out.println("对不起,留言添加失败,请重新输入!");
out.println("<a href='/guestbook3/addMessage.jsp'>添加留言内容</a>");
}else{
out.println("恭喜,留言添加成功!");
out.println("<a href='/guestbook3/servlet/getMessege'>查看留言</a>");
}
out.println("</body>");
out.println("</html>");
}
}
无法添加数据 请各位帮忙 我刚学 谢谢啦
------解决方案--------------------
String sql = "insert into guestbook(name,email,phone,content,time) values(?,?,?,?,?)";
SQL语句改成这样的试试