关于java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)错误

报错:

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)


代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@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=UTF-8">
<title>Insert title here</title>
</head>
<body bgcolor="#EEDDFF">
<%
Connection con;
Statement sql;
ResultSet rs;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (Exception e) {
out.println("忘记把MySQL数据库的JDBC数据库驱动程序复制到JDK的扩展目录中");
}
try {
String uri = "jdbc:mysql://localhost:3306/warehouse";
String user = "root";
String password = "acbz630756";
con = DriverManager.getConnection(uri,user,password);
sql = con.createStatement();
rs = sql.executeQuery("select * from product");
out.print("<table border=2>");
out.print("<tr>");
out.print("<th width=100>" + "产品号");
out.print("<th width=100>" + "名称");
out.print("<th width=50>" + "生产日期");
out.print("<th width=50>" + "价格");
out.print("</tr>");
while (rs.next()) {
out.print("<tr>");
out.print("<td>" + rs.getString(1) + "</td>");
out.print("<td>" + rs.getString(2) + "</td>");
out.print("<td>" + rs.getDate("madeTime") + "</td>");
out.print("<td>" + rs.getFloat("price") + "</td>");
out.print("<tr>");
}
out.print("</table>");
con.close();
} catch (SQLException e) {
out.print(e);
}
%>
</body>

</html>


报错:java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)

有两个原因:要么是数据库的访问的问题,要么是编码错误。

仔细一看,原来是我数据库的登陆密码错误。。。


con = DriverManager.getConnection(uri,user,password); 

若报错,须将jsp开头的jdbc的connection导入包代码删掉

阅读更多
下一篇报错:Type mismatch: cannot convert from java.sql.Connection to com.mysql.jdbc.Connection
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭