English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Diese Anleitung bietet Beispiele dazu, wie man eine Datenbank mit einer JDBC-Anwendung erstellt. Stellen Sie sicher, dass Sie die folgenden Bedingungen erfüllen, bevor Sie die folgenden Beispiele ausführen:
Sie sollten Administratorrechte haben, um in der angegebenen Struktur eine Datenbank zu erstellen. Um den folgenden Beispiel zu durchführen, müssen SieBenutzernameundPasswortersetzten Sie sie durch die tatsächlichen Benutzername und Passwort
Ihre MySQL oder jede andere von Ihnen verwendete Datenbank ist gestartet und läuft
Um eine neue Datenbank mit einer JDBC-Anwendung zu erstellen, müssen Sie die folgenden Schritte ausführen-
Import package:It requires you to include the software package that contains the JDBC classes required for database programming. Typically, use import java.sql.* is enough.
Register JDBC driver: It requires you to initialize the driver so that you can open a communication channel with the database.
Establish connection:is requiredDriverManager.getConnection()A method to create a Connection object representing a physical connection to the database server. To create a new database, no database name needs to be provided when preparing the database URL, as shown in the following example.
Execute query: A Statement object type is required to construct SQL statements and submit them to the database.
Cleanup:All database resources must be explicitly closed, rather than relying on JVM garbage collection.
Copy and paste the following example into JDBCExample.java, compile and run as follows:
//步骤1.Import the required software packages import java.sql.*; public class JDBCExample { // Name of the JDBC driver and database URL static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost/"; // Database credentials static final String USER = "username"; static final String PASS = "password"; public static void main(String[] args) { Connection conn = null; Statement stmt = null; try{ //步骤2:注册JDBC驱动程序 Class.forName("com.mysql.jdbc.Driver"); //步骤3:建立连接 System.out.println("Connecting to database..."); conn = DriverManager.getConnection(DB_URL, USER, PASS); //步骤4:执行查询 System.out.println("Creating database..."); stmt = conn.createStatement(); String sql = "CREATE DATABASE STUDENTS"; stmt.executeUpdate(sql); System.out.println("Datenbank erfolgreich erstellt..."); catch(SQLException se){ //Fehlerbehandlung für JDBC-Fehler se.printStackTrace(); Fehlerbehandlung für Class.forName //e.printStackTrace(); finally{ } //Verwendung zum Schließen von Ressourcen try{ stmt.close(); catch(SQLException se }2{ } try{ if(conn!=null) conn.close(); catch(SQLException se){ se.printStackTrace(); } }//try beenden System.out.println("Auf Wiedersehen!"); }//main beenden }//JDBCExample beenden
Lassen Sie uns den obigen Beispielcode kompilieren, wie folgt:
C:\>javac JDBCExample.java C:\>
LaufzeitJDBCExampleEs wird folgende Ausgabe erzeugt:-
C:\>java JDBCExample Verbindung zur Datenbank herstellen... Datenbank erstellen... Datenbank erfolgreich erstellt... Auf Wiedersehen! C:\>