creating a database in mysql from java

前端 未结 7 1944
情深已故
情深已故 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: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

提交回复
热议问题