English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This chapter provides examples of how to insert records into a table using a JDBC application. Before executing the following example, please ensure that you have the following conditions:
To execute the following example, you can useUsernameAndPasswordReplace with the actual username and password.
Your MySQL or any database you are using has been started and is running.
To create a new database using a JDBC application, you need to perform the following steps-
Import package:It requires you to include the software package that contains the JDBC classes required for database programming. Usually, use import java.sql.* That is enough.
Register JDBC driver: It requires you to initialize the driver so that you can open a communication channel with the database.
Establish a connection:It is necessary to use DriverManager.getConnection() Methods to create a Connection object, which represents a physical connection to the database server.
Execute the query: It is necessary to use an object of type Statement to construct and submit SQL statements, so that tables can be created in the selected database.
Clean up environment: Explicitly close all database resources is required, instead of relying on JVM's garbage collection.
Copy and paste the following example into JDBCExample.java, compile and run as follows-
//步骤1.import the required packages import java.sql.*; public class JDBCExample { // JDBC driver name and database URL static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost/STUDENTS"; // 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 a selected database..."); conn = DriverManager.getConnection(DB_URL, USER, PASS); System.out.println("Connected database successfully..."); //步骤4:执行查询 System.out.println("Inserting records into the table..."); stmt = conn.createStatement(); String sql = "INSERT INTO Registration" + "VALUES ("100, 'Zara', 'Ali', 18); stmt.executeUpdate(sql); sql = "INSERT INTO Registration" + "VALUES ("101, 'Mahnaz', 'Fatma', 25); stmt.executeUpdate(sql); sql = "INSERT INTO Registration" + "VALUES ("102, 'Zaid', 'Khan', 30)"; stmt.executeUpdate(sql); sql = "INSERT INTO Registration" + "VALUES("103, 'Sumit', 'Mittal', 28); stmt.executeUpdate(sql); System.out.println("Inserted records into the table..."); catch(SQLException se){ //Handle JDBC error se.printStackTrace(); } //Handle Class.forName error e.printStackTrace(); }finally{ //Used for closing resources try{ if(stmt!=null) conn.close(); catch(SQLException se){ }// Nothing to do try{ if(conn!=null) conn.close(); catch(SQLException se){ se.printStackTrace(); } }//End try System.out.println("Goodbye!"); }//End main }//End JDBCExample
Now, let's compile the above example as follows:
C:\>javac JDBCExample.java C:\>
RuntimeJDBCExampleIt will produce the following result-
C:\>java JDBCExample Connecting to a selected database... Connected database successfully... Inserting records into the table... Inserted records into the table... Goodbye! C:\>