Adel Bachene
Adel Bachene

Reputation: 994

create mysql database with username and password from java code

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

make sure that:

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

SET CHARACTER SET utf8

can i do that on java code ?

Upvotes: 2

Views: 5088

Answers (2)

user890904
user890904

Reputation:

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.

Upvotes: 0

Satish Pandey
Satish Pandey

Reputation: 1184

There is a example Create database using java

Hope this helps

Upvotes: 0

Related Questions