Stream Closed Error

Hello!

I have this 2-class package which gets informations on Microsoft SQL Server 2005 tables and table content. It worked perfectly on J2SE 5. Today I moved to Java J2SE1.6 u1 and recompiled the project. Now, it triggers bizzare Steam Closed exceptions when trying to read a line from the keyboard (if I let exceptions to be thrown). I found several posts regarding this readLine-stream closed issue on the java forums, but none answered this problem satisfactorily. This is my code:

package databaseinfo;

import java.io.*;

import java.sql.*;

publicclass DatabaseInfo{

privatestatic DBConnection dbConn;// connection to DB

/** connects to the SQL Server 2005, using the given credntials

*/

publicstaticboolean connect(){

String username ="test";

String password ="test";

String driver ="com.microsoft.sqlserver.jdbc.SQLServerDriver";// JDBC driver for connecting to MSSQL 2005

String url ="jdbc:sqlserver://FIREBLADE\\SQLEXPRESS";// JDBC connection URL to MSSQL 2005

try{

dbConn =new DBConnection(username,password,driver,url);

}

catch (Exception e){returnfalse;}

returntrue;

}

/** disconnects from the SQL Server 2005

*/

publicstaticvoid disconnect(){

try{

dbConn.logout();

}

catch (Exception e){}

}

/** prints the list of tables belonging to the user (dbo) from the current

* database

*/

publicstaticvoid printTablesList(){

try{

dbConn.printQueryResult("EXEC sp_tables @table_owner=dbo");

}

catch (Exception e){return;}

}

/** prints extra information and the content of a user-given table

*/

publicstaticvoid printTableInfoAndContent()throws IOException{

try{

BufferedReader in =new BufferedReader(

new InputStreamReader(System.in));

String tableName;

System.out.println("\nGet extra information and the content of which table? ");

tableName = in.readLine();

in.close();

dbConn.printQueryResult("EXEC sp_columns @table_name=" + tableName);

System.out.println();

dbConn.printQueryResult("SELECT * FROM " + tableName);

}

catch (IOException e){}

}

publicstaticvoid main(String args[])throws IOException{

connect();

printTablesList();

printTableInfoAndContent();

disconnect();

}

}

package databaseinfo;

import java.io.*;

import java.sql.*;

publicclass DBConnection{

// maximum no. of rows on the user console screen

privatefinalstaticint maxRowCount = 25;

private Connection conn;// connection to DB

/** connects to the MSSQL 2005 database

*/

public DBConnection (String username, String password, String driver,

String url)throws ClassNotFoundException, SQLException{

login(username,password,driver,url);

}

/** login to SQL Server 2005 for a user having a username and a passsword,

* using a certain database driver and a given url

*/

publicint login(String username, String password, String driver, String url)

throws ClassNotFoundException,

SQLException{

Class.forName(driver);// sets the JDBC driver

conn = DriverManager.getConnection(url,username,password);// obtains the JDBC connection

if (conn !=null)

System.out.println("\nSQL Server Connection established ...\n");

else{

System.out.println("\nBad username and/or password.\n");

return 0;

}

return 1;

}

/** closes the JDBC connection

*/

publicvoid logout()throws SQLException{

conn.close();

System.out.println("\nSQL Server Connection closed.\n");

}

/** executes SQL queries and prints their output to the screen

*/

publicvoid printQueryResult(String sql){

try{

BufferedReader in =new BufferedReader(

new InputStreamReader(System.in));

int rowCount = 0;

Statement st = conn.createStatement();

ResultSet rs = st.executeQuery(sql);

ResultSetMetaData rsmd = rs.getMetaData();

int numberOfColumns = rsmd.getColumnCount();

int i;

for (i = 1; i <= numberOfColumns; i++)

System.out.print(rsmd.getColumnName(i)+"\t");

System.out.println();

while (rs.next()){

if ((rowCount != 0) && (rowCount % maxRowCount == 0)){

System.out.println("\nPress ENTER to continue ... ");

in.read();

}

for (i = 1; i <= numberOfColumns; i++)

System.out.print(rs.getString(i) +"\t\t");

rowCount++;

System.out.println();

}

in.close();

}

catch (Exception e){}

}

}

Thanks for reading this post!

[9435 byte] By [domnul_mihneaa] at [2007-11-27 9:58:00]
# 1
Don't close the input stream, and don't create a new one every time you want to read - create one for the life of the program.
ejpa at 2007-7-13 0:28:26 > top of Java-index,Core,Core APIs...