unknown database in jdbc
Asked Answered
K

12

16

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

Kenton answered 5/4, 2013 at 10:53 Comment(3)
What's the name of your database?Subaqueous
it tells you that it can't find kholofelodb in MySQLLancelot
Have you verified that the MySQL server is running on your local machine?Skateboard
T
9

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

Thetic answered 5/4, 2013 at 10:57 Comment(0)
I
16

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.

Inaction answered 10/6, 2016 at 6:25 Comment(1)
Make sure to not miss the semicolon in the command of listing the databases.Franciskus
T
9

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

Thetic answered 5/4, 2013 at 10:57 Comment(0)
T
6

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

Tutor answered 21/10, 2013 at 13:8 Comment(0)
C
1

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

Cottonseed answered 26/1, 2020 at 17:34 Comment(1)
Well formatted answers get more attention on Stack-overflow. I suggest you format any code fragments so they stand out from normal text. More information here stackoverflow.com/help/how-to-answerCurcuma
Z
0

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.

Zanezaneski answered 5/4, 2013 at 10:57 Comment(0)
W
0

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*

Winterize answered 5/7, 2015 at 1:50 Comment(0)
J
0

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

Jacobsen answered 14/10, 2020 at 11:2 Comment(0)
M
0

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-

Monanthous answered 7/8, 2021 at 21:47 Comment(0)
P
0

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

Polygynist answered 20/9, 2021 at 18:33 Comment(0)
C
0

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

Combustible answered 2/9, 2023 at 16:23 Comment(0)
R
0

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
Refractory answered 10/4, 2024 at 5:30 Comment(0)
P
0

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;

Pasteurism answered 27/5, 2024 at 6:11 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Spray

© 2022 - 2025 — McMap. All rights reserved.