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
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
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);
}
}
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();
}
}
Few points to note and correct-
private Connection con;
but you are using it as statement = Conn.createStatement();
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
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.
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.