4

I'm creating a java program that creates a MySQL database and table. I'm fairly new, so I'm learning as I go. I was able to create the Database and then I proceed to create the table. When I ran my program I got this exception error

java.sql.SQLException: No database selected

I understand the error that I have, but I don't know I can select the database when I already have my URL setup to my root.

String url = "jdbc:mysql://localhost:3306/";

Below is my code, any feedback is welcome and thank you.

import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.SQLException;


public class MysqlSetUp{

private static final String EMPLOYEE_TABLE = "create table MyEmployees3 ( "
  + "   id INT PRIMARY KEY, firstName VARCHAR(20), lastName VARCHAR(20), "
  + "   title VARCHAR(20), salary INT )";

public static Connection getConnection() throws Exception {
String driver = "org.gjt.mm.mysql.Driver";
String url = "jdbc:mysql://localhost:3306/";
String username = "root";
String password = "";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}
  public static void main(String args[]) {
  Connection conn = null;
  Statement stmt = null;
  try {
    conn = getConnection();
    stmt = conn.createStatement();
    stmt.executeUpdate("CREATE DATABASE Employess");
    stmt.executeUpdate(EMPLOYEE_TABLE);
    stmt.executeUpdate("insert into MyEmployees3(id, firstName) values(100, 'A')");
    stmt.executeUpdate("insert into MyEmployees3(id, firstName) values(200, 'B')");
    System.out.println("CreateEmployeeTableMySQL: main(): table created.");
      } catch (ClassNotFoundException e) {
    System.out.println("error: failed to load MySQL driver.");
    e.printStackTrace();
  } catch (SQLException e) {
    System.out.println("error: failed to create a connection object.");
    e.printStackTrace();
  } catch (Exception e) {
    System.out.println("other error:");
    e.printStackTrace();
  } finally {
    try {
      stmt.close();
      conn.close();
    } catch (SQLException e) {
      e.printStackTrace();
     }
   }
  }
 }
2
  • 1
    USE Employess after creating. Commented Feb 21, 2018 at 21:26
  • @clinomaniac you should add it as an answer, with java code of course. Since it is only what it is needed for his code. Commented Feb 21, 2018 at 21:29

3 Answers 3

6

You need to USE Employess after creating it.

...
stmt = conn.createStatement();
stmt.executeUpdate("CREATE DATABASE Employess");
stmt.executeUpdate("USE Employess");
stmt.executeUpdate(EMPLOYEE_TABLE);
...
Sign up to request clarification or add additional context in comments.

Comments

4

The answer provided by @clinomaniac is right and you should accept it as the right one. This one is to add some more context.

When you create a database as root, in order to work with it you have to select it, so you do it by issuing a USE statement USE yourDatabaseName as already said by @clinomaniac.

Another way to work with your database without using the use command is to prepend every command with your database name like this:

stmt.executeUpdate("insert into Employess.MyEmployees3(id, firstName) values(100, 'A')");
stmt.executeUpdate("insert into Employess.MyEmployees3(id, firstName) values(200, 'B')");

You would have to add this at your create table statement as well:

private static final String EMPLOYEE_TABLE = "create table Employess.MyEmployees3 ( "
   + "   id INT PRIMARY KEY, firstName VARCHAR(20), lastName VARCHAR(20), "
   + "   title VARCHAR(20), salary INT )";

Comments

3

You can also try adding the database name to the connection url. Modify your getConnection() method like this.

public static Connection getConnection(String dbName) throws Exception {
    String driver = "org.gjt.mm.mysql.Driver";
    String url = "jdbc:mysql://localhost:3306/" + ((dbName != null)? (dbName) : (""));
    String username = "root";
    String password = "";
    Class.forName(driver);
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
}

Call getConnection(null) for creating the database, and call getConnection("Employess") after creation of database.

conn = getConnection(null);
stmt = conn.createStatement();
stmt.executeUpdate("CREATE DATABASE Employess");
stmt.close();
conn.close();

conn = getConnection("Employess");
stmt = conn.createStatement();
stmt.executeUpdate(EMPLOYEE_TABLE);
stmt.executeUpdate("insert into MyEmployees3(id, firstName) values(100, 'A')");
stmt.executeUpdate("insert into MyEmployees3(id, firstName) values(200, 'B')");
System.out.println("CreateEmployeeTableMySQL: main(): table created.");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.