10

你能帮忙解决这个问题吗?

我正在尝试创建并使用一个名为 TIGER 的数据库。

如果我在 MySQL 中创建数据库并且它运行完美,我没有问题。

我想做的是从Java创建它。因此,当代码第一次运行时,它会创建数据库作为初始启动的一部分。如果可能的话,我想用一种干净的方法把它装箱。

有人可以告诉我您实际放置代码的位置吗

这是代码

    private String jdbcDriver = "com.mysql.jdbc.Driver";
    private String dbAddress = "jdbc:mysql://localhost:3306/";
    private String dbName = "TIGER";
    private String userName = "root";
    private String password = "";

    private PreparedStatement statement;
    private ResultSet result;
    private Connection con;

    public DbStuff() {
        try {
            Class.forName(jdbcDriver);
            con = DriverManager.getConnection(dbAddress + dbName, userName, password);
        } 

        catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

这是创建数据库的代码

    con = DriverManager.getConnection("jdbc:mysql://localhost/?user=root&password=rootpassword"); 
    statement = Conn.createStatement();
    int myResult = statement.executeUpdate("CREATE DATABASE TIGER");

提前致谢,感谢任何和所有帮助

对不起,因为我是一个长期读者,但一个新作家的错误。

当我尝试运行代码的主要部分时,它会生成 SQLException,因为数据库不存在。此时我想捕获异常并在此时创建数据库。但是当我尝试这个时,它不会创建数据库。

4

7 回答 7

16

我建议使用这段代码

private String jdbcDriver = "com.mysql.jdbc.Driver";
private String dbAddress = "jdbc:mysql://localhost:3306/TIGER?createDatabaseIfNotExist=true";
private String userName = "root";
private String password = "";

private PreparedStatement statement;
private ResultSet result;
private Connection con;

public DbStuff() {
    try {
        Class.forName(jdbcDriver);
        con = DriverManager.getConnection(dbAddress, userName, password);
    } 

    catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
    catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
于 2017-03-21T08:26:25.787 回答
14

你总是可以跑

int myResult = statement.executeUpdate("CREATE DATABASE IF NOT EXISTS TIGER;")

如果这是在项目启动时运行的,它将简单地检查数据库是否存在,如果不存在,它将创建新数据库。

供参考:http ://dev.mysql.com/doc/refman/5.0/en/create-database.html

于 2013-09-25T15:40:20.087 回答
6

试试这个代码。

public class DbStuff {

    private static String jdbcDriver = "com.mysql.jdbc.Driver";
    private static String dbName = "TIGER";


    public static void main(String[] args) throws Exception {
        Class.forName(jdbcDriver);
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/?user=root&password=");
        Statement s = conn.createStatement();
        int Result = s.executeUpdate("CREATE DATABASE "+dbName);
    }
}
于 2013-09-25T15:39:47.797 回答
1

首先,

我要感谢所有回答的人,获得不同的观点和意见确实非常有帮助。

这是我在一起的解决方案。

    public class DbStuff {
    private String jdbcDriver = "com.mysql.jdbc.Driver";
    private String dbAddress = "jdbc:mysql://localhost:3306/";
    private String userPass = "?user=root&password=";
    private String dbName = "TIGER";
    private String userName = "root";
    private String password = "";

    private PreparedStatement statement;
    private ResultSet result;
    private Connection con;

    public DbStuff() {

        try {
            Class.forName(jdbcDriver);
            con = DriverManager.getConnection(dbAddress + dbName, userName, password);
        } 

        catch (ClassNotFoundException e) {
            e.printStackTrace();
        } 
        catch (SQLException e) {
            createDatabase();
        }
    }

    private void createDatabase() {
        try {
        Class.forName(jdbcDriver);
        con = DriverManager.getConnection(dbAddress + userPass);
        Statement s = con.createStatement();
        int myResult = s.executeUpdate("CREATE DATABASE " + dbName);
        } 
        catch (ClassNotFoundException | SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    }

随意评论任何代码。我知道使用 e.printStackTrace() 并不是最好的方法,别担心,稍后会修改它。

于 2013-09-26T06:47:52.123 回答
1

解决问题的简单方法是:

package Database;
import java.sql.*;

public class Database {

 public static void main(String[] args) {
    final  String URl = "JDBC:mysql:/localhost:3306/info";
    final  String id = "root";
    final  String  password = "";

    try
    {
        Connection con1 = DriverManager.getConnection(URl,id,password);
        Statement s1 = con1.createStatement();
        String s = "CREATE DATABASE CLASSIFIED";
        s1.executeUpdate(s);

    }
    catch(SQLException err)
    {
        System.out.println(err.getMessage());
    }

 }

}
于 2016-06-25T02:56:26.983 回答
0

在一个不相关的(与问题)注释上:

我认为以编程方式生成数据库并不是一个好主意。最好使用数据库附带的实用工具。使用 MySQL 的每个人都几乎肯定会熟悉该实用工具,而不必熟悉 Java 代码、更改、编译和运行它来进行更改。此外,该实用程序具有一组您的 Java 代码可能不具备的丰富功能,例如分配权限、索引、设置外键等。

于 2013-09-25T18:55:23.820 回答
0

需要注意和纠正的几点-

  1. 您已将连接对象声明为,private Connection con;但您将其用作statement = Conn.createStatement();
  2. 您已声明对 Prepared Statement 的引用,private PreparedStatement statement;但您正在创建 Statement 的实例statement = Conn.createStatement();。您的代码应该是 -

    try {
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost/?user=root&password=rootpassword");
        Statement  statement = con.createStatement();
        int myResult = statement.executeUpdate("CREATE DATABASE IF NOT EXISTS TIGER"); //should get 0
    }
    catch (SQLException e) {
        System.out.println("Database creation failed");
        e.printStackTrace();
    } 
    

请注意,当 executeUpdate 的返回值为 0 时,它可能意味着以下两种情况之一:

  • 执行的语句是影响零行的更新语句。

  • 执行的语句是 DDL 语句。

文档

于 2015-02-14T05:41:39.363 回答