Please send questions to
st10@humboldt.edu .
//
// modified from [CIS 318] Lab6.java, written by Ann Burroughs
//
// let's write a flat, comma-delimited file giving the emp data;
// we'll call it emp.dat.
//
// 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: 4-16-01
import java.sql.*;
import java.io.*;
public class BuildEmpFile
{
public static void main(String args[])
{
Statement myStmt;
ResultSet myResultSet;
String myQuery, rowToWrite;
int rowCount;
File empFile;
FileWriter toEmpFile;
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);
// set up the file and stream for writing
try
{
empFile = new File("emp.dat");
toEmpFile = new FileWriter(empFile);
// write each row in result set to file emp.dat
while (myResultSet.next() != false)
{
int empno = myResultSet.getInt("empno");
String ename = myResultSet.getString("ename");
String job = myResultSet.getString("job");
int mgr = myResultSet.getInt("empno");
Date hiredate = myResultSet.getDate("hiredate");
double sal = myResultSet.getDouble("sal");
double comm = myResultSet.getDouble("comm");
int deptno = myResultSet.getInt("deptno");
rowToWrite = "" + empno + ","
+ ename + ","
+ job + ","
+ mgr + ","
+ hiredate + ","
+ sal + ","
+ comm + ","
+ deptno;
toEmpFile.write(rowToWrite + "\n");
}
toEmpFile.close();
}
catch(FileNotFoundException exc)
{
System.out.println("BuildEmpFile: Could "
+ "not open emp.dat");
}
catch(IOException exc)
{
System.out.println("BuildEmpFile: IOError: "
+ exc.getMessage());
}
finally
{
// CLOSE BOTH statement AND connection
// instances!!! IMPORTANT!!!
myStmt.close();
con.close();
}
}
catch (Exception e)
{
System.out.println(e);
}
}
}