Home  >  Article  >  Database  >  How to Create a MySQL Database from a Java Application Using Only Login Credentials?

How to Create a MySQL Database from a Java Application Using Only Login Credentials?

Linda Hamilton
Linda HamiltonOriginal
2024-11-25 06:49:26255browse

How to Create a MySQL Database from a Java Application Using Only Login Credentials?

Creating MySQL Databases from Java Applications

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn