package com.new;
import oa.sys.*;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
****************************************************
*类名称: Del<br>
*类功能: 删除新闻<br>
*
****************************************************
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class Del extends HttpServlet{
private int temp;
private String sqli;
private int id;
private int infoid;
private HttpSession session=null;
private ResultSet rs=null;
private Statement stmt=null;
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
request.setCharacterEncoding("gb2312");
response.setContentType("text/html; charset=gb2312");
Str str=new Str();
Db db=new Db();
PrintWriter out=response.getWriter();
session=request.getSession();
try{
id=Integer.parseInt((String)session.getAttribute("id"));
}catch(Exception e){
e.printStackTrace();
}
try{
infoid=Integer.parseInt((String)request.getParameter("infoid"));
}catch(Exception e){
e.printStackTrace();
}
if(id==-1){
sqli="DELETE FROM new WHERE infoid="+infoid;
}else{
sqli="DELETE FROM new WHERE accepter="+id+" AND infoid="+infoid;
}
try {
stmt=db.getStmt();
temp=stmt.executeUpdate(sqli);
} catch (SQLException e) {
e.printStackTrace();
}finally{
db.close();
RequestDispatcher dispatcher=request.getRequestDispatcher("view");
dispatcher.forward(request,response);
}
}
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
doPost(request,response);
}
}
编译结果:
Del.java:1:需要 <标识符〉
package com.new;
^
1 错误
Str 是字符串转化的javabeans
Db 里面连接数据库的代码
public class Db {
private Connection con;
private Statement stmt;
private ResultSet rs;
private PreparedStatement pstmt;
public static int error=0;
/***************************************************
*函数名称: getCon()<br>
*函数功能: 获取数据库连接<br>
*返回值: 无<br>
*参数说明: 无<br>
*
****************************************************/
public static synchronized Connection getCon()throws Exception{
Context ctx;
DataSource ds;
try{
ctx = new InitialContext();
ds = (DataSource)ctx.lookup("java:comp/env/jdbc/OA");
System.err.println();
System.err.println("数据连接+"+(++error));
return ds.getConnection();
}catch(SQLException e){
System.out.print(e);
throw e;
}
catch(NamingException e){
System.out.print(e);
throw e;
}
}