数据库访问技术之JDBC
在了解JDBC之前呢,我们可以先对ODBC做一个回顾,以便于更好的理解JDBC。看名字也知道这两个关系不一般,他们实现了同样的功能,为应用程序连接和操作数据库提供支持。所以,我们先从ODBC开始。 ODBC ODBC(Open Database Connectivity)是开放数据库互连的简称
在了解JDBC之前呢,我们可以先对ODBC做一个回顾,以便于更好的理解JDBC。看名字也知道这两个关系不一般,他们实现了同样的功能,为应用程序连接和操作数据库提供支持。所以,我们先从ODBC开始。ODBC
ODBC(Open Database Connectivity)是开放数据库互连的简称,是一种使用SQL的应用程序接口。它是一系列的规范和对数据库访问的API。那么API+SQL就可以执行对数据库的操作。它是不依赖于DBMS的,即通过ODBC可以以相同的方式连接大部分数据库。它包括了应用程序接口、驱动器管理器、数据库驱动器、数据源。下面我们通过一副图来了解ODBC各个部分之间的关系:
JDBC
之前说过了JDBC和ODBC实际上的功能是一致的,只不过实现不太一样。首先ODBC是基于C++语言的,那么与Java的面向对象思想不太相符,通信比较困难。因此,JDBC就出项了,即JDBC是基于JAVA语言的数据库访问API接口。然后其他内容就和ODBC基本一致了。只要你了解了ODBC,那么在概念上基本就理解了JDBC。那么你需要做的就是对新面孔出项的接口再了解一遍就好了,并且这些接口的功能和ODBC是一致的,只是说在实现上有细微的差别。下面也看看JDBC的结构图:
连接方式
由上面的结构图可以看出来,JDBC提供了多种不同的连接方式。这个做个大概的了解吧,因为这主要是数据库厂商关心的事,我们知道就好了,有些也确实不是很明白。1、JDBC-ODBC连接桥:这种方式是一位ODBC为基础的,上面说了java应用程序和ODBC之间的通信是有点麻烦的。但是ODBC作为一种数据库访问的标准应用是很广的。因此JDBC通过映射ODBC的功能调用就保证了原来使用ODBC的数据库也可以很方便的访问的。
2、本地API驱动:即把JDBC调用转换为对数据库接口的客户端二进制代码库的调用。但是这个接口库依赖于产商,因为这里我们调用的不是数据库厂商提供的JDBC的接口实现。
3、纯Java本地协议:即把JDBC调用映射为DBMS的网络监听协议的功能调用,监听程序监听到请求后执行相关的数据库操作。监听程序是由厂商提供的。
常用接口简介
DriverManager
关于驱动程序如何注册的,我们不需要知道。我们需要知道的是,如何调用方法去加载得到数据库驱动程序就好了。即Class.forName()方法,调用这个方法需要传递一个包含该驱动程序类名的String对象作为实参。如下:Class.forName("oracle.jdbc.driver.OracleDriver")
Connection
加载了驱动程序后,与数据库建立连接需要调用DriverManager.getConnection()方法,此方法需要数据库URL作为参数,不同的数据库URL的有些区别,但都符合“协议名 + Ip地址+端口号+数据库名”的格式。数据库用户名和密码如果有,也得加上,如下:String url = "jdbc:oracle:thin:@localhost:1521:pdborcl"; String username = "123"; String password = "123"; Connection conn = DriverManager.getConnection(url, username, password);
StateMent
执行静态的SQL语句,它还可以组合多个SQL语句成为一个批处理,整体提交给数据库。我们通过Connection对象来创建Statement对象,然后用Statement的execute方法来执行SQL。另外PreparedStatement对象是继承自Statement对象的,这里我们用PreparedStatement为例,需要注意的是批处理只能用Statement对象来执行。PreparedStatement pstmt = conn.prepareStatement("select * from t_user where userId=?"); pstmt.setInt(1, id); ResultSet rs = pstmt.executeQuery();
ResultSet
执行一个SQL查询之后的结果集,Result具有指向当前行的指针,可以用来读取结果集中的数据。初始时指针指向第一行前面。该对象的Next()方法可以移动指针。如果Next()之后的行合法返回True,否则False。因此,循环时Next()方法作为判断依据。到这里JDBC的简单介绍就结束了,JDBC在使用中充当了一个沟通者的角色。这让我想起姚明在NBA打球的那个设计模式:适配器模式,这也就成就了Java应用程序跨平台的特性。同时,JDBC、ODBC等也是面向接口编程思想的典型体现。 对了,这里还缺了一个OLE DB,没有提及。下回吧……

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Go language is an efficient, concise and easy-to-learn programming language. It is favored by developers because of its advantages in concurrent programming and network programming. In actual development, database operations are an indispensable part. This article will introduce how to use Go language to implement database addition, deletion, modification and query operations. In Go language, we usually use third-party libraries to operate databases, such as commonly used sql packages, gorm, etc. Here we take the sql package as an example to introduce how to implement the addition, deletion, modification and query operations of the database. Assume we are using a MySQL database.

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

Written above & The author’s personal understanding is that image-based 3D reconstruction is a challenging task that involves inferring the 3D shape of an object or scene from a set of input images. Learning-based methods have attracted attention for their ability to directly estimate 3D shapes. This review paper focuses on state-of-the-art 3D reconstruction techniques, including generating novel, unseen views. An overview of recent developments in Gaussian splash methods is provided, including input types, model structures, output representations, and training strategies. Unresolved challenges and future directions are also discussed. Given the rapid progress in this field and the numerous opportunities to enhance 3D reconstruction methods, a thorough examination of the algorithm seems crucial. Therefore, this study provides a comprehensive overview of recent advances in Gaussian scattering. (Swipe your thumb up

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

The GPT-4o model released by OpenAI is undoubtedly a huge breakthrough, especially in its ability to process multiple input media (text, audio, images) and generate corresponding output. This ability makes human-computer interaction more natural and intuitive, greatly improving the practicality and usability of AI. Several key highlights of GPT-4o include: high scalability, multimedia input and output, further improvements in natural language understanding capabilities, etc. 1. Cross-media input/output: GPT-4o+ can accept any combination of text, audio, and images as input and directly generate output from these media. This breaks the limitation of traditional AI models that only process a single input type, making human-computer interaction more flexible and diverse. This innovation helps power smart assistants

In September 23, the paper "DeepModelFusion:ASurvey" was published by the National University of Defense Technology, JD.com and Beijing Institute of Technology. Deep model fusion/merging is an emerging technology that combines the parameters or predictions of multiple deep learning models into a single model. It combines the capabilities of different models to compensate for the biases and errors of individual models for better performance. Deep model fusion on large-scale deep learning models (such as LLM and basic models) faces some challenges, including high computational cost, high-dimensional parameter space, interference between different heterogeneous models, etc. This article divides existing deep model fusion methods into four categories: (1) "Pattern connection", which connects solutions in the weight space through a loss-reducing path to obtain a better initial model fusion

HTML cannot read the database directly, but it can be achieved through JavaScript and AJAX. The steps include establishing a database connection, sending a query, processing the response, and updating the page. This article provides a practical example of using JavaScript, AJAX and PHP to read data from a MySQL database, showing how to dynamically display query results in an HTML page. This example uses XMLHttpRequest to establish a database connection, send a query and process the response, thereby filling data into page elements and realizing the function of HTML reading the database.
