日期:2014-05-17  浏览次数:20979 次

编写留言板程序代码
好心的同志们给帮助看看:看看这段代码有什么问题呀,就是总是往mysql数据库里插不进去东西呀。
<%@ page contentType="text/html;charset=GBK"%>
<%@ page import="java.sql.*"%>
<html>
<head>
<title>验证信息</title>
</head>
<body>
<%
String name=request.getParameter("name");
String mail=request.getParameter("mail");
String title=request.getParameter("title");
String content=request.getParameter("content");
System.out.println(content);
if(name==null)name="";
if(mail==null) mail="";
if(title==null) title="";
if(content==null) content="";
if(name.length()==0)
out.println("留言人姓名不能为空!");
else if(title.length()==0)
out.println("留言主题不能为空!");
else if(content.length()==0)
{
out.println("留言内容不能为空!");

}
else
{
java.util.Date date=new java.util.Date();
String datetime=new Timestamp(date.getTime()).toString();
System.out.println(datetime);
try
{
Class.forName("com.myslq.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/bbs";
Connection con=DriverManager.getConnection(url,"root","admin");
PreparedStatement stm=con.prepareStatement("insert into liuyan(name,mail,title,datetime,content) values(?,?,?,?,?)");
stm.setString(2,name);
if(mail.length()==0)
stm.setString(3,null);
else stm.setString(3,mail);
stm.setString(4,title);
stm.setString(5,datetime);
stm.setString(6,content);
try{
stm.executeQuery();
}catch(SQLException e)
{
System.out.println("数据库连接有问题!");
}
  con.close();
}
catch(Exception e)
{
System.out.println("操作数据库有问题!");
}
%>
<jsp:forward page="view_message.jsp"/>
<%
}
%>
</body>
</html>


------解决方案--------------------
插得進才怪
PreparedStatement stm=con.prepareStatement("insert into liuyan(name,mail,title,datetime,content) values(?,?,?,?,?)");
stm.setString(2,name);
if(mail.length()==0)
stm.setString(3,null);
else stm.setString(3,mail);
stm.setString(4,title);
stm.setString(5,datetime);
stm.setString(6,content);

異常信息又不打印出來,一定是報錯了
------解决方案--------------------
HTML code

<%@ page import="com.mysql.jdbc.Driver" %>

------解决方案--------------------
报的什么错给贴出来。