简体   繁体   中英

MySQL connection error with java

Eclipse gives me a lot of errors when i try to connect to my database. 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'. 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. The user and pw should be root , atleast i use them to log in. But there are like 30 errors ...

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.

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 ...

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

Connection thingy : 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. 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. Replace the above code with a valid datadase.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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