当前位置: 代码迷 >> Java Web开发 >> 无任何异常或错误,却无法进行数据库插入操作,求救
  详细解决方案

无任何异常或错误,却无法进行数据库插入操作,求救

热度:80   发布时间:2016-04-17 12:57:17.0
无任何错误或异常,却无法进行数据库插入操作,求救
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(?,?,?,?) ");
  相关解决方案