JDBC基础--翻译sun官方教程

本文介绍如何使用JDBC连接数据库,包括加载驱动、建立连接及基本的数据库操作如创建表和插入数据等。
  JDBC基础
Connecting to the Database
和数据库建立连接
There are always two steps to making a database connection using the DriverManager:
通常通过DriverManager连接数据库需要两步.

1.Load the JDBC driver
加载 jdbc驱动

You must load a driver that enables the JDBC classes to communicate with a data source.
你必须加载一个能够通过jdbc的类和数据源进行通信的驱动

Here's the standard method for dynamically loading a driver:
下面是动态加载驱动的标准方法
Class.forName( DriverClassName);

A standard JDBC Compliant driver should also create a new instance of the driver class with this code.
一个标准合适的JDBC驱动在执行Class.forName( DriverClassName)的时候同时也应该创建该driver class的一个实例

Unfortunately, in practice this does not work for all cases. For that reason, the exercises use the following code:
但是不幸的是,在实际应用中并不是所有的案例都能创建该实例.所以实际中我们通常用下面的代码
Class.forName(DriverClassName).newInstance();

While this code will create an additional object in many cases,
the code required to determine whether an instance was created,
and to create a new instance if not, generally outweighs that extra cost.
Fortunately, the garbage collector eventually cleans up the unreferenced object
and the DriverManager does not register the driver twice.

然而Class.forName(DriverClassName).newInstance()大多数情况下会创建一个额外的对象,
那么就需要用代码去判断是否已经创建了一个实例,如果没有那么就创建该实例.
但是通常情况下新增的判断的代码开销比额外创建一个的对象的开销更大.
幸好,垃圾回收器可以最终清理掉没有被引用的对象,所以DriverManager不会对驱动注册两次.

2.Connect to a data source.
连接数据源

The driver supplies methods to make a Connection, but requires a specific type of URL, which uses the jdbc protocol.
通常驱动程序提供建立连接的方法,但是需要使用jdbc协议的url.

The generalized form is jdbc:<subprotocol>:<subname>.
url格式如下 jdbc:<subprotocol>:<subname>

Using the DriverManager class, you request a Connection using the passed URL and the DriverManager selects the appropriate driver;
 Here's the standard form of the Connection request:
Connection con = DriverManager.getConnection(URL,Username,Password );
使用DriverManager,你需要一个通过url建立的Connection,DriverManager会选择适当的驱动.
下面是一个Connection需要的标准格式
Connection con = DriverManager.getConnection(URL,Username,Password );

对数据库进行操作
1.Creating a Table
 创建一个表
While the Connection class has a number of capabilities,
in order to use DDL or Data Manipulation Language ( DML ) SQL statements,
a Statement object is required. So, the next step is to ask the Connection for a Statement object:
Statement stmt = con.createStatement();

为了使用ddl或者dml来操作数据库需要创建一个Statement对象,
所以接下来就是通过Connection来创建一个Statement对象:
Statement stmt = con.createStatement();

CREATE TABLE JJJJData (
   Entry      INTEGER      NOT NULL,
   Customer   VARCHAR (20) NOT NULL,
   DOW        VARCHAR (3)  NOT NULL,
   Cups       INTEGER      NOT NULL,
   Type       VARCHAR (10) NOT NULL,
   PRIMARY KEY( Entry )
                      )
上面创建JJJJData表的DDL,在程序中可以通过

stmt.executeUpdate( "CREATE TABLE JJJJData ("  +
         "Entry      INTEGER      NOT NULL, "    +
         "Customer   VARCHAR (20) NOT NULL, "    +
         "DOW        VARCHAR (3)  NOT NULL, "    +
         "Cups       INTEGER      NOT NULL, "    +
         "Type       VARCHAR (10) NOT NULL,"     +
         "PRIMARY KEY( Entry )"                  +
                                            ")" );

来实现.

Inserting Information into a Database
向数据库中插入信息

INSERT INTO JJJJData VALUES ( 1, 'John', 'Mon', 1, 'JustJoe' )
INSERT INTO JJJJData VALUES ( 2, 'JS',   'Mon', 1, 'Cappuccino' )
INSERT INTO JJJJData VALUES ( 3, 'Marie', 'Mon', 2, 'CaffeMocha' )


on the example program, an array named SQLData contains the actual values, with each element in a form like this:

"(1,  'John',   'Mon', 1, 'JustJoe')"

在程序中可以这样实现
创建一个名为SQLData的数组,该数组中包括实际的值,数组中的元素就象下面这样
"(1,  'John',   'Mon', 1, 'JustJoe')"

The program code corresponding to the INSERT statements above is:

 stmt.executeUpdate(
   "INSERT INTO JJJJData VALUES " + SQLData[i] )


那么程序中插入数据的表达式就可以写成下面这样

for(...)
{
  stmt.executeUpdate(
   "INSERT INTO JJJJData VALUES " + SQLData[i] )
}

未完待续...
内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值