刚入门,请帮助调试jsp程序!
程序通过不报错,但没有向数据库中写入数据,请指点!
程序如下:
<%@ page import= "java.sql.* " %>
<HTML>
<HEAD>
<TITLE> add message into table </TITLE>
</HEAD>
<BODY>
<%
String name=request.getParameter( "name ");
String mail=request.getParameter( "mail ");
String title=request.getParameter( "title ");
String content=request.getParameter( "content ");
if(name==null)
name= " ";
if(title==null)
title= " ";
if(content==null)
content= " ";
if(mail==null)
mail= " ";
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();
try
{
Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver ").newInstance();
String url= "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test ";
//pubs为你的数据库的
String user= "sa "; //用户名
String password= " "; //密码
Connection con= DriverManager.getConnection(url,user,password);
PreparedStatement stm=con.prepareStatement( "insert into message values(?,?,?,?,?) ");
stm.setString(1,title);
stm.setString(2,name);
if(mail.length()==0)
stm.setString(3,null);
else stm.setString(3,mail);
stm.setString(4,datetime);
stm.setString(5,content);
try
{
stm.executeQuery();
} catch(Exception e)
{}
con.close();
}
catch (Exception e)
{}
%>
<jsp:forward page= "view_message.jsp " />
<%
}
%>
</body>
</html>
------解决方案--------------------stm.executeQuery(); //这是执行返回结果集的SQL语句的
改成
stm.executeUpdate();//这是执行删除,更新语句的
------解决方案--------------------应该是出错了,抛出的例外,都被捕捉了,但是捕捉完却没做任何动作..
你把try catch 全部都去掉,看看....