Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8219217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:04:45+00:00 2026-06-07T13:04:45+00:00

I have written the following java code to connect the Oracle Express Edition database…..I

  • 0

I have written the following java code to connect the Oracle Express Edition database…..I have read a lot about closing the connection, statement and result but there are some questions about this issue…

1- I usually test my code several time so If I Don not close the connection and etc. will I face with any problem?!!(because the connection is made on the same variable or object in my code!!!!)

2-how can I understand how many connections exist now?(after testing the code for several time)

3- Should I place CLOSE methods in finally block with a new TRY CATCH block or add THROWS SQLException after main method?!!!! what is the difference between these to implementation?!!!

package mainAssignment;

import java.sql.*;
import java.text.MessageFormat;
import java.math.*;
import java.util.*;
//import java.text.MessageFormat;

public class Conn {

/**
 * @param args
 */
public static void main(String[] args)throws SQLException{
    // TODO Auto-generated method stub

    String jdbcURL = "jdbc:oracle:thin:@localhost:1521:xe";
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    String user = "hr";
    String passwd = "hr";
    Scanner input = new Scanner(System.in);
    try {

        Class.forName("oracle.jdbc.driver.OracleDriver");
        conn = DriverManager.getConnection(jdbcURL, user, passwd);
        System.out.println("ok");
        System.out.println("Please enter the desired employee ID: ");
        Object[] arg={new String(input.nextLine())};
        String query = MessageFormat.format(
            "select * from EMPLOYEES where employee_id={0}", arg);
        //System.out.println(query);
        stmt = conn.createStatement();
        stmt.executeQuery(query);
        rs = stmt.getResultSet();
        while (rs.next()) {
                BigDecimal empid = rs.getBigDecimal(1);
                String firstname = rs.getString("FIRST_NAME");
                System.out.println("employee ID " + empid
                        + "  first name is " + firstname);
            }

    } catch (SQLException e) {
        e.printStackTrace();
        // TODO
    } catch (ClassNotFoundException e) {
        // TODO
        e.printStackTrace();
    } finally {
        conn.close();
        rs.close();
    if (conn.isClosed())
        System.out.println("no connection any more");
    else
        System.out.println("connection exists");
    }
   }
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T13:04:46+00:00Added an answer on June 7, 2026 at 1:04 pm

    1) You’ll be left with many idle (hanging) connections after you finish the execution. To prevent that check whether the connection is null. If it is – establish a new one, if it’s not null – use existing.

    2) You can check it on the sql server. Example for ms sql server: How to determine total number of open/active connections in ms sql server 2005

    3) Putting close methods in finally block is a good practice. Don’t change that. Sometimes finally block also requires its own inner try-catch-finally block.

    Side note: if you create methods that throw exceptions you’re not solving any issues. it’s just pushing the need to maintain the exception to the person who’ll use your methods. Sometimes it’s a good approach but more often it is not.

    In real life applications you should also check whether the database is available, the connection was established and the query result is not null.

    Appendix 1: How to check the number of active connections in oracle express:

    How to list active / open connections in Oracle?

    http://dbalink.wordpress.com/2008/06/08/find-active-sessions-in-oracle-database/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written the following code for inserting video bytes into the database DBAdapter.java
I have written following code for getting location name : UseGpsActivity.java public class UseGpsActivity
I have written following code to get location name package demo.gps.locname; import java.io.IOException; import
I have written following code for the client of RMI. But getting java.rmi.ConnectException: Connection
I have written the following code: import java.util.Calendar; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; class Voter
i have written a java code for updating the element value with the following
i have written the following code to download file. java.io.BufferedInputStream in = new java.io.BufferedInputStream(new
I have written following code to attach gesture recogniser to multiple imageviews. [imageview1 setUserInteractionEnabled:YES];
I am trying my hands on WPF MVVM. I have written following code in
i have written the following code class Program { static void Main(string[] args) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.