我有过
<%@ 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>我得到了一个Class Not Found Exception for com.mysql.jdbc.Driver
我将外部jar添加到项目中,并使用相同的代码在一个普通的java文件中获得结果。
这里怎么了?
发布于 2015-03-28 19:30:30
将mysql jar放在lib文件夹中。在本地,您可能会将其作为外部jar来完成。
发布于 2015-03-28 19:23:22
首先,JSP是您应用程序的表示层,它不应该知道数据库。将JSP中的数据库代码删除到bean中。JSP读取bean并显示数据。
也就是说,JDBC驱动程序是运行JSP的服务器的外部依赖项,必须添加到服务器的类路径中。
您是在服务器中还是在项目中添加了它,所以这实际上是在war文件中吗?
发布于 2015-03-28 21:39:41
我将查看像DataTables (http://www.datatables.net/)这样的框架,并将数据库访问代码移到它属于服务器的位置。
https://stackoverflow.com/questions/29321483
复制相似问题