Question: How can a MySQL database be created from a Java application, given only login credentials?
In contrast to connection URLs that specify the database name, such as:
String url = "jdbc:mysql://localhost:3306/test";
It can be challenging to create a database without specifying its name in the URL.
Answer:
Despite the lack of database specification in the connection URL, it is possible to create a database using the following method:
Conn = DriverManager.getConnection("jdbc:mysql://localhost/?user=root&password=rootpassword"); s = Conn.createStatement(); int Result = s.executeUpdate("CREATE DATABASE databasename");
This method allows you to establish a connection without specifying the database and then executes a SQL statement to create the desired database.
The above is the detailed content of How to Create a MySQL Database from a Java Application Using Only Login Credentials?. For more information, please follow other related articles on the PHP Chinese website!