回 帖 发 新 帖 刷新版面

主题:求助啊!!!Jsp连接mysql+Servlet输出

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.sql.*;

public class Mytest extends HttpServlet {
    
    public String a[]=new String[9];
    
    public Mytest() {
        super();
    }

    
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }


    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        response.setContentType("text/html;charset=gb2312");
        PrintWriter out = response.getWriter();
        
            
        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
        out.println("<HTML>");
        out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
        out.println("  <BODY>");
        
        out.println("<form name='form1' methed='post'action >");
        /*for(int i=0;i<10;i++){
            out.print("<input type='text' name='a[i]' size=5>");
        }*/
        out.print("<input type='text' name='num1' size=5>");
        out.print("<input type='text' name='num2' size=5>");
        out.print("<input type='text' name='num3' size=5>");
        out.print("<input type='text' name='num4' size=5>");
        out.print("<input type='text' name='num5' size=5>");
        
        out.println("<br><input type=submit value=提交>");
        out.println("<input type=reset value=重新输入>");
        out.println("</form>");
        
        
        String sort1=request.getParameter("num1");
        String sort2=request.getParameter("num2");
        String sort3=request.getParameter("num3");
        String sort4=request.getParameter("num4");
        String sort5=request.getParameter("num5");
        
        
        try{
            Class.forName("com.mysql.jdbc.Driver");
            
        }catch(Exception e){
            out.print(e);
        }
        
        try{
            String sqlString=null;
            sqlString="insert into sort (a,b,c,d,e) values('"+sort1+"','"+sort2+"','"+sort3+"','"+sort4+"','"+sort5+"')";
            //sqlString="select * from sort";
            ResultSet rs;
            Connection con;
            Statement sql;
            con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","aaa","aaa");
            sql=con.createStatement();
            rs=sql.executeQuery(sqlString);
            
            
           
            while(rs.next()){
                out.print("<table>");       //显示出表格
                out.print("<tr>");
                out.print("<td>"+rs.getString(1)+"</td>");
                out.print("<td>"+rs.getString(2)+"</td>");
                out.print("<td>"+rs.getString(3)+"</td>");
                out.print("<td>"+rs.getString(4)+"</td>");
                out.print("<td>"+rs.getString(5)+"</td>");
                out.print("</tr>");
                out.print("</table>");
            }
          
        }catch(SQLException e1){
            out.print("connectionException:"+e1.getMessage());
        }
        out.println("</BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
        
    }

    
    public void init() throws ServletException {
        // Put your code here
    }

}
报的是什么错?connectionException:Can not issue data manipulation statements with executeQuery().

回复列表 (共1个回复)

沙发

rs=sql.executeQuery(sqlString);
改成:
rs=sql.executeUpdate(sqlString);

我来回复

您尚未登录,请登录后再回复。点此登录或注册