简体   繁体   中英

How can i execute 2 Select Queries in Java?

I need your help there with my code I am working on 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. I need to execute 2 select because I need data from 2 tables I got in a database.

Table 1: questions Table 2: selections

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. ". // Selid Column is on Table 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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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