kholofelo Maloma
kholofelo Maloma

Reputation: 1003

unknown database in jdbc

I am using JDBC and new to it. but I keep getting this runtime exception:

   connecting to psysical database...
   com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'kholofelodb'
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:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:943)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:4113)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1308)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2336)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2369)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2153)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:792)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
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:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:381)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
at java.sql.DriverManager.getConnection(DriverManager.java:579)
at java.sql.DriverManager.getConnection(DriverManager.java:221)
at com.psybergate.database.SimbleCode.main(SimbleCode.java:22)

Here is the gave code

try {
            String connectionURL =      "jdbc:mysql://localhost:3306/kholofelodb";
            Class.forName("com.mysql.jdbc.Driver");

            System.out.println("connecting to psysical database...");
            Connection conn = DriverManager.getConnection(connectionURL, USER,
                    PASS);

            Statement statement = conn.createStatement();
            System.out.println("Connection has been made");

            Scanner keyBoardScanner = new Scanner(System.in);
            System.out.println("Enter table name:");
            String tableName = keyBoardScanner.nextLine();

            System.out.println("Creating table...");
            statement.executeQuery("create table " + tableName
                    + " (name , age ,salary)");
            System.out.println("Table successfully created");
            System.out.println("Inserting data into the table ...");
            statement.executeUpdate("insert into " + tableName
                    + "values (kholofelo , 21 , 9969696)");
        } 

how do I get this code to work? I am only a beginner with JDBC....

for the above code; PASS = "passowrd", USER = "root"

I had a problem with port which it was fixed through this site

thanks a lot

Upvotes: 16

Views: 73807

Answers (12)

vikram singh
vikram singh

Reputation: 1

I had an issue where i was creating a database with name order-service. Because it has "-" dash in the middle, it supposed to be passed in backticks when creating database. Example - create database order-service;

Upvotes: 0

Manish Sahu
Manish Sahu

Reputation: 3

In my case it was

spring.datasource.url=jdbc:mysql://localhost:3306/books/

which was giving me this error. I just removed the last '/' and it worked.

spring.datasource.url=jdbc:mysql://localhost:3306/books

Upvotes: 0

mich900
mich900

Reputation: 11

FIX FOR THE NON TERMINAL SAVY

... here is how I solved this problem for me by using MySql Workbench.

  1. I Made sure I knew the name of the database I was trying to connect to by looking in my application.properties file. My database was called rewards.

spring.datasource.url=jdbc:mysql://localhost:3306/rewards

2)Then I opened my MySql workbench file which I had named rewards....I thought that was the name of the database but it wasn't! When I did the "show databases;" command in MySql, "rewards" was not listed. I needed to create a new schema called "rewards" within my MySql Workbench file.

Here's an annotated screen shot of MySql workbench and the commands I used in MySql to see if my database was created yet. If you are a visual person, LOOK AT THIS IMAGE Creating/Naming the Database Correctly

Upvotes: 0

Lion
Lion

Reputation: 11

You've probably not created a database or the name of it is not "kholofelodb". If you haven't created one, do it. If you have but you're not sure about it's name, on command prompt type C:\ MariaDB location> cd bin, then mysql -u root -p and enter your password. Then type show databases; . Your database will show up with the name of it. Mine for instance is called "sys" enter image description here

Upvotes: 0

Jessica Woods
Jessica Woods

Reputation: 3

I had this same problem and its actually because they tell you to use SQLWorkbench....which is a nice set up but when you do dev things like this sometimes your work on that GUI won't actually save to the mySQL, I don't know why that happens. Workbench is a nice thing to have. I am tediously doing it step by step through this website: https://dev.mysql.com/doc/mysql-getting-started/en/ ...its old school...but its working, I just threw away my workbench LOL-

Maybe there is a misstep in how I set up workbench? I don't know but I think I am just going to stick to the old school set up-

Upvotes: 0

user14448527
user14448527

Reputation: 1

yeah this case arrived to me also and actually my database name was correct but the problem is in my port number I was using WAMPP server whose port number is 3308 and i wasted my time by using 3306 so u can check that are u using any other app then first it is recommended to check the port value of it...........

Upvotes: 0

tk_
tk_

Reputation: 17418

Log in to the mysql server,

$ mysql -u <username> -p<password>

List all the databases,

mysql> show databases;

Check whether your DB name is there,

+--------------------+
| Database           |
+--------------------+
| information_schema |
| kholofelodb        |
| mysql              |
| phpmyadmin         |
+--------------------+

Please note: database names are case sensitive.

Hope this helps.

Upvotes: 16

G.Sharma
G.Sharma

Reputation: 21

This error occurs when server is not able to find the database. This can be due to a wrong port given in the database URL, for example

String DB_URL = "jdbc:mysql://localhost:3307/database_name"

So check this port number (3307 in the above case) with the port mentioned in the Xampp controler (go to Xampp controller and then configuration of MySql).

Upvotes: 1

MaratMir
MaratMir

Reputation: 99

I had the same issue and found that the cause of it was: The name of the database here is CASE SENSITIVE! So, I was trying to connect using a database name like "test2", but the database name actually was "Test2".

Upvotes: 6

praveen kumar bommali
praveen kumar bommali

Reputation: 145

Issue is with your database names only. First create on database in mysql* create database connection .Concentrate on these areas : database name : URL: mention database name properly that database name should be reflects database in mysql*

Upvotes: 0

radha krishna
radha krishna

Reputation: 230

please check in mysql database whether the database name 'kholofedb' is existed or not

i think you haven't created

please check it once if no create it and its related tables also

Upvotes: 9

blackpanther
blackpanther

Reputation: 11486

It seems that the fault lies with the name of your database. MySQL is the database engine, but within MySQL you can have a number of databases with their own tables. Once you get the database name correct, you may find that the problem will resolve.

Upvotes: 0

Related Questions