/*********************************************************
This program shows how to list the book titles in the
inventory table.
To use this program, you need to create a table
inventory by using the following command:
SQL> create table books (
2 title varchar(128) not null,
3 ISBN char(12) primary key,
4 authors varchar(128) not null,
5 price number(5,2) not null check( price >= 0.0 ),
6 quantity integer not null check( quantity >= 0 ) );
Table created.
*********************************************************/
// You need to import the java.sql package to use JDBC.
import java.sql.*;
import java.io.*;
class ListTitles {
public static void main( String args[ ] )
throws SQLException {
// Load the Oracle JDBC driver.
DriverManager.registerDriver ( new oracle.jdbc.driver.OracleDriver( ));
// Connect to the database. You can put a database
// name after the @ sign in the connection URL.
Connection conn = DriverManager.getConnection ( "jdbc:oracle:thin:@172.20.4.9:1521:aero", "userid", "password" );
try {
Statement stmt = conn.createStatement ( );
String query = "select title from books where ";
query += "authors like '%" + args[0].trim( ) + "%'";
System.out.print( "" + query + "
" );
ResultSet rset = stmt.executeQuery( query );
// Iterate through the result and print the data.
while ( rset.next( ) ) {
System.out.print ( rset.getString( 1 ) );
System.out.println( "
");
}
// Close the Statement and ResultSet.
stmt.close( );
rset.close( );
}
catch ( SQLException ex ) {
System.out.println( ex );
}
// Close the connection.
conn.close( );
}
}