This slide shows how to use HTML form to send the user input from a single-line input text field to a server for processing.
If you type a region, Central, East, or West, in the text field below and click the “List” button, the browser will call the following scripts:
⇒ ⇒ ⇒ ⇒
and list the corresponding stores in that region.
An empty region will list all stores.
The following Perl script is mainly used to process the web inputs.
It could be skipped by letting the shell and Java scripts handle the web inputs.
However, Perl is known for its strong string processing, which is greatly needed for web processing, so the Perl script is kept to do most part of string processing.
The following JDBC script accesses the database and retrieves the stores.
~/public_html/cgi-bin/jdbc/List.java
/*******************************************************************
This program shows how to list the stores in the
stores table.
To use this program, you need to create a table
stores by using the following commands:
SQL> create table stores (
2 store_key INTEGER PRIMARY KEY,
3 city VARCHAR(32) NOT NULL,
4 region VARCHAR(16) NOT NULL );
Table created.
*******************************************************************/
// Import the following packages to use JDBC.
import java.sql.*;
import java.io.*;
import oracle.jdbc.*;
import oracle.jdbc.pool.OracleDataSource;
class List {
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( );
try {
// Create, compose, and execute a statement.
Statement stmt = conn.createStatement( );
String query = "select store_key, city, region from stores ";
query += "where region like '%" + args[0].trim( ) + "%'";
System.out.println( query + "<b>" );
ResultSet rset = stmt.executeQuery( query );
// Iterate through the result and print the data.
while ( rset.next( ) ) {
System.out.print( rset.getString(1) + ", " + rset.getString(2) );
System.out.print( ", " + rset.getString(3) );
}
// Close the ResultSet and Statement.
rset.close( );
stmt.close( );
}
catch ( SQLException ex ) {
System.out.println( ex );
}
// Close the Connection.
conn.close( );
}
}