Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

JDBC operation

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

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 ();}}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report