将数据插入表中 - jsp错误

时间:2014-06-15 21:44:02

标签: jsp insert

我需要一些帮助才能将数据插入数据库。这就是我的ATM:

<%@  page  language="java"  import="java.sql.*" %>

    <%
String url=  "jdbc:mysql://localhost:3306/reclamacoes";
String titulo=request.getParameter("titulo"); 
String reclamacao=request.getParameter("reclamacao"); 


  try  {  
        String  insert  = "INSERT  INTO  reclamacoes  (id_utilizador, titulo, reclamacao)" + "VALUES  (?,  ?,  ?,  ?,  ?)";
        Class.forName("com.mysql.jdbc.Driver");  

        Connection con = DriverManager.getConnection(url,  "root",  "root");

        PreparedStatement ps =  con.prepareStatement(insert);  
        ps.setString(1,  session.getAttribute("id"));  
        ps.setString(2,  titulo);  
        ps.setString(3,  reclamacao);
        ps.executeUpdate();
        con.close();  
  }catch  (ExcepLon  ex)  {
        out.println ("ERRO:  Insert"); 
  }

%GT;

它给了我这个错误:

  

org.apache.jasper.JasperException:无法为JSP编译类:

     

jsp文件中第15行发生错误:   /recurso/grava_reclamacao.jsp中的方法setString(int,String)   类型PreparedStatement不适用于参数(int,   对象)12:连接con =   DriverManager.getConnection(url,“root”,“root”); 13:14:
  PreparedStatement ps = con.prepareStatement(insert); 15:
  ps.setString(1,session.getAttribute(“id”)); 16:
  ps.setString(2,titulo); 17:ps.setString(3,   reclamacao); 18:ps.executeUpdate();

     

jsp文件中的行:20发生错误:   /recurso/grava_reclamacao.jsp无法将ExcepLon解析为某种类型   17:ps.setString(3,reclamacao); 18:
  ps.executeUpdate(); 19:con.close(); 20:}抓住   (ExcepLon ex){21:out.println(“ERRO:Insert”); 22:   } 23:%&gt;

     

堆栈跟踪:     org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)     org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:366)     org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:476)     org.apache.jasper.compiler.Compiler.compile(Compiler.java:378)     org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)     org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)     org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:657)     org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)     org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)     org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)     javax.servlet.http.HttpServlet.service(HttpServlet.java:727)     org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

我在连接线上找不到错误,对我来说似乎没问题。我需要除jdbc驱动程序以外的任何其他软件包吗?

谢谢大家

2 个答案:

答案 0 :(得分:0)

第一个错误:

  

PreparedStatement类型中的方法setString(int,String)不适用于参数(int,Object)

您需要对从会话中提取的内容进行类型转换,因为默认情况下它会以对象形式出现:

ps.setString(1,  (String)session.getAttribute("id"));  

第二个错误:

  

无法将ExcepLon解析为类型

在你的catch区块中,你拼写错误Exception为&#34; ExcepLon&#34;

答案 1 :(得分:0)

试试这个

<%@  page  language="java"  import="java.sql.*" %>
<%
String url=  "jdbc:mysql://localhost:3306/reclamacoes";
String titulo=request.getParameter("titulo"); 
String reclamacao=request.getParameter("reclamacao");

try{
    String  insert  = "INSERT  INTO "
                    + " reclamacoes(id_utilizador, titulo, reclamacao) "
                    + "VALUES(?, ?, ?)";
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection(url,  "root",  "root");
    PreparedStatement ps =  con.prepareStatement(insert);
    String id = (String) session.getAttribute("id");
    ps.setString(1, id );
    ps.setString(2, titulo);
    ps.setString(3, reclamacao);
    ps.executeUpdate();
}
catch(Exception ex){
   out.print(ex);
}