request.setCharacterEncoding("UTF-8");
    String id=request.getParameter("id");
    String name=request.getParameter("name");
    System.out.println(name);
    String age=request.getParameter("age");
    String gender=request.getParameter("gender");
    String major=request.getParameter("major");
    
    Connection conn=null; 
    Statement stmt=null; 
    ResultSet rs=null;
    Class.forName("com.mysql.jdbc.Driver"); 
    String url="jdbc:mysql://localhost:3306/orcl"; 
    String user="root"; 
    String password="root"; 
    conn=DriverManager.getConnection(url,user,password); 
    stmt=conn.createStatement(); 
    rs=stmt.executeQuery("insert into student (id,name,age,gender,major)values("+id+"','"+name+"','"+age+",'"+gender+"','"+major+")");%>
   把executeQuery改成executeUpdate也不行 因为要返回一个ResultSet 

解决方案 »

  1.   

    你能不能把JSP贴全一点,然后把报错信息也贴全一点,就一行信息,大家都不是神仙,明白?
      

  2.   

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@ page import="java.sql.*"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>插入学生信息</title>
        
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">

    <link rel="stylesheet" type="text/css" href="styles.css">
      </head>
        <body>
        <% 
        request.setCharacterEncoding("UTF-8");
        String id=request.getParameter("id");
        String name=request.getParameter("name");
        System.out.println(name);
        String age=request.getParameter("age");
        String gender=request.getParameter("gender");
        String major=request.getParameter("major");
        
        Connection conn=null; 
        Statement stmt=null; 
        ResultSet rs=null;
        Class.forName("com.mysql.jdbc.Driver"); 
        String url="jdbc:mysql://localhost:3306/orcl"; 
        String user="root"; 
        String password="root"; 
        conn=DriverManager.getConnection(url,user,password); 
        stmt=conn.createStatement(); 
        rs=stmt.executeQuery("insert into student (id,name,age,gender,major)values("+id+"','"+name+"','"+age+",'"+gender+"','"+major+")");%>
       
       <center>
       <%
        if(rs.next())
        {
        out.print("<br><h3>成功输入!</h3>");
        }
        else{
        out.print("<br><h3>输入失败!</h3>");
        }
      
        %>
       
          <br>
        <a href=submit.jsp>返回信息输入页面</a>   <a href=layout.jsp>进入信息查询页面</a> 
        </center>
        <% 
        if(rs!=null)
        {
            rs.close();
        }
        
            if(stmt!=null)
        {
            stmt.close();
        }
            if(conn!=null)
        {
            conn.close();
        }
        %> 
          </body>
    </html>
    严重: Servlet.service() for servlet jsp threw exception
    java.sql.SQLException: Can not issue data manipulation statements with executeQuery().
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910)
    at com.mysql.jdbc.Statement.checkForDml(Statement.java:417)
    at com.mysql.jdbc.Statement.executeQuery(Statement.java:1140)
    at org.apache.jsp.insert_jsp._jspService(insert_jsp.java:102)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Thread.java:619)
      

  3.   

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@ page import="java.sql.*"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>插入学生信息</title>
        
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">

    <link rel="stylesheet" type="text/css" href="styles.css">
      </head>
        <body>
        <% 
        request.setCharacterEncoding("UTF-8");
        String id=request.getParameter("id");
        String name=request.getParameter("name");
        System.out.println(name);
        String age=request.getParameter("age");
        String gender=request.getParameter("gender");
        String major=request.getParameter("major");
        
        Connection conn=null; 
        Statement stmt=null; 
        ResultSet rs=null;
        Class.forName("com.mysql.jdbc.Driver"); 
        String url="jdbc:mysql://localhost:3306/orcl"; 
        String user="root"; 
        String password="root"; 
        conn=DriverManager.getConnection(url,user,password); 
        stmt=conn.createStatement(); 
        rs=stmt.executeQuery("insert into student (id,name,age,gender,major)values("+id+"','"+name+"','"+age+",'"+gender+"','"+major+")");%>
       
       <center>
       <%
        if(rs.next())
        {
        out.print("<br><h3>成功输入!</h3>");
        }
        else{
        out.print("<br><h3>输入失败!</h3>");
        }
      
        %>
       
          <br>
        <a href=submit.jsp>返回信息输入页面</a>   <a href=layout.jsp>进入信息查询页面</a> 
        </center>
        <% 
        if(rs!=null)
        {
            rs.close();
        }
        
            if(stmt!=null)
        {
            stmt.close();
        }
            if(conn!=null)
        {
            conn.close();
        }
        %> 
          </body>
    </html>
      

  4.   

    报错是 严重: Servlet.service() for servlet jsp threw exception
    java.sql.SQLException: Can not issue data manipulation statements with executeQuery().
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910)
    at com.mysql.jdbc.Statement.checkForDml(Statement.java:417)
    at com.mysql.jdbc.Statement.executeQuery(Statement.java:1140)
    at org.apache.jsp.insert_jsp._jspService(insert_jsp.java:102)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Thread.java:619)
      

  5.   

    rs=stmt.executeQuery("insert into student (id,name,age,gender,major)values("+id+"','"+name+"','"+age+",'"+gender+"','"+major+")");%>
    这里你用的是executeQuery这个API,但是SQL语句却是insert语句,导致了出错
    如果你像插入数据,返回结果肯定不是ResultSet,你试下下面的JSP可以么<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@ page import="java.sql.*"%>
    <%
        String path = request.getContextPath();
        String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
        <base href="<%=basePath%>">    <title>插入学生信息</title>    <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">    <link rel="stylesheet" type="text/css" href="styles.css">
    </head>
    <body>
    <%
        request.setCharacterEncoding("UTF-8");
        String id=request.getParameter("id");
        String name=request.getParameter("name");
        System.out.println(name);
        String age=request.getParameter("age");
        String gender=request.getParameter("gender");
        String major=request.getParameter("major");    try{
            Connection conn=null;
            Statement stmt=null;
            ResultSet rs=null;
            Class.forName("com.mysql.jdbc.Driver");
            String url="jdbc:mysql://localhost:3306/orcl";
            String user="root";
            String password="root";
            conn=DriverManager.getConnection(url,user,password);
            stmt=conn.createStatement();        boolean result=stmt.execute("insert into student (id,name,age,gender,major)values("+id+"','"+name+"','"+age+",'"+gender+"','"+major+")");%><center>
        <%
            if(result)
            {
                out.print("<br><h3>成功插入!</h3>");
            }
            else{
                out.print("<br><h3>输入失败!</h3>");
            }    %>    <br>
        <a href=submit.jsp>返回信息输入页面</a> <a href=layout.jsp>进入信息查询页面</a>
    </center>
    <%
            if(rs!=null)
            {
                rs.close();
            }        if(stmt!=null)
            {
                stmt.close();
            }
            if(conn!=null)
            {
                conn.close();
            }    }catch(Exception e){
            e.printStackTrace();
        }
    %>
    </body>
    </html>
      

  6.   


    又报错了 ou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''dfg )' at line 1
      

  7.   

    你的数据表student的字段和类型给出来,这个会影响插入的语法是否正确论坛这种速度太慢 519582737 加QQ
      

  8.   

    这是我做实验的student表的创建语句CREATE TABLE `student` (
      `id` bigint(20) NOT NULL AUTO_INCREMENT,
      `NAME` varchar(255) DEFAULT NULL,
      `age` int(11) DEFAULT NULL,
      `gender` varchar(20) DEFAULT NULL,
      `major` varchar(255) DEFAULT NULL,
      PRIMARY KEY (`id`)
    )
    下面是测试可以通过的JSP页面,我的JDBC的URL连接是MSDB数据库,你改成你自己的ORCL就可以了
    还有我只是跑通了一个自己给定的值,你可以改成你其他页面传过来的各种属性,但是id age需要从
    String转化为int类型<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@ page import="java.sql.*"%>
    <%
    //    这个JSP有待于测试
        String path = request.getContextPath();
        String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
        <base href="<%=basePath%>">    <title>插入学生信息</title>    <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">    <link rel="stylesheet" type="text/css" href="styles.css">
    </head>
    <body>
    <%
    //    request.setCharacterEncoding("UTF-8");
    //    String id=request.getParameter("id");
    //    String name=request.getParameter("name");
    //    System.out.println(name);
    //    String age=request.getParameter("age");
    //    String gender=request.getParameter("gender");
    //    String major=request.getParameter("major");    String idStr="1";
        int id=Integer.parseInt(idStr);
        String name="name";
    //    System.out.println(name);
        String ageStr="20";
        int age=Integer.parseInt(ageStr);
        String gender="gender";
        String major="major";    try{
            Connection conn=null;
            Statement stmt=null;
            ResultSet rs=null;
            Class.forName("com.mysql.jdbc.Driver");
            String url="jdbc:mysql://localhost:3306/msdb";
            String user="root";
            String password="root";
            conn=DriverManager.getConnection(url,user,password);
            stmt=conn.createStatement();        String insert="insert into student (id,name,age,gender,major)values('"+id+"','"+name+"','"+age+"','"+gender+"','"+major+"')";
            System.out.println("SQL:"+insert);
            int result=stmt.executeUpdate(insert);%><center>
        <%
            if(result==1)
            {
                out.print("<br><h3>成功插入!</h3>");
            }
            else{
                out.print("<br><h3>输入失败!</h3>");
            }    %>    <br>
        <a href=submit.jsp>返回信息输入页面</a> <a href=layout.jsp>进入信息查询页面</a>
    </center>
    <%
            if(rs!=null)
            {
                rs.close();
            }        if(stmt!=null)
            {
                stmt.close();
            }
            if(conn!=null)
            {
                conn.close();
            }    }catch(Exception e){
            e.printStackTrace();
        }
    %>
    </body>
    </html>