没有合适的资源?快使用搜索试试~ 我知道了~
首页深入理解Oracle JDBC:Java API与数据库连接详解
Oracle JDBC指南是一份详尽的教材,专为希望深入了解Java Database Connectivity (JDBC)框架及其架构和实际应用的Java程序员设计。JDBC是Java API,它允许开发者在各种平台上(如Windows、MacOS和不同版本的UNIX)访问各类表格数据,特别是关系型数据库中的数据。 该教程的受众是那些已经掌握了Java编程语言,并且对SQL和数据库概念有所了解的开发人员。通过学习,读者将能够深入理解JDBC的工作原理,包括其组件、连接数据库的过程、执行SQL语句、处理结果集以及错误处理等方面。 在开始之前,有一些先决条件需要满足:首先,对Java编程有扎实的基础,因为JDBC是在Java环境中操作数据库的关键。其次,熟悉SQL语言和数据库的基本概念,这有助于更好地与JDBC进行交互。 版权和免责声明方面,所有内容和图形均属于TutorialsPoint (I) Pvt. Ltd.,未经书面许可,用户不得擅自复制、保留、分发或再发布本教程的任何部分。尽管作者团队努力保持网站和教程内容的最新性和准确性,但可能存在信息不准确或错误的情况。因此,在使用这些内容时,请确保查阅最新的官方文档或官方教程以获取最精确的信息。 Oracle JDBC指南提供了实践导向的学习材料,不仅帮助读者掌握基础,还能引导他们提升在Java环境中与Oracle数据库高效交互的能力,从而在实际项目中发挥重要作用。
资源详情
资源推荐
JDBC
9
In addition, download and install MySQL Administrator as well
as MySQL Query Browser. These are GUI based tools that will make
your development much easier.
Finally, download and unzip MySQL Connector/J (the MySQL JDBC
driver) in a convenient directory. For the purpose of this tutorial, we will
assume that you have installed the driver at C:\Program
Files\MySQL\mysql-connector-java-5.1.8.
Accordingly, set CLASSPATH variable to C:\Program Files\MySQL\mysql-
connector-java-5.1.8\mysql-connector-java-5.1.8-bin.jar. Your driver
version may vary based on your installation.
PostgreSQL DB: PostgreSQL is an open source database. You can
download it from PostgreSQL Official Site.
The Postgres installation contains a GUI based administrative tool called
pgAdmin III. JDBC drivers are also included as part of the installation.
Oracle DB: Oracle DB is a commercial database sold by Oracle. We
assume that you have the necessary distribution media to install it.
Oracle installation includes a GUI based administrative tool called
Enterprise Manager. JDBC drivers are also included as a part of the
installation.
Install Database Drivers
The latest JDK includes a JDBC-ODBC Bridge driver that makes most Open
Database Connectivity (ODBC) drivers available to programmers using the JDBC
API.
Now-a-days, most of the Database vendors are supplying appropriate JDBC
drivers along with Database installation. So, you should not worry about this
part.
Set Database Credential
For this tutorial we are going to use MySQL database. When you install any of
the above database, its administrator ID is set to root and gives provision to set
a password of your choice.
Using root ID and password you can either create another user ID and
password, or you can use root ID and password for your JDBC application.
There are various database operations like database creation and deletion, which
would need administrator ID and password.
For rest of the JDBC tutorial, we would use MySQL Database with username as
ID and password as password.
JDBC
10
If you do not have sufficient privilege to create new users, then you can ask
your Database Administrator (DBA) to create a user ID and password for you.
Create Database
To create the EMP database, use the following steps:
Step 1
Open a Command Prompt and change to the installation directory as follows:
C:\>
C:\>cd Program Files\MySQL\bin
C:\Program Files\MySQL\bin>
Note: The path to mysqld.exe may vary depending on the install location of
MySQL on your system. You can also check documentation on how to start and
stop your database server.
Step 2
Start the database server by executing the following command, if it is already
not running.
C:\Program Files\MySQL\bin>mysqld
C:\Program Files\MySQL\bin>
Step 3
Create the EMP database by executing the following command:
C:\Program Files\MySQL\bin> mysqladmin create EMP -u root -p
Enter password: ********
C:\Program Files\MySQL\bin>
Create Table
To create the Employees table in EMP database, use the following steps:
Step 1
Open a Command Prompt and change to the installation directory as follows:
JDBC
11
C:\>
C:\>cd Program Files\MySQL\bin
C:\Program Files\MySQL\bin>
Step 2
Login to the database as follows:
C:\Program Files\MySQL\bin>mysql -u root -p
Enter password: ********
mysql>
Step 3
Create the table Employee as follows:
mysql> use EMP;
mysql> create table Employees
-> (
-> id int not null,
-> age int not null,
-> first varchar (255),
-> last varchar (255)
-> );
Query OK, 0 rows affected (0.08 sec)
mysql>
Create Data Records
Finally you create few records in Employee table as follows:
mysql> INSERT INTO Employees VALUES (100, 18, 'Zara', 'Ali');
Query OK, 1 row affected (0.05 sec)
mysql> INSERT INTO Employees VALUES (101, 25, 'Mahnaz', 'Fatma');
Query OK, 1 row affected (0.00 sec)
JDBC
12
mysql> INSERT INTO Employees VALUES (102, 30, 'Zaid', 'Khan');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO Employees VALUES (103, 28, 'Sumit', 'Mittal');
Query OK, 1 row affected (0.00 sec)
mysql>
For a complete understanding on MySQL database, study the MySQL Tutorial.
Now you are ready to start experimenting with JDBC. Next chapter gives you a
sample example on JDBC Programming.
JDBC
13
This chapter provides an example of how to create a simple JDBC application.
This will show you how to open a database connection, execute a SQL query,
and display the results.
All the steps mentioned in this template example, would be explained in
subsequent chapters of this tutorial.
Creating JDBC Application
There are following six steps involved in building a JDBC application:
Import the packages: Requires that you include the packages
containing the JDBC classes needed for database programming. Most
often, using import java.sql.* will suffice.
Register the JDBC driver: Requires that you initialize a driver so you
can open a communication channel with the database.
Open a connection: Requires using
the DriverManager.getConnection() method to create a Connection object,
which represents a physical connection with the database.
Execute a query: Requires using an object of type Statement for building
and submitting an SQL statement to the database.
Extract data from result set: Requires that you use the
appropriateResultSet.getXXX() method to retrieve the data from the result
set.
Clean up the environment: Requires explicitly closing all database
resources versus relying on the JVM's garbage collection.
Sample Code
This sample example can serve as a template when you need to create your
own JDBC application in the future.
This sample code has been written based on the environment and database
setup done in the previous chapter.
Copy and past the following example in FirstExample.java, compile and run as
follows:
//STEP 1. Import required packages
4. SAMPLE CODE
剩余161页未读,继续阅读
LimitedEditio
- 粉丝: 0
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 构建Cadence PSpice仿真模型库教程
- VMware 10.0安装指南:步骤详解与网络、文件共享解决方案
- 中国互联网20周年必读:影响行业的100本经典书籍
- SQL Server 2000 Analysis Services的经典MDX查询示例
- VC6.0 MFC操作Excel教程:亲测Win7下的应用与保存技巧
- 使用Python NetworkX处理网络图
- 科技驱动:计算机控制技术的革新与应用
- MF-1型机器人硬件与robobasic编程详解
- ADC性能指标解析:超越位数、SNR和谐波
- 通用示波器改造为逻辑分析仪:0-1字符显示与电路设计
- C++实现TCP控制台客户端
- SOA架构下ESB在卷烟厂的信息整合与决策支持
- 三维人脸识别:技术进展与应用解析
- 单张人脸图像的眼镜边框自动去除方法
- C语言绘制图形:余弦曲线与正弦函数示例
- Matlab 文件操作入门:fopen、fclose、fprintf、fscanf 等函数使用详解
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功