cphilpot
cphilpot

Reputation: 1225

database connection on local machine

I was originally coding on a server with Apache installed and used this function to connect to the database

public Connection getDBConnection()
{
java.sql.Connection conn=null;
//synchrnized(this)
    try
    {
        DriverManager.registerDriver(new OracleDriver());
        conn = DriverManager.getConnection("jdbc:oracle:thin:@dukeorrac01:1521:ORDB1","nrsc","nrsc");

    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    return conn;        
}

Probably not the best way to do it, but it worked. However, I've moved to coding on my local machine and I was given jboss as an IDE to work with. I've gotten everything working (sorta) except, it seems to be connecting to a different databse. I get exhausted resultsets (which isn't right). I "thought" the "getConnection("jdbc:oracle....") was what established the connection to the actual database.

Why is this not working on my local machine when it worked perfectly fine on the remote server?

P.S. I'm new to database/server configuration stuff. So, don't assume I know some step in setting up a database or server. Also, I did not create this orginally. It was given to me to use.

Thanks

Upvotes: 1

Views: 240

Answers (1)

uaarkoti
uaarkoti

Reputation: 3657

The error seems to indicate that you are not closing your result sets properly. In the following example see how after a statement is executed you have to close those resources in the finally block.

W.r.t why you did not see this error before is probably because your server probably had lots of resources whereas your local machine has limited resources and your default settings are not modified to reflect the needs of your application.

In general, its better to do basic house keeping like closing all the open result sets and statements whether or not you have enough resources allocated on your DB.

public void getDBConnection() {
    Connection conn = null;
    Statement statement = null;
    ResultSet rs = null;

    try {
        connection  = DriverManager.getConnection("jdbc:oracle:thin:@dukeorrac01:1521:ORDB1","nrsc","nrsc");
        statment = connection.createStatement();
        statement.setFetchSize(Integer.MIN_VALUE);

        // Do more stuff, iterate to ResultSet etc...
    } catch (SQLException ex) {
        // Exception handling stuff
        ...
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) { /* ignored */}
        }
        if (statment != null) {
            try {
                statment.close();
            } catch (SQLException e) { /* ignored */}
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) { /* ignored */}
        }
    }
}

Upvotes: 1

Related Questions