代码如下:
JSP:
<div class="container">
<table class="table table-striped table-hover">
<tr>
<th>编号</th>
<th>名字</th>
<th>价格</th>
<th>库存</th>
<th>生产日期</th>
<th>操作</th>
</tr>
<%
//定义集合 存储商品信息
ArrayList<Shops> list = new ArrayList<Shops>();
//通过JDBC把数据库值取给List
Class.forName("com.gzlg.bigdata.Database");
String name = "root";
String pwd = "123456";
String url = "jdbc:mysql://localhost:3306/wrok?useSSL=false&serverTimezone=UTC";
Connection connection = DriverManager.getConnection(url, name, pwd);
String sql = "SELECT * FROM shops;";
PreparedStatement pst = connection.prepareStatement(sql);
ResultSet res = pst.executeQuery();
while (res.next()) {
Shops shops1 = new Shops();
shops1.setShopid(res.getInt(1));
shops1.setShopName(res.getString(2));
shops1.setPrice(res.getFloat(3));
shops1.setCount(res.getInt(4));
shops1.setMakedate(res.getString(5));
list.add(shops1);
}
for (int i = 0; i < list.size(); i++) {
out.print(" <tr >\n" +
" <td >" + list.get(i).getShopid() + " </td >\n" +
" <td > " + list.get(i).getShopName() + " </td >\n" +
" <td > " + list.get(i).getPrice() + " </td >\n" +
" <td > " + list.get(i).getCount() + " </td >\n" +
" <td > " + list.get(i).getMakedate() + "</td >\n" +
" </tr >");
}
%>
</table>
</div>
然后通过Tomcat运行出来就报:
javax.servlet.ServletException: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/wrok?useSSL=false&serverTimezone=UTC 这个错误。
已经将jar放Tomcat目录的lib下还是运行不了。
你加载的驱动都写错了,报错的意思也是没有找到合适的驱动。
Class.forName("com.gzlg.bigdata.Database");
改为
Class.forName("com.mysql.jdbc.Driver");
如有帮助望采纳。点击我回答右上角【采纳】按钮。
那可以试试将获取连接的代码放到Java类中,然后在JSP中引用这个类获取连接
1.jar包是否在可访问目录
2.servlet版本多少 2.3不支持表达式 可在jsp头部添加<%@ page isELIgnored="false" %>
3.mysql加载驱动路径Class.forName("com.mysql.jdbc.Driver");