2016-11-22 28 views
0

我在一個jsp程序中工作,試圖自己學習一個jsp。在jsp的這條線上發現的多註釋

所以我做了一個測驗程序,其中的問題是從數據庫表中獲得的。所以這裏是測驗頁面的代碼,其中發佈了問題。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Insert title here</title> 
</head> 
<body> 

    <%@ include file="_header.jsp"%> 

    <center>QUIZ PROGRAM</center> 

    <br /> 

    <%@ page import="java.sql.*"%> 

    <% 
     //print the question and answer 

     int questionaire = 1; 
     Class.forName("com.mysql.jdbc.Driver"); 
     Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/quiz", "root", ""); 
     PreparedStatement pst = con.prepareStatement("SELECT * FROM questions WHERE id = ?"); 
     pst.setInt(1, questionaire); 

     ResultSet rs = pst.executeQuery(); 

     int questionId; 
     String questionp; 

     if (rs.next()) { 
      questionId = rs.getInt("id"); 
      String questionp = rs.getString("question"); 
      //String option1 = rs.getString("option1"); 
      //String option2 = rs.getString("option2"); 
      //String right = rs.getString("right"); 
      questionaire++; 
     } 
     //get the answer and check 
     //String question1 = "asd"; 
    %> 

    <center> 
     <form method="post" action="quiz.jsp"> 
      <table border="1" cellpadding="5" cellspacing="2" align="center"> 
       <thead> 
        <tr> 
         <th colspan="2"><% out.println(questionp); %></th> 
        </tr> 
       </thead> 
       <tbody> 

        <tr> 
         <td><input type="radio" name="question" 
          value="value1">Yes</td> 
         <td><input type="radio" name="question" 
          value="value2">No</td> 
        </tr> 
        <tr> 
         <td colspan="2" align="center"><input type="submit" 
          value="Next" onClick="next();" /></td> 
        </tr> 
       </tbody> 
      </table> 
     </form> 
    </center> 



</body> 
</html> 

在上面的代碼,我在這條線得到一個錯誤,指出,在這條線找到

String questionp = rs.getString("question"); 

muliple註釋

enter image description here

+0

什麼是你的問題?你面臨的問題是什麼?請參閱此鏈接(http://stackoverflow.com/help/how-to-ask)以瞭解如何正確說明您的問題。 – greenPadawan

+0

'<%out.println(「questionp」); %>'是打印值'questionp'而不是變量。使用'<%out.println(questionp); %>'或'<%= questionp%>'(我認爲)。但是就像greenPadawan說的那樣,下次請看[問]並提供[mcve]。只要寫它,你會發現你的問題。我想補充一點,你應該很快學會如何使用'Servlets'和'Jstl'來正確地編寫JSP;) – AxelH

+0

@greenPadawn對不起,我沒有解釋我面臨的問題,現在我已經編輯了這個問題,解釋了我的問題,仍然顯示錯誤。 – javailike

回答

0

你的輸出是錯誤的

<% out.println("questionp"); %> 

這將不會打印您恢復的值,而是顯示「questionp」文本。

你需要輸出與

<% out.println(questionp); %> 

或者

<%= questionp %> 

更多的解決方案,see the post

編輯變量:

爲了您的編輯,multiple annontaion意味着你已經用這個名字聲明瞭一個變量。在這裏,你聲明questionp兩次。

String questionp; 
if (rs.next()) { 
    String questionp = rs.getString("question"); 

刪除類型:

String questionp = null; 
if (rs.next()) { 
    questionp = rs.getString("question"); 
+0

我在行中出現錯誤,<%out.println(questionp); %>局部變量的問題可能沒有初始化 – javailike

+0

@javailike這是因爲我沒有檢查...因爲你沒有'else',字符串可能永遠不會被初始化。因此,請使用默認值或null初始化該值。 (請參閱編輯的代碼)這是一些基本的Java,如果您不知道Java,請不要學習如何快速JSP。 – AxelH

+0

int,int questionId = null;這表明我錯誤 – javailike