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 1020509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:10:30+00:00 2026-05-16T11:10:30+00:00

As a C# developer new to Java, i thought it might be easiest if

  • 0

As a C# developer new to Java, i thought it might be easiest if i simply show a bit of C# code so i can see what the equivalent Java JDBC calls are:

String myConnectionString = "...";
String mySql = "select name from people where id = @myId";
int myId = 123456;

List<Field> fields = new List<Field>();
using (SqlConnection conn = new SqlConnection(myConnectionString)
{
  conn.Open();
  using (SqlCommand cmd = new SqlCommand(mySql,conn))
  {
    cmd.Parameters.AddWithValue("@myId", myId);
    using(SqlDataReader rdr = cmd.ExecuteReader())
    {
      while (rdr.Read())
      {
        String name = rdr.GetString(0);
        fields.Add(name);
      }
    }
  }
}

Now i know that the using statements above will safely close the database if anything goes wrong, whereas with java it’s a bit more complicated (try..finally or something). And in java, i’m not sure what exactly needs to be disposed or closed (or whatever) – the connection only? Or the statement as well?

If you could give me a leg up, that’d be great. Thanks a lot

edit: I really like the code here: (Java Exception Handling – Style)

Connection conn = MyDatabaseLayer.getConnection();
try {
  ... use conn ...
}
finally {
  conn.close();
}

However, do i need to do any further exception handling to ensure the statement and reader and all that other stuff gets closed? Or is closing the connection enough?

  • 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-05-16T11:10:30+00:00Added an answer on May 16, 2026 at 11:10 am

    Here’s a rough explanation of the steps, as liberally copied from some random page:

    first, you load the driver. This will be a class in your driver jar file. In many environments you get this, actually, from a datasource, this is sort of old fashioned, but probably better to see the nuts and bolts.

    Class.forName("com.imaginary.sql.msql.MsqlDriver");
    

    then you get a connection:

      Connection conn = DriverManager.getConnection("jdbc:msql://www.myserver.com:1114/....", "user1", "password");
    

    The url string tends to be different for different db vendors. Luckily we don’t swap databases too often, so you only need to look it up once. Now you can finally use the damned thing.

    PreparedStatement ps = conn.prepareStatement("SELECT * FROM FOO WHERE A=?", 1);
    

    A prepared statement gets cached, so you can use it with inseted parameters. It will work with a plain SQL statement, but for that you can just use a statement. You can also just call conn.executeQuery(…) to get a resultSet, which is what you want.

     ResultSet rs = ps.executeQuery();
    

    Now you can loop through the rs, and get whatever:

    while (rs.next())
    {
    ..
    }
    

    ResultSets also have ResultSetmetadata which gives you things like the column names, # of columns (but not the total # of results, which would be too easy).

    As for try catch, you need to close your statement/result set after you use them. Every time. Otherwise bad things will happen. Like leaving open resources on your db. Since your db connect method can throw errors, you rap the whole thing in a try catch, and close your statement (and connection, if you’ve made it here) in a finally block.

    This is why people use ORM frameworks in java.

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

Sidebar

Related Questions

I am developer from java background , new to ruby on rails. What's my
I'm a .NET developer who needs to use the Java platform for a new
I'm developer moving from C# to Java. Heard about new ASP net feature. <%:
I started a new job recently. I've been a Java & XML web developer
I am a java Developer,new to C# silverlight. in this class I want to
I'm a new Java developer and would like to find out if it's possible
I'm a .Net Developer and new to Java environment. I'm currently I'm using Netbeans
I am relatively new Java developer that's been thrown in the deep end, my
I'm new to Hibernate, and I can't get @OneToOne to function in our code.
I'm a new Java developer I've only ever done PHP in the past and

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.