简体   繁体   中英

Create a database with username and password in Java

How to create a database with username and password in Java?

I created a DB using

Connection conn = null;
Statement stmt = null;
try {
    Class.forName("com.mysql.jdbc.Driver");
    conn = DriverManager.getConnection("jdbc:mysql://localhost/", USER, PASS);
    stmt = conn.createStatement();
    stmt.executeUpdate("CREATE DATABASE "+DB_NAME;);
    // DB creatd
}
catch(Exception e) {
    e.printStackTrace();
}

but now if I connect to this database using:

Connection conn1 = DriverManager.getConnection("jdbc:mysql://localhost/"+DBNAME,user,pass);

what should I enter in the use and pass?
And more importantly how do I create a DB with properties?

One has to have very good reason to create a database via code but You would enter the same login credential as the one that created the database:

DriverManager.getConnection("jdbc:mysql://localhost/"+DBNAME, USER, PASS);

Alternatively once connected to the server you can connect to the database by running following query:

USE DB_NAME
  Connection conn1=DriverManager.getConnection("jdbc:mysql://localhost/",user,pass);
  stmt = conn1.createStatement();
  String sql = "CREATE DATABASE UTSAV";
  stmt.executeUpdate(sql);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM