我正在尝试将 JSP 连接到数据库而不执行任何 DDL 或 DML 命令。
这是我的 JSP:
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<html> <head> <title>Connection with mysql database</title>
</head>
<body>
<h1>Connection status</h1>
<%
try {
String connectionURL = "jdbc:mysql://localhost:3306/bharath";
Connection connection = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "root", "root");
if(!connection.isClosed())
%>
<font size="+3" color="green"></b>
<%
out.println("Successfully connected to " + "MySQL server using TCP/IP...");
connection.close();
}
catch(Exception ex){
%></font>
<font size="+3" color="red"></b>
<%
out.println("Unable to connect to database.");
}
%>
</font></body> </html>
catch 块中的异常被执行。那么 try 块中可能出了什么问题。我使用默认设置安装了 MySql,用户为"root"
,数据库名称为"bharath"
.
Exception -
java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:919) at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1694) at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1244) at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2412) at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2445) at