2015-03-13 85 views
0

我想檢索一個jsp頁面中的mysql數據。我面臨的問題是檢索值沒有得到打印。但是,你好是根據表中的行數正確打印JSP數據庫訪問不起作用

的代碼是

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!DOCTYPE html> 
<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title>JSP Page</title> 
</head> 
<body> 
    <h1>Hello World!</h1> 
    <%@page import="java.sql.*,javax.servlet.*,javax.sql.*" %> 
    <% 

     //Register JDBC driver 
     Class.forName("com.mysql.jdbc.Driver"); 
     //Open a connection 
     Connection conn=null; 
     conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/quiz","root","root"); 
     // Execute SQL query 
     Statement stmt = conn.createStatement(); 
     String sql="select Question_Id,Question from question_master;"; 
     out.println(sql); 
     ResultSet rs=stmt.executeQuery(sql); 
     while(rs.next()) 
     { 
      out.println("hello"); 
      int id=rs.getInt("Question_Id"); 
      String ques=rs.getString("Question"); 
      System.out.println("ID: " + id + "<br>"); 
      System.out.println("Question " + ques+ "<br>"); 
     } 
     %> 
</body> 
</html> 

回答

2

的問題是你寫

System.out.println("ID: " + id + "<br>"); 
      System.out.println("Question " + ques+ "<br>"); 

將在服務器控制檯打印

你必須寫

out.println("ID: " + id + "<br>"); 
      out.println("Question " + ques+ "<br>"); 

打印在jsp頁面

+0

謝謝老兄!這是一個愚蠢的錯誤沒有注意到 – user305774 2015-03-13 16:14:09