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

The study of JDBC

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

Share

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

A short day ago spent a long time learning MySQL, Oracle database, now it is time to learn JDBC, the following do some of their own learning notes.

JDBC is called Java Data Base Connectivity (java database connection)

It can provide unified access to a variety of databases, reflecting the high-end spirit of Java "write once, run everywhere".

In popular terms, JDBC is a "bridge", which is the hub of communication between JAVA applications and databases.

Here is a simple example:

First create a database with your own name

Then connect to the database through code on the project

Public class DBUtil {private static final String url = "jdbc:mysql://127.0.0.1:3306/imooc"; private static final String user = "root"; private static final String password = "root"; public static void main (String [] args) throws ClassNotFoundException, SQLException {/ / connect to the database / / 1. Load the driver Class.forName ("com.mysql.jdbc.Driver"); / / 2. Get the database connection Connection conn = DriverManager.getConnection (url, user, password); / / 3. Through the connection of the database to operate the database, add, delete, query and modify Statement stmt = conn.createStatement (); / / get the data and store it in ResultSet ResultSet rs = stmt.executeQuery ("select user_name,age from imooc_goddess"); while (rs.next ()) {/ / continue to get System.out.println (rs.getString ("user_name") + "," + rs.getInt ("age")) when the next entry is still data;}

/ / the above code tests that the database connection is normal

To use JDBC, you also need to use the corresponding driver. The corresponding jar package is given in the attachment.

The following example uses the three-tier MVC architecture that everyone should be familiar with:

View (view layer), Control (control layer), Model (model layer), DB (database)

Comparison of various connection modes of JDBC

1. The way of JDBC+ODBC bridge. Features: need database ODBC driver, only suitable for Microsoft system. (it is not commonly used and cannot be used for Linux)

2. The form of JDBC+ manufacturer API. The characteristic manufacturer API is generally suitable for C programming. (troublesome steps, not commonly used)

3. The form of JDBC+ manufacturer Database Connection Server+DataBase. Features: a server dedicated to connecting to the database has been set up between Java and DATABASE (usually provided by database vendors)

4. The connection mode of JDBC+DATABASE. This separates Application from database. Developers only need to pay attention to the implementation of internal logic rather than the specific implementation of database connection (efficient and simple, more commonly used).

In addition, it also wrote a more complex example of adding, deleting, checking and modifying the database. More source code is stored in the form of attachments.

Attachment: http://down.51cto.com/data/2368558

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