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

How to implement JDBC with MySQL

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly shows you "MySQL how to achieve JDBC", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "MySQL how to achieve JDBC" this article.

Getting started with JDBC basic concept

JDBC (Java DataBase Connectivity,java Database connection) is a Java API used to execute SQL statements, which can provide unified access to a variety of relational databases. It is composed of a set of classes and interfaces written in Java language.

The    JDBC specification defines the interface, and the specific implementation is implemented by the major database vendors.

JDBC is the standard specification for Java to access the database. How to operate the database really needs a specific implementation class, that is, database driver. Each database manufacturer writes its own database driver according to the communication format of its own database. So we only need to call the method in the JDBC interface, and the database driver is provided by the database vendor.

Essence

In fact, it is a set of specifications (interfaces) provided by java officially. Used to help developers quickly connect different relational databases!

7 steps of JDBC

(1) Import jar package

(2) Registration driver

Class.forName ("com.mysql.cj.jdbc.Driver")

Note: if you use a higher version of MySQL, you must use com.mysql.cj.jdbc.Driver, otherwise an error will be reported!

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

If it is a lower version, you can use com.mysql.jdbc.Driver.

If you still report an error, you can add a time zone on url!

Url=jdbc:mysql://localhost:3306/ database name? serverTimezone=UTC

(3) obtain the connection

String url = "jdbc:mysql://localhost:3306/db1"; Connection con = DriverManager.getConnection (url, "root", "888888")

(4) get the executor object

Statement stat = con.createStatement ()

(5) execute the sql statement and receive the returned result

String sql = "SELECT * FROM user"; ResultSet rs = stat.executeQuery (sql)

(6) processing result

While (rs.next ()) {System.out.println (rs.getInt ("id") + "\ t" + rs.getString ("name");}

(7) release resources

Con.close (); stat.close (); rs.close (); JDBC getting started sample code

Change the user name, database name and password in the Connection object to your own!

Public class jdbc_demo01 {public static void main (String [] args) throws ClassNotFoundException, SQLException {/ / 1. Import jar package / / 2. Register driver Class.forName ("com.mysql.cj.jdbc.Driver"); / / 3. Get connection String url = "jdbc:mysql://localhost:3306/db1"; Connection con = DriverManager.getConnection (url, "root", "888888"); / / 4. Get the executor object Statement stat = con.createStatement (); / / 5. Execute the sql statement and receive the result String sql = "SELECT * FROM product"; ResultSet rs = stat.executeQuery (sql); / / 6. Processing result while (rs.next ()) {System.out.println (rs.getInt ("id") + "\ t" + rs.getString ("name") + "\ t" + rs.getInt ("price") + "\ t" + rs.getString ("brand") + "\ t" + rs.getInt ("stock")) } / / 7 . Release resources con.close (); stat.close (); rs.close ();}}

The results are as follows:

The above is all the content of the article "how to achieve JDBC in MySQL". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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

Development

Wechat

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

12
Report