creating a database in mysql from java

前端 未结 7 1913
情深已故
情深已故 2020-12-30 12:06

Could you help with this problem.

I\'m trying to create and then use a database called TIGER.

I have no problem if I create the database in MySQL and it runs

相关标签:
7条回答
  • 2020-12-30 12:35

    You could always run

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

    If this is ran on project start up it will simply check if the database exists and if it doesn't it will create the new database.

    For reference: http://dev.mysql.com/doc/refman/5.0/en/create-database.html

    0 讨论(0)
  • 2020-12-30 12:44

    Try with this code.

    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);
        }
    }
    
    0 讨论(0)
  • 2020-12-30 12:44

    I suggest to use this snippet of code

    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();
        }
    }
    
    0 讨论(0)
  • 2020-12-30 12:50

    Few points to note and correct-

    1. You have declared Connection Object as private Connection con; but you are using it as statement = Conn.createStatement();
    2. You have declared a reference to Prepared Statement private PreparedStatement statement; but you are creating instance of Statement statement = Conn.createStatement();. Your code should be -

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

    Note that when the return value for executeUpdate is 0, it can mean one of two things:

    • The statement executed was an update statement that affected zero rows.

    • The statement executed was a DDL statement.

    Documentation

    0 讨论(0)
  • 2020-12-30 12:53

    On an unrelated (to the question) note:

    I don't think it’s ever a good idea to programmatically generate the database. You will be better off using the utility tool that comes with your database. Everyone that works with MySQL will almost certainly be familiar with the utility tool and will not have to become familiar with your Java code, change it, compile it, and run it to make a change. Also, the utility has a rich set of features that your Java code probably doesn't have such as assigning permissions, indexing, setting up foreign keys, etc.

    0 讨论(0)
  • 2020-12-30 12:59

    First of all,

    I would like to thank all those who answered it was very helpful indeed to get different views and opinions.

    Here is the solution that I have together.

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

    Feel free to comment on any of the code. I know using the e.printStackTrace() is not the best way forward, don't worry It will be modified later on.

    0 讨论(0)
提交回复
热议问题