2017-07-24 70 views
0

我是jsp的初學者,我試圖用jsp連接我的數據庫,並在jsp上顯示我的表的內容,但它沒有到達那裏。請告訴我我哪裏錯了?jsp iant顯示mysql表的內容

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

<% 
String roll=request.getParameter("roll"); 
String driver="com.mysql.jbdc.Driver"; 
String connectionUrl="jbdc:mysql://localhost:3306/"; 
String database="try"; 
String userid="root"; 
String password="password"; 
try{ 
    Class.forName(driver); 
}catch(Exception e){ 
    e.printStackTrace(); 
}   
Connection conn=null; 
Statement stmt=null; 

%> 

<html> 
    <body> 
    <h1>STUDENT RECORD</h1> 
    <table border=1> 
    <tr> 
     <td>Roll</td> 
     <td>Name</td> 
     <td>Trade</td> 
     <td>Semester</td> 
     <td>Update</td> 
    </tr> 

<% 
try{ 
    conn=DriverManager.getConnection(connectionUrl+database,userid,password); 
    stmt=conn.createStatement(); 
    String s="select*from student"; 
    ResultSet rs=stmt.executeQuery(s); 
    while(rs.next()){ 
%> 
    <tr> 
     <td><%= rs.getString("roll") %></td> 
     <td><%= rs.getString("name") %></td> 
     <td><%= rs.getString("trade") %></td> 
     <td><%= rs.getString("sem") %></td> 
     <td><a href="update.jsp?id=<%=rs.getString("roll")%>">UPDATE</a></td> 
     </tr> 
<% 
    } 
    conn.close(); 
}catch(Exception e){ 
    e.printStackTrace(); 
} 
%> 
    </table> 
    </body> 
</html> 

我得到的結果顯示在圖像中。我正在使用eclipse mars jee和mysql數據庫。 Tomcat 8.0作爲服務器。 enter image description hereenter image description hereenter image description here

+0

你只是想在JSP頁面加載獲取數據? – sForSujit

回答

0

看一看到我的代碼,看到你錯了

<%@page import="java.sql.ResultSet"%> 
<%@page import="java.sql.Statement"%> 
<%@page import="java.sql.DriverManager"%> 
<%@page import="java.sql.Connection"%> 
<%@ 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> 
    <% 
     Class.forName("com.mysql.jdbc.Driver"); 
     Connection con=DriverManager.getConnection( 
     "jdbc:mysql://localhost:3306/demo","root","root"); //Connecting to the database 
     Statement stmt=con.createStatement(); 
     ResultSet rs=stmt.executeQuery("select * from student"); // fetching data from student table 
    %> 
    <table border="2"> 
     <tr> 
      <th>Roll</th> 
      <th>Name</th> 
      <th>Trade</th> 
      <th>Sem</th>   
     </tr> 
     <% 
      while(rs.next()) 
      { 
       %> 
       <tr> 
         <td><%=rs.getInt(1)%></td> // you can use column name instead of index 
         <td><%=rs.getString(2)%></td> 
         <td><%=rs.getString(3)%></td> 
         <td><%=rs.getString(4)%></td>  
       </tr> 
       <% 
      } 
     %> 

    </table> 
</body> 
</html> 
+0

你能解釋我這個..! – rango

+0

@rango請告訴我你不懂的部分代碼,以便我可以解釋你 – sForSujit

+0

感謝您的幫助,但我得到了我的錯。我寫了jbdc而不是jdbc。 – rango