导入jdbc驱动程序包其实有很多种方法,但是不同的导包方式有不同的含义,
1、给Tomcat导包(表示服务器可能要用到数据库,例如数据源),如果是MyEclipse集成Tomcat,显然是要用“右键项目 -> Properties -> Java Build Path -> 右侧选项卡选择Libraries -> Add JARs...”。如果是非集成Tomcat,你必须把驱动程序包复制粘贴在Tomcat根目录“common\lib”文件夹下。
2、给Web项目导包(表示Web项目需要使用到数据库),把驱动程序包复制粘贴到“Web项目\WEB-INF\lib”文件夹下。同时也要在Tomcat的webapps\Web项目\WEB-INF\lib文件夹中粘贴驱动程序。
3、给单纯Java项目导包(表示Web项目需要使用到数据库),对着project单击右键,选择Bulid Path选择Add External Achieves再选择mysqljdbc.jar 或者将其添加到classpath中去。
问题:The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
代码实现:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.Connection,java.sql.DriverManager,java.sql.PreparedStatement,java.sql.ResultSet,java.sql.SQLException" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String driver = "com.mysql.cj.jdbc.Driver";
String passwrod = "april";
String userName = "root";
String url="jdbc:mysql://localhost:3306/weixin?useUnicode=true&allowPublicKeyRetrieval=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false";
String sql = "select * from userinfo";
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, userName,
passwrod);
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {%>
<table>
<tr>
<td><%= rs.getInt(1) %></td>
<td><%= rs.getString(2) %></td>
<td><%= rs.getString(3) %></td>
</tr>
</table>
<%
System.out.println("id : " + rs.getInt(1) + " name : "
+ rs.getString(2) + " avatar : " + rs.getString(3));
}
// 关闭记录集
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 关闭声明
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 关闭链接对象
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
%>
<table>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
</table>
</body>
</html>