] > Embedded SQL

Embedded SQL

14.1 Background

----------|   --------|   ------|   -----------| |  java    -----JDBC   -----     ---|| database | application ||--manager -----driver----management | -program--|   --------|   ------|   ---system----

14.2 Loading Drivers

<..DBdriver.java..>
 import java.sql.*;
 import com.sybase.jdbcx.SybDriver;
 
 public class DBdriver {
   public static void main(String args[]) {
     try {
       <.sybdriver = get a sybase driver.>
     } catch(Exception e) {
         System.err.println("error " + e);
 } } }
 -_-_-

[java.lang.Class, java.sql, com.sybase.jdbcx.SybDriver]

14.3 Getting Connections

<..connection = connect to database..>
 Connection connection = null;
 String server = "database";
 String port = "4101";
 String database = "c670ab";
 String username = args[0];
 String password = args[1];
 String url =   "jdbc:sybase:Tds:"
              + server + ":"
              + port + "/"
              + database;
 connection =
   DriverManager.getConnection(url, username, password);
 -_-_-

<..Connect.java..>
 import java.sql.*;
 import com.sybase.jdbcx.SybDriver;
 
 public class Connect {
   public static void main(String args[]) {
     try {
       <.sybdriver = get a sybase driver.>
       <.connection = connect to database.>
       if(!connection.isClosed()){
         System.out.println("Hurray!");
         connection.close();
       }
     } catch(Exception e) { System.err.println("error " + e); }
 } }
 -_-_-

14.4 Executing SQL’s DDL and Update Instructions

<..ddl and update..>
 Statement stat = connection.createStatement();
 stat.executeUpdate(
   "CREATE TABLE DBupdate (Name CHAR(10))" );
 stat.executeUpdate(
   "INSERT INTO DBupdate VALUES ( ’my name’)" );
 stat.executeUpdate(
   "DROP TABLE DBupdate" );
 stat.close();
 -_-_-

14.5 Query Instructions

<..java query instructions..>
 Statement stat = connection.createStatement();
 stat.executeUpdate(
   "CREATE TABLE DBquery (Name CHAR(10))" );
 stat.executeUpdate(
   "INSERT INTO DBquery VALUES ( ’my name’)" );
 stat.executeUpdate(
   "INSERT INTO DBquery VALUES ( ’your name’)" );
 
 ResultSet query =
   stat.executeQuery("SELECT * FROM DBquery");
 while( query.next() ){
   System.out.println( query.getString( "Name" ) );
 }
 query.close();
 
 stat.executeUpdate( "DROP TABLE DBquery" );
 stat.close();
 -_-_-

my name 
your name 

14.6 Navigating within Returned Tables

<..java navigation instructions..>
 Statement stat = connection.createStatement(
      ResultSet.TYPE_SCROLL_SENSITIVE,
      ResultSet.CONCUR_READ_ONLY);
 stat.executeUpdate(
      "CREATE TABLE DBnav (Name CHAR(10))" );
 stat.executeUpdate(
      "INSERT INTO DBnav VALUES ( ’my name’)" );
 stat.executeUpdate(
      "INSERT INTO DBnav VALUES ( ’your name’)" );
 ResultSet query =
      stat.executeQuery("SELECT * FROM DBnav");
 while( query.relative( 1 ) ){
   System.out.println( query.getString( "Name" ) );
 }
 // query.close();
 -_-_-

14.7 Java’s Field Updating Instructions

<..update result table..>
 Statement stat = connection.createStatement(
      ResultSet.TYPE_SCROLL_SENSITIVE,
      ResultSet.CONCUR_UPDATABLE);
 stat.executeUpdate(
      "CREATE TABLE DBupdatetbl (Name CHAR(10))" );
 stat.executeUpdate(
      "INSERT INTO DBupdatetbl VALUES ( ’my name’)" );
 
 ResultSet query =
      stat.executeQuery("SELECT * FROM DBupdatetbl");
 query.absolute(1);
 // not supported?
 query.updateString("Name", "your name");
 query.updateRow();
 

 -_-_-

14.8 Transactions

<..commit transactions..>
 connection.setAutoCommit(false);
 stat.executeUpdate(
   "INSERT INTO DBcommit VALUES ( ’my name’)" );
 stat.executeUpdate(
   "INSERT INTO DBcommit VALUES ( ’your name’)" );
 connection.commit();
 connection.setAutoCommit(true);
 -_-_-

14.9 Prepared Statements

<..prepared statements..>
 Statement stat = connection.createStatement();
 stat.executeUpdate(
       "CREATE TABLE DBprepare (Name CHAR(10), Age int)" );
 PreparedStatement prep = connection.prepareStatement(
       "INSERT INTO DBprepare VALUES ( ?, ?)" );
 prep.setString( 1, "my name" );
 prep.setInt( 2, 10 );
 prep.executeUpdate();
 stat.executeUpdate( "DROP TABLE DBprepare" );
 stat.close();
 -_-_-

14.10 Information about Tables

int getColumnCount() number of columns
String getColumnLabel(int column) column’s title for displays
String getColumnName(int column) column’s name
int getColumnType(int column) column’s SQL type
String getTableName(int column) column’s table name
int isNullable(int column) is NULL permitted?

<..info about tables..>
 ResultSet query =
    stat.executeQuery( "SELECT * FROM DBinfotbl" );
 ResultSetMetaData infoTbl = query.getMetaData();
 
 System.out.println(
   "number of columns: " + infoTbl.getColumnCount() +
   "\ncolumn label: " + infoTbl.getColumnLabel( 1 ) +
   "\ncolumn name: " + infoTbl.getColumnName( 1 ) +
   "\ncolumn type: " + infoTbl.getColumnType( 1 ) +
   "\ntable name: " + infoTbl.getTableName( 1 ) 

 );
 
 query.last();
 System.out.println(
   "number of rows: " + query.getRow()
 );
 

 query.close();
 -_-_-

number of columns: 1 
column label: Name 
column name: Name 
column type: 1 

14.11 Information about Databases

<..db meta info..>
 DatabaseMetaData meta = connection.getMetaData();
 System.out.println(
   "product name: " + meta.getDatabaseProductName() +
   "\ndriver name: " + meta.getDriverName() +
   "\nsupports full ANSI 92 SQL: " +
   meta.supportsANSI92FullSQL() +
   "\nsupports outer joins: " +
   meta.supportsOuterJoins()
 );
 -_-_-

product name: Sybase SQL Server 
driver name: jConnect (TM) for JDBC (TM) 
supports full ANSI 92 SQL: false 
supports outer joins: true 

14.12 References

14.13 Assignment #8

Due: Mo, Mar 9, midnight

Provide a Java program with embedded SQL code which performs the following operations.

  1. Creates the tables of Figure 1.2 in the textbook (page 7 4th ed; 8 5th ed).
  2. Reads a student’s name
  3. Prints the grade point average of the student

Notes

Q&A

Reference: Ch. 9.4 in textbook; sample Jhyton program (contributed by Rick Holbert)