Java Reference
In-Depth Information
import java.sql.*;
import java.util.*;
public class Logging{
public static void main(String args[]){
PrintWriter errLog = null;
PrintStream stderr = null;
try{
FileOutputStream errors = new FileOutputStream ("StdErr.txt",
true);
stderr = new PrintStream (errors);
errLog = new PrintWriter(errors,true);
}
catch (Exception e){
System.out.println ("Redirection error: Unable to open SystemErr.txt");
}
System.setErr ( stderr );
int qty;
float cost;
String name;
String desc;
String query = "SELECT Name,Description,Qty,Cost,Sell_Price FROM Stock";
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection
("jdbc:odbc:Inventory");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
name = rs.getString("Name");
desc = rs.getString("Description");
qty = rs.getInt("Qty");
cost = rs.getFloat("Cost");
System.out.println(name+", "+desc+"\t: "+qty+"\t@ $"+cost);
}
}
catch(ClassNotFoundException e){
e.printStackTrace(errLog);
}
catch(SQLException e){
System.err.println((new GregorianCalendar()).getTime());
System.err.println("Thread: "+Thread.currentThread());