Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Creating Mysql Database and Table in the same class

Tags:

java

mysql

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();
     }
   }
  }
 }
like image 569
Jeff Avatar asked Dec 10 '22 07:12

Jeff


2 Answers

You need to USE Employess after creating it.

...
stmt = conn.createStatement();
stmt.executeUpdate("CREATE DATABASE Employess");
stmt.executeUpdate("USE Employess");
stmt.executeUpdate(EMPLOYEE_TABLE);
...
like image 183
clinomaniac Avatar answered Dec 30 '22 02:12

clinomaniac


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 )";
like image 23
Jorge Campos Avatar answered Dec 30 '22 02:12

Jorge Campos