Sarah Szabo
Sarah Szabo

Reputation: 10815

Problems with SQL Connection in Java

Alright, so I've decided to follow a tutorial on Youtube about SQL databases, and I created a database on PhpMyAdmin then exported (what I think is a table, not the entire database) an SQL backup file (customers.sql) to my desktop. I want to connect to it using JDBC, and I tried it with the code below.(note that I ensured that the driver was installed correctly with Class.forName("com.mysql.jdbc.Driver"); For debugging purposes please assume that only the file exists on my desktop, I have the driver, the following code, and nothing else. Also, I'm not sure how to get the sql file on localhost.

Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/customers","root", "");

This code generated an exception:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'customers'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:395)
at com.mysql.jdbc.Util.getInstance(Util.java:370)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1038)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:925)
at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1704)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1250)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2467)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2500)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2285)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:818)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:31)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:395)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:400)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
at java.sql.DriverManager.getConnection(DriverManager.java:579)
at java.sql.DriverManager.getConnection(DriverManager.java:221)
at com.gmail.PhysicistSarah.TestProject.Database.DatabaseDriver.<init>(DatabaseDriver.java:32)
at com.gmail.PhysicistSarah.TestProject.Core.Main.databaseTrial(Main.java:99)
at com.gmail.PhysicistSarah.TestProject.Core.Main.main(Main.java:49)

Upvotes: 0

Views: 847

Answers (3)

JB Nizet
JB Nizet

Reputation: 691635

You can't connect to a SQL file. A SQL file is just that: a file, with some text inside. If you want to connect to a MySQL database running on localhost, you need to install MySQL on your machine, start it, create a database, create tables in it, and populate the tables.

Follow the MySQL documentation for how to do that.

Upvotes: 3

KhAn SaAb
KhAn SaAb

Reputation: 5366

check for your (customers)database spelling in mysql database by query show databases; if it display your customers database then copy that name and put in your java code. other wise create by firing a query create database customers;

Upvotes: 0

Juned Ahsan
Juned Ahsan

Reputation: 68715

Error clearly mentions that you don't have a database called 'customers'. Make sure that you have the database in mysql before connecting it through jdbc. Or maybe some typo errors in schema name in mysql or in jdbc code.

Upvotes: 0

Related Questions