简体   繁体   中英

Display table data from mysql to jsp page

I have

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

<html>
<body>
<div id="content">


    <p>Displaying table contents: </p>

    <table border="0" cellpadding="10">
        <thead>
            <tr>
                <th>Id</th>
                <th>Name</th>
                <th>Company</th>
                <th>Salary</th>
            </tr>
        </thead>
        <tbody>


            <%
                Class.forName("com.mysql.jdbc.Driver");
                Connection conn = null;
                conn = DriverManager.getConnection("jdbc:mysql://localhost:8080/Connection", "username", "password");
                Statement stmt = null;
                stmt = conn.createStatement();
                String query = "select * from employeee";
                ResultSet rs = null;
                rs = stmt.executeQuery(query);
                while(rs.next()){
            %>
            <tr>
                <%
                    int id = rs.getInt("id");
                    String name = rs.getString("name");
                    String company = rs.getString("company");
                    int salary = rs.getInt("salary");
                %>
                <td><%=id %></td>
                <td><%=name %></td>
                <td><%=company %></td>
                <td><%=salary %></td>
            </tr>               

            <%      
                }
            %>

        </tbody>
    </table>
</div>
</body>
</html>

I am getting a Class Not Found Exception for com.mysql.jdbc.Driver

I have added the external jar to the project and using this same code i get the results in a normal java file.

What is wrong here?

First of, JSP is the presentation layer of your application, it should not know of the database. Remove the database code from the JSP into a bean. The JSP reads the bean and presents the data.

That said, the JDBC driver is an external dependency to your server running the JSP and must be added to the classpath of the server.

Did you add it in the server or in the project, so that is actually really in the war file?

将mysql jar放在您的lib文件夹中..在本地,您可能已将其作为外部jar

我将研究诸如DataTables( http://www.datatables.net/ )之类的框架,并将数据库访问代码移至服务器上的位置。

If you are getting the

CLASS NOT FOUND EXCEPTION

then place your SQL connector jar file inside the Webinf/library folder

or if data is not retrieving from the database then use these line of statements after your while block and delete your code line.

<tr>
  <td><%=rs.getInt("ID") %></td>
  <td><input type="text" value="<%=rs.getString("NAME") %>"></td>
  <td><input type="text" value="<%=rs.getString("COMPANY") %>"></td>
  <td><input type="text" value="<%=rs.getInt("SALARY") %>"></td>
</tr>

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