package fatie;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class fatieServlet extends HttpServlet
{
//初始化变量
public void init(ServletConfig config) throws ServletException
{
super.init(config);
}
//获取数据库连接
private Connection initConnection()
{
Connection conn=null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
try
{
  conn=DriverManager.getConnection("jdbc:odbc:forumData","sa","sa");
}
catch(SQLException e)
{
e.printStackTrace();
}
return conn;
}
//释放数据库连接
private void freeConnection(Connection conn)
{
try
{
conn.close();
}
catch(SQLException e)
{
System.out.print("释放数据库连接失败"+e.toString());
}
}

//插入数据库操作
public int insertData(PreparedStatement pstmt)
{
int n=0;
try
{
n=pstmt.executeUpdate();
}
catch(SQLException e)
{
e.toString();
}
return n;
}
//获得主题ID
private int getTopicID()
{
String sql="select * from sumTopic";
ResultSet rs=null;
Statement stmt=null;
int n=0;
try
{
  Connection conn=this.initConnection();
  stmt=conn.createStatement();
  rs=stmt.executeQuery(sql);
if(rs.next())
{
n=rs.getInt(1);
}
}
catch(SQLException e)
{
e.printStackTrace();
}
return n;
}
//实现总贴数目的更新
  private int addOne(int n)
  {
   int k=0;
   String sql="update sumTopic set sumTopic='"+n+"'+1";
   try
   {
   Connection conn=this.initConnection();
   PreparedStatement pstmt=conn.prepareStatement(sql);
     k=pstmt.executeUpdate();
   }
   catch(SQLException e)
   {
   e.toString();
   }
    return k;
  }
//doPost方法实现
public void doPost(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException
{
int n=0;
String typeid=req.getParameter("typeID");
//int i=Integer.parseInt(typeid);   
String title=req.getParameter("articleN");
//String Face=req.getParameter("face");
String content=req.getParameter("text");
PrintWriter out=resp.getWriter();
int m=getTopicID();
String s=String.valueOf(m+1);
Connection conn=this.initConnection();
try{
  PreparedStatement pstmt=conn.prepareStatement("insert into article(TypeID,ID,Title,Content)value(?,?,?,?)");
  pstmt.setString(1,typeid);
  pstmt.setInt(2,m);
  pstmt.setString(3,title);
  //pstmt.setString(4,Face);
  pstmt.setString(4,content);
  //pstmt.setString(4,Face);
  n=pstmt.executeUpdate();
  }
  catch(SQLException e)
    {
     e.toString();
    }
if(n>0)
  {
   out.println("success");
   addOne(m);
  }
  else
    {
  out.println("false");
  addOne(m);
    }
out.println(typeid);
out.println(title);
//out.println(Face);
out.println(content);
out.println(s);
}
}
} 上面代码是一个servlet,从表单中获取数据并输出没有问题,但就是无法成功进行数据库插入操作(输出"false"),也无任何错误或异常,请指点

解决方案 »

  1.   

    注意一下你的SQL语句,和参数类型是否和你的数据库表结构定义的类型是一致的
      

  2.   

    估计就是pstmt传入的参数类型有问题 还有就是 获得主题ID的方法你测了没如果这个方法出为题 那么也影响你的 addone()吧
      

  3.   

    75分------------------------
      catch(SQLException e)
        {
         e.toString();
        }
    改成这样就看到错误了;
      catch(SQLException e)
        {
         e.printStackTrace();
        }
      

  4.   

    以上答案都不对,是你的INSERT语句错了应该是values你写的是value,我特地为你的帖子注册了个号,这个错误错的可太粗心了。
      

  5.   

    楼主要细心啊,另外别把异常都给捕获了却不输出啊.
    你实在不用e.printStackTrace();还用个System.out.println();啊
      

  6.   

    呵呵! 主要还是你没把异常system.out.pringln()出来 不然这种小错误 一定能报出来的