Get the App
SLTechnology News&Howtos  ›  Database  › 

JDBC operation

Shulou Source: shulou.com Published: 2022-06-01 06:03:10 09月19日 Update

I. JDBC:

Java Database Connection stands for database connection and is a set of standards specifically provided in Java for operating databases. If all database manufacturers want to provide support for Java

This standard must be supported. JDBC is actually an interface to a set of class libraries.

Note: currently, JDBC can connect to any database.

two。 The main operation classes and interfaces:

Connection interface, Statement interface, PreparedStatement interface, ResultSet interface, CallableStatement interface, DriverManager class.

III. Preparation before JDBC operation

1. Create a database table

two。 Import database driver package

Mysql-connector-java-5.1.27-bin.jar

four。 Implement the insert operation

1. Load the database driver

Class.forName (driver class)

two。 Obtain the database connection object through the user name, password and connection address

DriverManager.getConnection (connection address, user name, password)

3. Construct an inserted SQL statement

4.Statement instance

Statement stmt=conn.createStatement ()

5. Execute insert SQL statement

Stml.executeQuery (sql)

6. Close the connection

Stml.close ()

Conn.close ()

five。 Implementing the update operation is the same as step 4 except that the SQL statement is not the same.

six。 Implementing the delete operation is the same as step 4 except that the SQL statement is not the same.

/ / implement database connection and insert operation public void insert () {/ / load driver try {Class.forName ("com.mysql.jdbc.Driver"); String url= "jdbc:mysql://localhost:3306/employee"; String username= "root" String password= "asd"; / / obtain the database connection Connection conn=DriverManager.getConnection (url, username, password); / / construct the SQL statement String sql= "insert into employee (id,name,salary) values (1Jing 'scholar-officials', 10000)" / / construct a Statement instance to send the carrier Statement stmt=conn.createStatement () of the SQL statement; / / execute the SQL statement stmt.executeUpdate (sql); / / close the connection (release resources) stmt.close () Conn.close (); System.out.println ("execution succeeded!") Catch (ClassNotFoundException e) {e.printStackTrace ();} catch (SQLException e) {e.printStackTrace ();}}

Tags: Data database interface statement driver just address instance password standard user user name program driver support success scholar-bureaucrat actually actually object Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Linux Xiaomi MariaDB OPPO Reno