在Java类中自己测试,结果正常
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://127.0.0.1:3306/mysql";
String user = "root";
String password = "32147";
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
if(!conn.isClosed())
System.out.println("Succeeded connecting to the Database!");
Statement statement = conn.createStatement();
String sql = "select * from person";
ResultSet rs = statement.executeQuery(sql);
while(rs.next()) {
System.out.println(rs.getString("name"));
}
} catch (Exception e) {
用eclipse创建了一个简单的web项目
在index.jsp中把上面代码格式了下写进去,报ClassNotFound com.mysql.jdbc.Driver
<%@page import="javax.sql.*"%>
<%@page import="java.sql.*"%>
<%@page import="javax.naming.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://127.0.0.1:3306";
String user = "root";
String password = "32147";
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
if(!conn.isClosed())
out.println("Succeeded connecting to the Database!");
Statement statement = conn.createStatement();
String sql = "select * from mysql.person";
ResultSet rs = statement.executeQuery(sql);
while(rs.next()) {
out.println(rs.getString("name"));
}
} catch (Exception e) {
e.printStackTrace();
}
%>
</body>
</html>
搞不懂为什么
------解决思路----------------------
导入mysql.jar了吗?
------解决思路----------------------
如果你在eclipse中引用 了,但是运行起来没找到。检查一下你的jar是不是发布到lib目录中去了。
Eclipse --> 项目右键 --> Properties --> Deployment Assembly --> Add --> Java Build Path Entries
然后找到你的jar包,加进去。再运行的时候,会自动复制jar到lib中去。