【问题标题】:creating a database in mysql from java从java在mysql中创建数据库
【发布时间】:2013-10-01 07:01:45
【问题描述】:

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

我正在尝试创建并使用一个名为 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,因为数据库不存在。此时我想捕获异常并在此时创建数据库。但是当我尝试这个时,它不会创建数据库。

【问题讨论】:

标签: java mysql database jdbc


【解决方案1】:

我建议使用这个sn-p的代码

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

【讨论】:

  • 我认为这是创建数据库的最佳方式。我们不需要使用这个 createDatabaseIfNotExist=true 执行命令
【解决方案2】:

你总是可以跑

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

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

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

【讨论】:

    【解决方案3】:

    试试这个代码。

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

    【讨论】:

      【解决方案4】:

      首先,

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

      这是我共同拥有的解决方案。

          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() 不是最好的方法,别担心,稍后会修改。

      【讨论】:

        【解决方案5】:

        解决问题的简单方法是:

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

        【讨论】:

          【解决方案6】:

          关于(与问题)无关的注释:

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

          【讨论】:

            【解决方案7】:

            需要注意和纠正的几点-

            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 语句。

            Documentation

            【讨论】:

              猜你喜欢
              • 2010-10-17
              • 1970-01-01
              • 2013-10-01
              • 2012-05-15
              • 1970-01-01
              • 2018-03-02
              • 1970-01-01
              相关资源
              最近更新 更多