user544079
user544079

Reputation: 16629

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?

Upvotes: 1

Views: 4902

Answers (4)

Yogesh Sati
Yogesh Sati

Reputation: 9

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>

Upvotes: 0

Brian Kates
Brian Kates

Reputation: 488

I would look into a framework like DataTables (http://www.datatables.net/) and move the database access code where it belongs on the server.

Upvotes: 0

place the mysql jar inside your lib folder .. in local you might of have done as a external jar

Upvotes: 1

thst
thst

Reputation: 4602

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?

Upvotes: 1

Related Questions