简体   繁体   English

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

[英]How can i execute 2 Select Queries in Java?

I need your help there with my code I am working on eclipse. 我正在Eclipse上工作的代码需要您的帮助。 When I try to execute 2 Select Queries in Java it doesn't seem to work , I searched the internet but I couldn't find a solution. 当我尝试执行Java中的2 Select Queries似乎不起作用时,我搜索了Internet,但找不到解决方案。 I need to execute 2 select because I need data from 2 tables I got in a database. 我需要执行2 select,因为我需要数据库中2个表中的数据。

Table 1: questions Table 2: selections 表1:问题表2:选择

Well the first query seems to work fine as I can find my items as I should when I execute. 好吧,第一个查询似乎运行良好,因为我可以在执行时按需找到项目。 The items from Table 2 throws me an execution that "Column 'selid' not found. ". 表2中的项目使我执行“找不到列'selid'。”。 // Selid Column is on Table 2. // Selid列在表2。

I am posting you the faulty code on bottom in case you can help me on this. 如果您可以在此方面为我提供帮助,我会在底部将您的错误代码发布到您的网站上。 Thanks in advance. 提前致谢。

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);  
}

Concept is that i have 2 tables and iam making a form that the users answers some questions. 概念是我有2张桌子,而iam制作了一个表格,用户可以回答一些问题。 Iam executing both tables and then iam trying to put the variables in to the form with submit. 我执行两个表,然后尝试使用提交将变量放入表单。 DoPost will take effect after DoGet in the same servlet. DoPost将在同一Servlet中的DoGet之后生效。

Here is the example of the tables. 这是表格的示例。

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 

Change 更改

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

to

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM