Home  >  Article  >  Database  >  Can Java Create MySQL Databases Using Only Login Credentials?

Can Java Create MySQL Databases Using Only Login Credentials?

Susan Sarandon
Susan SarandonOriginal
2024-11-20 00:43:03139browse

Can Java Create MySQL Databases Using Only Login Credentials?

Can MySQL Databases Be Created from Java?

While typical examples of connecting to MySQL databases through Java include a database name in the connection URL, it is also possible to create a database without one. This can be useful when only a login name and password are available.

Creating a Database with Limited Credentials

To create a MySQL database in Java with only a login name and password, follow these steps:

  1. Establish a connection without specifying a database in the URL:
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/?user=root&password=rootpassword");
  1. Create a statement object:
Statement stmt = conn.createStatement();
  1. Execute the SQL statement to create the database:
int result = stmt.executeUpdate("CREATE DATABASE databasename");

By following these steps, you can successfully create a MySQL database from Java using only login credentials.

The above is the detailed content of Can Java Create MySQL Databases 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