简体   繁体   English

Java的MySQL连接错误

[英]MySQL connection error with java

Eclipse gives me a lot of errors when i try to connect to my database. 当我尝试连接到数据库时,Eclipse给了我很多错误。 Here is the code: 这是代码:

public static void main(String[] args) throws Exception{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
    "jdbc:mysql://127.0.0.1:3306/Turniir", "root", "root");
    System.out.println("Connected to MySQL"); 
con.close(); 
}

First it says Unknown database 'turniir'. 首先,它说未知数据库“ turniir”。 In mysql workbench, there is a connection 127.0.0.1:3306 , it uses a schema , in workbench i can insert rows and i get a connection tho. 在mysql工作台中,有一个连接127.0.0.1:3306,它使用一个schema,在工作台中,我可以插入行并获得连接。 The user and pw should be root , atleast i use them to log in. But there are like 30 errors ... 用户和pw应该是root,至少我要用它们登录。但是有30个错误...

Also in eclipse , i have mysql connector 5.1.27 under referenced libraries and eclipse does not show any red flags when i examine my code , only when i run. 同样在eclipse中,我在引用的库下有mysql连接器5.1.27,当我检查我的代码(仅在运行时)时eclipse不会显示任何红色标记。

I think the problem is in jdbc:mysql://127.0.0.1:3306/Turniir", "root", "root" this code, either i have somehow changed server or i don't know what password or username is for server or i don't know the right server ip/name ... 我认为问题出在jdbc:mysql://127.0.0.1:3306 / Turniir”,“ root”,“ root”此代码,我以某种方式更改了服务器,或者我不知道服务器的密码或用户名或者我不知道正确的服务器IP /名称...

Database : http://www.upload.ee/image/3724759/mysqldb.png 数据库: http : //www.upload.ee/image/3724759/mysqldb.png

Connection thingy : http://www.upload.ee/image/3724938/cra0.png 连线内容: http ://www.upload.ee/image/3724938/cra0.png

These are the errors : 这些是错误:

Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:          Unknown database 'turniir'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:928)
at    com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1750)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1290)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2493)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2526)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2311)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:834)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:347)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Program.main(Program.java:12)

You said Turiir is not the database name. 您说的是Turiir不是数据库名称。 That's why you're getting the error 这就是为什么你会得到错误

"jdbc:mysql://127.0.0.1:3306/Turniir", "root", "root");

   // This need to be database ^^^^

                      // this username ^^^^

                              // this password  ^^^^

If you run mysql and type show databases and turniir does't show up, then it's not a database. 如果您运行mysql并键入show databasesturniir没有出现,则它不是数据库。 Replace the above code with a valid datadase. 将上面的代码替换为有效的datadase。

可能区分“ Turniir”的大小写,错误消息显示“ Turniir”

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

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