2

How to create mysql database with username and password from java code ? and create all the tables ?

i was googling and i found this code

Conn = DriverManager.getConnection
("jdbc:mysql://localhost/?user=root&password=rootpassword"); 
s=Conn.createStatement();
int Result=s.executeUpdate("CREATE DATABASE databasename");

but in my case i need to :

-set a username and password from java

  • create all the tables

make sure that:

  • MySQL charset: UTF-8 Unicode (utf8)

  • MySQL connection collation: utf8_general_ci

  • my database and table collations are set to: utf8_general_ci or utf8_unicode_ci

and after that i must execute this in mysql : SET NAMES 'utf8'

SET CHARACTER SET utf8

can i do that on java code ?

Adel Bachene
  • 974
  • 1
  • 14
  • 34
  • Why don't you create a database stored procedure which does all your database creation, tables etc and call this procedure from Java by passing necessary parameters. – Jacob Aug 28 '12 at 14:11
  • @Polppan a procedure ?? if i export my database do i have this procedure ?? – Adel Bachene Aug 28 '12 at 14:19
  • It is certainly possible from Java.. You may need to do with seperate statements if you are not using database stored procedures. – Jacob Aug 28 '12 at 14:20
  • @Polppan how i create procedure on a file and call it on java ? – Adel Bachene Aug 28 '12 at 14:27
  • Do you want to create procedure on database or file? Not sure about creating procedure on a file. – Jacob Aug 28 '12 at 14:39
  • 1
    Some examples on creating database from Java [One](http://stackoverflow.com/questions/717436/create-mysql-database-from-java) [Two](http://www.java2s.com/Code/Java/Database-SQL-JDBC/CreateDatabaseforMySQL.htm) [Three](http://www.tutorialspoint.com/jdbc/jdbc-create-database.htm) – Jacob Aug 28 '12 at 14:57

2 Answers2

0

There is a example Create database using java

Hope this helps

Satish Pandey
  • 1,184
  • 4
  • 12
  • 32
0

seeing your comment about running script from a file, you can check my answer in the following post:

Java/Mysql How would you inject an entire SQL file to a mysql server?

it will allow you to run a file through shell. It was made for linux but you can easily rename the function and the command to run under windows. this will allow you to run all the commands from a file. so actually you can have an export of some database and run it every time you need to create a similar database, with the support of username and password too.

Community
  • 1
  • 1