Create MySQL database from Java
The database isn't required in the jdbc connection, so you can do something like recommended at http://forums.mysql.com/read.php?39,99321,102211#msg-102211 and http://marc.info/?l=mysql-java&m=104508605511590&w=2:
Conn = DriverManager.getConnection
("jdbc:mysql://localhost/?user=root&password=rootpassword");
s=Conn.createStatement();
int Result=s.executeUpdate("CREATE DATABASE databasename");
creating a database in mysql from java
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);
}
}
Connect Java to a MySQL database
DriverManager
is a fairly old way of doing things. The better way is to get a DataSource
, either by looking one up that your app server container already configured for you:
Context context = new InitialContext();
DataSource dataSource = (DataSource) context.lookup("java:comp/env/jdbc/myDB");
or instantiating and configuring one from your database driver directly:
MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setUser("scott");
dataSource.setPassword("tiger");
dataSource.setServerName("myDBHost.example.org");
and then obtain connections from it, same as above:
Connection conn = dataSource.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT ID FROM USERS");
...
rs.close();
stmt.close();
conn.close();
Java: Creating Mysql Database and Table in the same class
You need to USE Employess
after creating it.
...
stmt = conn.createStatement();
stmt.executeUpdate("CREATE DATABASE Employess");
stmt.executeUpdate("USE Employess");
stmt.executeUpdate(EMPLOYEE_TABLE);
...
Getting user input to create mysql database
To create database you don't need to use 'DB_NAME'
you have to use the name without ''
like this :
String EMPLOYEE_DB = " CREATE DATABASE " + Data;
Like @baao mention in comment, your way is not secure and open to an SQL Injection you have to check the name before you use it in your query.
Creating a database and tables in MySQL using a JDBC application
To create tables, you should use the "CREATE TABLE" SQL syntax. This can be done on the database management program but also in JDBC.
stmt = conn.createStatement();
String sql = "CREATE TABLE password" +
"(passwordId INTEGER not NULL, " +
" password VARCHAR(20), " +
" PRIMARY KEY ( passwordId ))";
stmt.executeUpdate(sql);
Related Topics
Is There a Good Reason to Use "Printf" Instead of "Print" in Java
How to Compile a Java Source File Which Is Encoded as "Utf-8"
This: Cannot Use This in Static Context
Adding Points to Xyseries Dynamically with Jfreechart
How to Check If a Date Object Equals Yesterday
How to Read a File from a Certain Offset in Java
How Does the Bitwise & (And) Work in Java
Documentlistener Java, How to Prevent Empty String in Jtextbox
Difference Between Double and Double in Comparison
Iterating Through a List in Reverse Order in Java
Extract String Between Two Strings in Java
Spring Boot Jackson Date and Timestamp Format
Use of '? Extends ' and '? Super ' in Collection Generics
Error:Java: Invalid Source Release: 8 in Intellij. What Does It Mean
How to Set Hard Limit on a Jcomponent When Setmaximumsize() and Setprefferedsize() Don't Work