A Sample JDBC Program


The following sample program is modified from a program in the page Sample Applications. Many JDBC program examples can be found from there. This program includes the following SQL commands:

 SQL> DROP TABLE student_table;
 Table dropped.
	 
 SQL> CREATE TABLE student_table (
   2    ID   INTEGER GENERATED ALWAYS AS IDENTITY,
   3    name VARCHAR(32),
   4    age  INTEGER );
   5    /
 Table created.

 SQL> INSERT INTO student_table ( name, age )
   2    VALUES ( 'Digi Mon', 10 );
 1 row created.

 SQL> SELECT * FROM student_table s
   2    WHERE name LIKE '%Mon%';
	 
     ID        NAME         AGE
   ------ -------------- ---------
      3   Poke Mon          25
      1   Digi Mon          10




    Drop   Create   Insert   Select   (args[0])

     Name (args[1]) = [for insert and select (listing all if empty)]

     Age (args[2]) = (for insert)

                  


// Import the following packages to use JDBC.
import  java.sql.*;
import  java.sql.DatabaseMetaData;
import  java.io.*;
import  oracle.sql.*;
import  oracle.jdbc.*;
import  oracle.jdbc.pool.OracleDataSource;

class  Student {
  public static void  main( String args[ ] ) throws SQLException {
    String user     = "C##user_id";
    String password = "password";
    String database = "20.185.147.112:1521/xe";

    // Open an OracleDataSource and get a connection.
    OracleDataSource ods = new OracleDataSource( );
    ods.setURL     ( "jdbc:oracle:thin:@" + database );
    ods.setUser    ( user );
    ods.setPassword( password );
    Connection conn = ods.getConnection( );
	
    String     cmd;
    Statement  stmt = conn.createStatement( );
    ResultSet  rset;
    DatabaseMetaData  dbm = conn.getMetaData( );

    if ( args[0].equals( "drop" ) ) {
      cmd  = "drop table student_table";
      System.out.println( cmd );
      stmt.execute( cmd );
    }

    else if ( args[0].equals( "create" ) ) {
      // Check if "student_table" table is there.
      rset = dbm.getTables(
               null, "C##user_id", "STUDENT_TABLE", new String[] {"TABLE"} );
      if ( rset.next( ) ) {
        // Table does exist.
        cmd  = "drop table student_table";
        System.out.println( cmd );
        stmt.execute( cmd );
      }
 
      cmd  = "create table student_table (";
      cmd += "  ID   integer generated always as identity,";
      cmd += "  name VARCHAR(32),";
      cmd += "  age  INTEGER )";
      System.out.println( cmd );
      stmt.execute( cmd );

      cmd  = "insert into student_table( name, age ) values (";
      cmd += "  'Digi Mon', 10 )";
      System.out.println( cmd );
      stmt.execute( cmd );
      rset.close( );
    }

    else if ( args[0].equals( "insert" ) ) {
      cmd  = "insert into student_table( name, age ) values ( '";
      cmd += args[1].trim( ) + "', '" + args[2].trim( ) + "' )";
      System.out.println( cmd );
      stmt.execute( cmd );
    }

    else if ( args[0].equals( "select" ) ) {
      cmd  = "select * from student_table s where ";
      cmd += "name like '%" + args[1].trim( ) + "%'";
      System.out.println( cmd );
      rset = stmt.executeQuery( cmd );

      while ( rset.next( ) ) {
        // Print the results.
        System.out.println( "\nStudent ID => " + rset.getInt(1) );
        System.out.println( "Student name => " + rset.getString(2) );
        System.out.println( "Student age => "  + rset.getInt(3) );
      }
      rset.close( );
    }
    stmt.close( );
    conn.close( );
  }
}