Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Ganesh_JavaSE7_Programming_1z0-804_study_guide.pdf
Скачиваний:
94
Добавлен:
02.02.2015
Размер:
5.88 Mб
Скачать

Chapter 10 Building Database Applications with JDBC

mysql> insert into contact values (default, 'Michael', 'Taylor', 'michael@abc.com', '+919876543210');

Query OK, 1 row affected (0.10 sec)

mysql> insert into contact values (default, 'William', 'Becker', 'william@abc.com', '+449876543210');

Query OK, 1 row affected (0.03 sec)

mysql> /* That's it. Our database is ready to use now.*/

Connecting to a Database Using a JDBC Driver

In this section, we will discuss how to programmatically connect to a database using a JDBC driver. Before that we’ll briefly cover the Connection interface.

The Connection Interface

The Connection interface of the java.sql package represents a connection from application to the database. It is a channel through which your application and the database communicate. Table 10-1 lists important methods in the Connection interface. All these methods will throw SQLExceptions so we won’t mention it again in the table. We list transaction-related methods of the Connection interface in Table 10-3.

Table 10-1.  Important Methods in the Connection Interface

Method

Description

Statement createStatement()

Creates a Statement object that can be used to send SQL statements to the

 

database.

PreparedStatement

Creates a PreparedStatement object that can contain SQL statements. The

prepareStatement(String sql)

SQL statement can have IN parameters; they may contain ‘?’ symbol(s),

 

which are used as placeholders for passing actual values later.

CallableStatement

Creates a CallableStatement object for calling stored procedures in the

prepareCall(String sql)

database. The SQL statement can have IN or OUT parameters; they may

 

contain ‘?’ symbol(s), which are used as placeholders for passing actual

 

values later.

DatabaseMetaData

Gets the DataBaseMetaData object. This metadata contains useful

getMetaData()

information, such as database schema information, table information, etc.,

 

which is especially useful when you don’t know the underlying database.

Clob createClob()

Returns a Clob object (Clob is the name of the interface). CLOB (Character

 

Large Object) is a built-in type in SQL; it can be used to store a column

 

value in a row of a database table.

Blob createBlob()

Returns a Blob object (Blob is the name of the interface). BLOB (Binary

 

Large Object) is a built-in type in SQL; it can be used to store a column

 

value in a row of a database table.

void setSchema(String schema)

When passed the schema name, it sets this Connection object to the

 

database schema to access.

String getSchema()

Returns the schema name of the database associated with this Connection

 

object; returns null if no schema is associated with it.

 

 

285

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]