简体   繁体   English

与本地mysql数据库连接时,JDBC中的getConnection未连接

[英]getConnection not connection in JDBC on connecting with local mysql db

I am trying to establish a connection with mysql db running on my machine. 我正在尝试与在我的机器上运行的mysql db建立连接。 Here is the code - 这是代码-

static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
static final String DB_URL = "jdbc:mysql://localhost:3306/SecureSharedStore";

//  Database credentials
static final String USER = "root";
static final String PASS = "password";

public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
  //STEP 2: Register JDBC driver
  Class.forName("com.mysql.jdbc.Driver");

  //STEP 3: Open a connection
  System.out.println("Connecting to database...");
  conn = DriverManager.getConnection(DB_URL, USER, PASS);
 } catch(SQLException se){
  //Handle errors for JDBC
  se.printStackTrace();
}

When I run this expect to connect to the local db but it gives an error saying the following - 当我运行此命令时,它会连接到本地数据库,但会显示以下错误:

Connecting to database. 连接到数据库。

java.sql.SQLException: Access denied for user 'root'@'10.0.2.15' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3970)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3906)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:873)
at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1710)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1226)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2253)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2284)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2083)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:806)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:410)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:328)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at JDBCExample.main(JDBCExample.java:22)

When I see the first line I expect it to connect to 'root'@'localhost' but it is taking an IP address, that too not 127.0.0.1 当我看到第一行时,我希望它可以连接到“ root” @“ localhost”,但它使用的是IP地址,也不是127.0.0.1。

Can you please help me on what I am missing. 您能帮我解决我所缺少的吗? Pretty new to jdbc and using it specially on linux machines. 对jdbc来说是相当新的东西,特别是在Linux机器上使用它。

Stuck for too long on to it. 坚持太久了。

Thanks a lot in advance. 非常感谢。

At first try to use localhost address as 127.0.0.1 and make sure your server is started. 首先,尝试将本地主机地址用作127.0.0.1,并确保服务器已启动。

Then check for DB credentials: are you sure about your username & pass? 然后检查数据库凭证:您确定您的用户名和密码通过吗?

Hope that'll help 希望能对您有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM