Java:在同一个类中创建Mysql数据库和表

时间:2018-02-21 21:25:22

标签: java mysql

我正在创建一个创建MySQL数据库和表的java程序。我很新,所以我正在学习。我能够创建数据库,然后继续创建表。当我运行我的程序时,我遇到了这个异常错误

java.sql.SQLException: No database selected

我理解我所遇到的错误,但是当我已经将URL设置为root时,我不知道我可以选择数据库。

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

以下是我的代码,欢迎任何反馈,谢谢。

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();
     }
   }
  }
 }

3 个答案:

答案 0 :(得分:6)

创建后需要USE Employess

...
stmt = conn.createStatement();
stmt.executeUpdate("CREATE DATABASE Employess");
stmt.executeUpdate("USE Employess");
stmt.executeUpdate(EMPLOYEE_TABLE);
...

答案 1 :(得分:4)

@clinomaniac提供的答案是正确的,你应该接受它作为正确的答案。这是为了增加一些背景。

当您以root身份创建数据库时,为了使用它,您必须选择它,因此您可以通过发出@ {1}}语句USE来执行此操作,如@clinomaniac已经说过的那样。

在不使用USE yourDatabaseName命令的情况下使用数据库的另一种方法是在每个命令前加上您的数据库名称,如下所示:

use

您还必须在create table语句中添加它:

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

答案 2 :(得分:3)

您还可以尝试将数据库名称添加到连接URL。像这样修改您的getConnection()方法。

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;
}

调用getConnection(null)创建数据库,并在创建数据库后调用getConnection("Employess")

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