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

Example Analysis of JDBC Registration driver and obtaining connection

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

Share

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

Editor to share with you the JDBC registration driver and access to the example analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

JDBC registers the driver to get the connection

Jdbc Power Node Video tutorial

JDBC programming six steps 1. Register driver (function: tell Java program, which brand of database to connect to) 2. Get the connection (indicates that the channel between the JVM process and the database process is open, belongs to inter-process communication, heavyweight, must be closed after use) 3. Get database operation objects (objects that specifically execute SQL statements) 4. Execute sql (main DQL DML) 5. Process the query result set (step 5 only occurs when the select statement is executed in step 4). Release resource registration driver mode 1

Java.sql.Driver driver = new com.mysql.jdbc.Driver (); (the parent type reference points to the subtype object) DriverManager.registerDriver (dirver); / / the package name of the Driver class is com.mysql.jdbc//Driver interface and the package name is java.sql

Mode 2 is more commonly used

/ / reflection mechanism Class.forName ("com.mysql.cj.jdbc.Driver") / / Why mode 2 is more commonly used, because the parameter is a string, the string can be written to the configuration file (xxx.properties) / / the execution of the Class.forName method will cause the later class to load / / the method does not need to receive the return value, only its class loading action / / when the class loads, the static code block will be executed (there is driver registration in the code block)

Establish a connection

Need to get the ip port database name, username and password

String url = "jdbc:mysql://localhost:3306/powernode?serverTimezone=UTC"; String user = "root"; String password = "*"; Connection conn = DriverManager.getConnection (url, user, password); System.out.println ("Database connection object =" + conn)

Url: uniform resource locator (absolute path to a resource in the network) https://www.baidu.com/ what are the parts of url url? Take Baidu as an example, http://183.232.231.174 80max index.html

Communication protocol http://

IP 183.232.231.174 server IP address (IP is the code name of the computer)

Port 80 of the software on the PORT server (the port number is the code name of a software on the computer)

Resource name index.html is a resource name on the server

MySQL:String url = "jdbc:mysql://localhost:3306/powernode?serverTimezone=UTC"

Communication protocol jdbc:mysql://

IP 127.0.0.1 Server IP address localhost Native IP (IP is the code name of the computer)

The port number of mysql on the PORT server is 3306 (the port number is the code of some software on the computer)

Resource name powernode is the name of the existing database instance

Version problem database connection of MySQL version 8.0 or later

1. Com.mysql.jdbc.Driver needs to be changed to com.mysql.cj.jdbc.Driver.

two。 The serverTimezone=UTC parameter needs to be added after the database-driven url.

String url = "jdbc:mysql://localhost:3306/powernode?serverTimezone=UTC"; supplementary url of oracle: driver of jdbc:oracle:thin:@localhost:1521:orcl oracle: Driver driver = new orcale.jdbc.driver.OrcaleDriver ()

The above is all the contents of the article "sample Analysis of JDBC Registration driver and getting connections". 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