P. 1
Database Fundamentals

Database Fundamentals

|Views: 72|Likes:
Published by mmmaheshwari
DB2 database
DB2 database

More info:

Published by: mmmaheshwari on Mar 02, 2013
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

04/07/2015

pdf

text

original

JDBC stands for Java Database Connectivity. As its name suggests, it is an SQL
application programming interface similar to ODBC and CLI, but for Java applications.

In a CLI application development, the dependent CLI libraries needed to be linked to the
application. Similarly, in JDBC, the relevant Java packages that contain the support for the
JDBC APIs need to be imported.

An example of how a SELECT statement can be issued from a JDBC application is shown
in Listing 7.4.

Database Fundamentals

176

// SQL for SELECT. The name, country, street and province information

// which has been provided by the user are stored in respective variables.

String sqlSel = "select “+name+”, ” +country+”, “+street+”, “+province+”,
“+zip+” from CUSTOMER where Customer = ?";

//prepare the SELECT statement

PreparedStatement pstmt=con.prepareStatement(sqlSel);

pstmt.setString (1, "custCountry");

//set the Input parameter

pstmt.execute();

//execute SELECT statement

ResultSet result = pstmt.getResultSet (); //get the results and set

//values

List custList = new ArrayList();

while (result.next ()) {

Customer cust = new Customer();

cust.name = result.getString (1);

cust.country = result.getString (2);

cust.street = result.getString (3);

cust.province = result.getString (4);

cust.zip = result.getString (5);

custList.add (cust);

}

}catch (SQLException e) {e.pringStackTrace ();}

Listing 7.4 - Code snippet using JDBC

In the above code snippet:

The complete SQL statement, once formed, is first stored in a string variable (sqlSel

above).

The dynamic SQL statement is then prepared using parameter markers in place of
predicate values.

Before execution of the SQL statement, the actual values are bound to the
respective parameter markers. The JDBC statement “pstmt.setString (1,
"custCountry")”" would replace the first parameter marker in the dynamic
SQL statement with the value ‘custCountry’.

Once executed, the programmers would need to map the results returned by JDBC
to respective Java objects.

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->