一、 

Connection interface

当驱动和连接建立后,我们就该考虑通过连接,我们能做哪些事,而接口相当于一个目录,就是告诉我们连接建立后能做什么事,做哪些事情。

 

ØDefines methods for interacting with the database via the established connection.
这个地方告诉我们这个连接的接口讲了什么,当然在概念上要首先认识到interface是一个接口,这里要注意到这个interface是一段代码。Connection interface通过已经建立的连接定义了和数据库交互的方法,Connection interface简单的说就是告诉我们通过已经建立的连接我们程序员可以和数据库做哪些事情。
§A connection object represents a connection with a database.
已经知道Connection interface是干什么的了,接下来就要具体说一些概念,首先要说的就是连接的对象。连接的对象代表了和数据库的连接。
§ A connection session includes the SQL statements that are executed and the results that are returned over that connection.
这里提到一个新的connection session,猜一下,肯定是通过和数据库一次交互的谈话过程。专业一点就是:通过连接将SQL语句发送到数据库去执行,然后数据库通过连接返回出结果。
§A single application can have one or more connections with a single database, or it can have many connections with many different databases.
 这里解决了上面自己闪现过的疑问,既然一个连接对象代表一个连接,那么是否可以通过多个对象来表示多个连接。这里说明了,的确是可以的。一种情况:一个应用程序可以和一个单独的有一个或者多个的连接;另一种情况:一个应用程序可以和很多不同的数据库有很多的连接。总结的话就是,一个程序可以有多个连接的对象。
Example :
Connection connection = DriverManager.getConnection(
  "jdbc:oracle:thin: @10.122.130.31:1521:training",“scott", “tiger");
这里和前面的例子的区别,是什么呢?
  Class.forName(“oracle.jdbc.driver.OracleDriver”)
这两个语句的联系是什么呢,下面的对上面的有什么用呢?
下面的语句是指明驱动的,指明了就行,不用返回或者输出什么的。
上面的语句就是建立连接的,通过DriverManager.getConnection()的方法。
在方法中jdbc:oracle:thin: @10.122.130.31:1521:training为JDBC的url。到这里肯定会要想,这个url是怎么来的,有什么要求,我们该怎么写。  数据库连接串URL,指定数据源以及使用的数据库访问 协议。语法格式:jdbc:<subprotocol>:<subname> 按照这种语法,我们就能看出数据库访问协议是Oracle,而数据源是thin: @10.122.130.31:1521:training。
 
To execute a SQL statement you need to create an object of the Statement(any of the three types) by invoking the appropriate method of the Connection interface. You should then use the method of the Statement interface to execute the SQL query. The method may return a ‘ ResultSet’ object, integer value telling number of lines modified or updated by the statement, etc.
在来讲讲,连接的接口到底能给SQL语句做什么事情呢?上面还是讲了连接对象,却木有将连接对象如何和SQL语句联系起来。连接好数据库后,如果我们要执行一个SQL语句,要怎么做呢。首先我们需要创建一个语句的对象,这里语句的对象有三种,是哪三种呢。又出现一个问题就是怎么创建一个语句的对象,答案就是通过调用连接接口里合适的方法。然后要执行sql语句查询的时候,我们应该使用语句接口中的方法。这种方法可能会返回另一个对象,这个对象是“ResultSet”。有人又要问,为什么要ResultSet对象呢?这个等下面慢慢介绍。当执行语句后可能返回一个ResultSet的整型值,这个整型值告诉了我们刚刚执行的sql语句更新或者修改的行数。
二、
  Creating a Statement object
上面讲到了要想通过连接执行SQL语句, 就必须要创建语句的对象。那么我们现在就该讨论如何创建语句的对象。
ØOnce a connection is obtained we can interact with the database.
interact:vi. 互相影响;互相作用 vt. 互相影响;互相作用 n. 幕间剧;幕间休息
我们要和数据库进行交互,首先就是要先和数据库建立连接。当连接获得后,我们就可以和数据库进行相互作用了。可以将sql语句发送给数据库,数据库也可以将结果返回给我们。
ØConnection interface defines methods for interacting with the database via the established connection.
ØTo execute SQL statements, we need to instantiate a Statement object from the connection object by using the createStatement() method.
Statement statement = connection.createStatement();
一直说创建语句对象才能发送sql语句到数据库,然后执行。可是怎么创建语句对象却木有说。到底怎么通过建立的连接来创建语句对象呢?这里需要使用crateStatement()方法。
具体的是
Statement statement = connection.createStatement();
这里的connection就是前面建立的连接
前面的建立过程是:
Connection connection = DriverManager.getConnection(
  "jdbc:oracle:thin: @10.122.130.31:1521:training",“scott", “tiger");
Ø A statement object is used to send and execute SQL statements to a database. 
Statement statement = connection.createStatement(); 我们通过这个代码建立的语句对象有什么用呢?为什么要建立语句对象?
肯定有人会说,不就是为了发送sql语句到数据库中并且执行sql语句嘛,是的,说的已经够多了。