您的位置:首页 > 其它

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

2006-03-22 00:16 337 查看
  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] )
}

未完待续...
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息