2016-01-24 55 views
1

我需要你的幫助,我的代碼是在eclipse上工作的。當我嘗試執行2在Java中選擇查詢時,它似乎不起作用,我搜索了互聯網,但找不到解決方案。我需要執行2 select,因爲我需要數據庫中的2個表中的數據。我該如何執行2在Java中選擇查詢?

表1:問題 表2:選擇

那麼第一個查詢似乎做工精細,我可以找到我的項目,我應該當我執行。表2中的項目引發我執行 「Column'selid'not found。」。 // Selid列在表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使用戶回答一些問題的形式。 Iam執行兩個表,然後試圖通過提交將變量放入表單中。 DoPost將在同一個servlet中的DoGet之後生效。

這裏是表格的例子。

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 
+0

停止嘗試推倒重來。獲取MyBatis,Hibernate或Spring Data Access。 MyBatis和Spring更容易學習, – DwB

回答

0

變化

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

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

仍然不起作用。我編輯我的代碼,如果你可以請重新檢查。 – foutzos

+0

@foutz是否在表2中存在「Selid」列? – Satya

+0

是的,我在這裏編輯帖子的結尾,向你展示我的表格。 Iam真的新手,我已經搞亂了我認爲的代碼.... – foutzos