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"),也无任何错误或异常,请指点
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"),也无任何错误或异常,请指点
解决方案 »
- 初学者求助:Java中的socket网络编程中的输入输出流问题
- javascript $
- Struts中select 标签问题
- 请哪位朋友帮帮忙,谢谢!
- Hebernate求助
- 请问 如何让一个javaScript事件 随着页面的打开而执行并随着页面的关闭而停止 期间一直执行?
- 我是做软件实施的。感觉非常没有前途~~~有没有同行。快来发表评论。来者有分
- 有没有那位在济南的jsp高手来帮我解决这个问题?很急啊
- 訪問ACCESS的.mdb數據庫, 為仕麼顯示結果亂碼.
- 在 jbuilder 中 类库javax.xxxxx.xxxxx 是 什么库啊?
- 怎么在数据库中实现分页????求助!~
- 关于blog的Trackback Ping-URL 发送的问题。
catch(SQLException e)
{
e.toString();
}
改成这样就看到错误了;
catch(SQLException e)
{
e.printStackTrace();
}
你实在不用e.printStackTrace();还用个System.out.println();啊