<%@ page language="java" contentType="text/html; charset=gb2312" pageEncoding="gb2312"%> <%@page import="java.sql.*"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>數據庫鏈接測試</title> </head> <body> <% try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/test"; Connection connection = DriverManager.getConnection(url, "root", "root"); if (connection != null) { out.println("<h3>數據庫鏈接成功!</h3>"); } else { out.println("<h3>數據庫鏈接失敗!</h3>"); } String sql = "select*from friends"; Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(sql); while (resultSet.next()) { out.println(resultSet.getString(1)); out.println(resultSet.getString(2) + "<br>"); } resultSet.close(); statement.close(); connection.close(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } %> </body> </html>
在採用上述的一個jsp頁面來測試鏈接數據庫時,出現了上圖所示的錯誤,mysql驅動包文件mysql-connector-java-5.1.7-bin我是按正確的方式導入的,但是仍然出現了上述的錯誤,瀏覽器頁面沒有任何的異常提示信息,最終經過將mysql-connector-java-5.1.7-bin文件放到Tomcat中的lib文件夾下才解決了上述的異常問題!html