// 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( ); } } |