servlet:数据库连接池问题,Cannot create JDBC driver of class '' for connect URL 'null'
webdemo.xml(路径:C:\apache-tomcat-8.0.28\conf\Catalina\localhost)<Context path="/webdemo" docBase="webdemo" debug="0">
<Resource name="jdbc/webdb" auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://127.0.0.1:3306/webdb?characterEncoding=utf-8"
username="root"
password="020500"
maxActive="5"
maxIdle="2"
maxWait="3000"/>
</Context>
web.xml
<web-app>
<display-name>Project Name</display-name>
<servlet>
<servlet-name>ViewDictionary</servlet-name>
<servlet-class>chapter4.ViewDictionary</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ViewDictionary</servlet-name>
<url-pattern>/servlet/ViewDictionary</url-pattern>
</servlet-mapping>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/webdb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
doget方法
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html;charset=utf-8");
PrintWriter printWriter=response.getWriter();
try {
javax.naming.Context ctxContext=new javax.naming.InitialContext();
javax.sql.DataSource dsDataSource=(javax.sql.DataSource) ctxContext.lookup("java:/comp/env/jdbc/webdb");
Connection connection= dsDataSource.getConnection();
String sqlString="select * from t_dictionary";
PreparedStatement statement=connection.prepareStatement(sqlString);
ResultSet resultSet=statement.executeQuery();
StringBuilder stringBuilder=new StringBuilder();
stringBuilder.append("<table border='1'>");
stringBuilder.append("<tr><td>书名</td><td>价格</td></tr>");
while(resultSet.next()){
stringBuilder.append("<tr><td>"+resultSet.getString("english")+"</td><td>"+resultSet.getString("chinese")+"</td></tr>");
}
stringBuilder.append("</table>");
printWriter.print(stringBuilder.toString());
statement.close();
} catch (Exception e) {
e.printStackTrace();
}
}
求解,配置应该没错。
还有用DriverManager.getConnection(url, username, password);能连上