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 IDEA

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

Share

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

This article will explain in detail how to achieve JDBC in IDEA. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

What is JDBC?

JDBC (Java Data Base 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 consists of a set of classes and interfaces written in Java language. JDBC provides a benchmark from which to build more advanced tools and interfaces that enable database developers to write database applications.

JDBC essence

JDBC interface: a set of jdbc interface provided by sun company, which is implemented by major database manufacturers and finally provides programmers and users with the interaction with the database.

Driver: driver is each implementation class implemented by each database manufacturer following the interface.

IDEA configuration

1. IDEA new project

2. Create a new modle

3. Add mysql database driver

3.1 New modle- > Open Module Setting in the right mouse button point set

3.2 libraries- > ±- > Java

3.3.Add mysql database driver

3.3 added successfully

3.4 check whether the driver is configured successfully-> ExternalLibraries

JDBC simply implements a sql statement

Code

Package com.test.jdbc;/** * @ author pan * @ date 18:52 * / import com.sun.java.util.jar.pack.DriverResource;import java.sql.*;import java.util.ResourceBundle; * @ ClassName: com.test.jdbc.jdbcTest04 * @ Description: class description public class JdbcTest04 {public static void main (String [] args) {Connection connection = null; Statement statement = null; ResultSet resultSet = null Try {ResourceBundle resourceBundle = ResourceBundle.getBundle ("com\\ test\\ jdbc\\ jdbc"); / / 1, registered driver / * Driver driver = new com.mysql.cj.jdbc.Driver (); DriverManager.registerDriver (driver); * / String aClass = resourceBundle.getString ("class") / / the registration driver can be completed by using the class loading mechanism, because the Driver class has a static code block that accomplishes the above tasks, and the reflection mechanism can be used to execute the static code block Class.forName (aClass) when the class is loaded; / / 2. Establish a connection String url = resourceBundle.getString ("url") String user = resourceBundle.getString ("user"); String password = resourceBundle.getString ("password"); connection = DriverManager.getConnection (url, user, password); / / 3. Get database operation object statement = connection.createStatement (); / / 4, execute sql statement String sql = "select * from emp" ResultSet = statement.executeQuery (sql); / / 5. Operation result set while (resultSet.next ()) {String ename = resultSet.getString ("ename"); String deptno = resultSet.getString ("deptno"); String sal = resultSet.getString ("sal"); System.out.println (ename+ "" + deptno+ "" + sal) }} catch (SQLException | ClassNotFoundException e) {e.printStackTrace ();} finally {/ / 6, release resource if (resultSet! = null) {try {resultSet.close ();} catch (SQLException e) {e.printStackTrace () } if (statement! = null) {statement.close (); if (connection! = null) {connection.close ();}}

Jdbc.properties

Url = jdbc:mysql://localhost:3306/yinpan?useSSl=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=trueuser = rootpassword = class = com.mysql.cj.jdbc.Driver

Execution result

This is the end of the article on "how to achieve JDBC in IDEA". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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