Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 376
kholofelo Maloma Avatar asked Apr 05 '13 10:04

kholofelo Maloma


People also ask

How do I fix no suitable driver for JDBC?

In brief, we can say that such an error occurs when no JDBC JAR file is added to the classpath of Java. Just we need to add the JAR file to the classpath and then execute the code. The code will hopefully get executed with success.

How do you fix Java SQL SQLException no suitable driver found?

This error occurs if JDBC is not able to find a suitable driver for the URL format passed to the getConnection() method e.g. "jdbc:mysql://" in our case. In order to solve this error, you need the MySQL JDBC driver like mysql-connector-java-5.1. 36. jar in your classpath.

What is Java SQL SQLSyntaxErrorException?

public class SQLSyntaxErrorException extends SQLNonTransientException. The subclass of SQLException thrown when the SQLState class value is '42', or under vendor-specified conditions. This indicates that the in-progress query has violated SQL syntax rules.


2 Answers

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.

like image 121
tk_ Avatar answered Oct 05 '22 09:10

tk_


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

like image 30
radha krishna Avatar answered Oct 05 '22 08:10

radha krishna