Please send questions to
st10@humboldt.edu .
//
// modified from [CIS 318] Lab6.java, written by Ann Burroughs
//
// trying to demonstrate several of the getXXX() methods of
// class ResultSet, now grabbing columns by relative position
// instead of by column name
//
// NOTE: this WILL NOT WORK unless you add
// /home/univ/oracle/products/jdbc/lib/classes12.zip
// ...to your CLASSPATH!!!
//
// modified by: Sharon Tuttle
// last modified: 11-9-00
import java.sql.*;
public class DemoGetXXXByPos
{
public static void main(String args[])
{
Statement myStmt;
ResultSet myResultSet;
String myQuery, rowToPrint;
int rowCount;
try
{
// load JDBC driver
Class.forName("oracle.jdbc.driver.OracleDriver");
// create a connection to Oracle student database
// on redwood, using account java/java
Connection con = DriverManager.getConnection
("jdbc:oracle:thin:@redwood:1521:student",
"java", "java");
// declare and create a statement object, using
// a Connection method createStatement()
myStmt = con.createStatement();
// this String contains the text of
// my SQL query
myQuery = "select empno, ename, job, " +
" mgr, hiredate, sal, " +
" comm, deptno " +
"from emp ";
// this executes my query using the statement
// instance, returning the result as a
// resultset object
myResultSet = myStmt.executeQuery(myQuery);
rowCount = 0;
while (myResultSet.next() != false)
{
rowCount++;
// this time, the getXXX() arguments
// are the position of the column
// within ResultSet...
int empno = myResultSet.getInt(1);
String ename = myResultSet.getString(2);
String job = myResultSet.getString(3);
int mgr = myResultSet.getInt(4);
Date hiredate = myResultSet.getDate(5);
double sal = myResultSet.getDouble(6);
double comm = myResultSet.getDouble(7);
int deptno = myResultSet.getInt(8);
System.out.println("row: " + rowCount);
System.out.println("---------------------");
System.out.println("empno: " + empno);
System.out.println("ename: " + ename);
System.out.println("job: " + job);
System.out.println("mgr: " + mgr);
System.out.println("hiredate: " + hiredate);
System.out.println("sal: " + sal);
System.out.println("comm: " + comm);
System.out.println("deptno: " + deptno);
System.out.println("---------------------");
}
// CLOSE BOTH statement AND connection instances!!!
// IMPORTANT!!!
myStmt.close();
con.close();
}
catch (Exception e)
{
System.out.println(e);
}
}
}