如何执行2在Java中选择查询?

时间:2016-01-24 15:51:53

标签: java sql

我需要你的帮助我的代码我正在研究eclipse。当我尝试执行2选择Java中的查询它似乎不起作用,我搜索了互联网,但我找不到解决方案。我需要执行2选择,因为我需要数据库中的2个表中的数据。

表1:问题 表2:选择

好吧,第一个查询似乎工作正常,因为我可以在执行时找到我的项目。表2中的项目抛出了我的执行 “没有找到列'selid'。” // Selid Column在表2中。

我在底部发布了有问题的代码,以防您对此有所帮助。 提前谢谢。

public void doGet(HttpServletRequest req, HttpServletResponse res)
        throws IOException, ServletException {


    //String connectionURL = "jdbc:mysql://127.0.0.1:3306/newData";// newData is the database  
    //Connection connection;  
    Connection conn=null;

    String dbName="teddb";

    res.setContentType("text/html");
    res.setCharacterEncoding("utf-8");
    PrintWriter out = res.getWriter();
//String dbUserName="root";
//String dbPassword="root";

try{ 



     String qid = "";
     String question = "";
     String sel1  = "";
     String sel2 = "";
     String sel3 = "";
     String correct = "";
     String selid ="";
     String sel="";






     Connection dbCon;


     Class.forName(driver);
     dbCon = DriverManager.getConnection(dbURL);
     ResultSet rs;
     ResultSet rs2;
     Statement stmt;
     Statement stmt2;



     stmt = dbCon.createStatement();
     stmt2 = dbCon.createStatement();

     String qry = "";
     String qry2 = "";



    qry = "select * from questions";
    qry2 = "select * from selections";

    rs = stmt.executeQuery(qry);
    stmt = dbCon.prepareStatement(qry);


    rs2 = stmt2.executeQuery(qry2);
    stmt2 = dbCon.prepareStatement(qry2);



    String[] columns = new String[] { "qid", 
            "question_text" , "selid" , "selection_text" ,};


    Random rn = new Random();
    int range = 2 - 1 + 1;
    int randomNum =  rn.nextInt(range) + 1;

    out.println(randomNum);



    while (rs.next()) {

        for (int i = randomNum; i <= randomNum; i++) {
            question = rs.getString(columns[1]);
            sel1 = rs.getString(columns[2]);
            sel2 = rs.getString(columns[3]);

        }
    }

    PreparedStatement pstmt;

    for (int z=1;z<=3;z++){

       selid = String.valueOf(rs.getString(columns[2]));
       pstmt = dbCon.prepareStatement(qry2 + " where qid = ? and selid ='z'");
       pstmt.setString(1, qid);
       rs2 = pstmt.executeQuery();

    while (rs2.next()) {

        for (int i = randomNum; i <= randomNum; i++) {

                if (z==1)
            sel1 = rs.getString(columns[3]);
                else  if (z==2)
            sel2 = rs.getString(columns[3]);
                else
            sel3 = rs.getString(columns[3]);

    }
    }
    }

    out.println("<!DOCTYPE html>"+
            "<html><body>"+
            "<form method=\"post\" action=\"demoServlet\">"+
            "<b><h1>Ερώτηση</h1></b> <br><br>"+
            "<b><h1>"+question+" </h1></b> <br><br>"+
            "<b> 1: </b> <input type=\"radio\" name=\"iscorrect\" value=\"" + sel1 + "\"/><br>"+
            "<b> 2: </b> <input type=\"radio\" name=\"iscorrect\" value=\"" + sel2 + "\"/> <br>"+
            "<b> 3: </b> <input type=\"radio\" name = \"iscorrect\" value=\"" + sel3 + "\"/><br><br>"+

            "<br><input type=\"submit\" name=\"submit\" value=\"Απάντηση\"/>"+
            "</form></body></html>");






    dbCon.commit(); 

    String msg=" ";



    rs.close();
    rs2.close();
    stmt.close();
    dbCon.close();
}  
catch (Exception e){  
  out.println(e);  
}

概念是我有2个表格,并且iam制作一个用户回答一些问题的表格。我执行两个表然后尝试将变量放入带有提交的表单中。 DoPost将在DoGet之后的同一个servlet中生效。

以下是表格的示例。

questions      | selections
qid | question | qid | | selid | selection_text |correct
q1  |  1+1?    | q1         1        5             0
                 q1         2        2             1 // true
                 q1         3        4             0 

1 个答案:

答案 0 :(得分:0)

更改

rs = stmt.executeQuery(qry);
rs2 = stmt.executeQuery(qry2);

rs = stmt.executeQuery(qry);//first query
rs2 = stmt2.executeQuery(qry2);//second query