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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:53:37+00:00 2026-05-15T02:53:37+00:00

hey all, I’m new to Java and was wondering if I define a method

  • 0

hey all, I’m new to Java and was wondering if I define a method to return a database object

like

import java.sql.*;

public class DbConn {

    public Connection getConn() {
        Connection conn;
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            if(System.getenv("MY_ENVIRONMENT") == "development") {
                String hostname = "localhost";
                String username = "root";
                String password = "root";
            }
            conn = DriverManager.getConnection("jdbc:mysql:///mydb", username, password);
            return conn;
        } catch(Exception e) {
            throw new Exception(e.getMessage());
        }

    }

}

if the connection fails when I try to create it what should I return? eclipse is telling me I have to return a Connection object but if it fails I’m not sure what to do.

thanks!

UPDATED CODE TO LET EXCEPTION BUBBLE:

public class DbConn {

    public Connection getConn() throws SQLException {
        Connection conn;
        String hostname = "localhost";
        String username = "root";
        String password = "root";

        Class.forName("com.mysql.jdbc.Driver").newInstance();
        if(System.getenv("MY_ENVIRONMENT") != "development") {
            hostname = "localhost";
            username = "produser";
            password = "prodpass";
        }
        conn = DriverManager.getConnection("jdbc:mysql:///mydb", username, password);
        return conn;

    }

}
  • 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-15T02:53:37+00:00Added an answer on May 15, 2026 at 2:53 am

    If an exception is thrown, there is no normal value returned from the method. Usually the compiler is able to detect this, so it does not even pester you with “return required” style warnings/errors. Sometimes, when it is not able to do so, you need to give an “alibi” return statement, which will in fact never get executed.

    Redefining your method like this

    public Connection getConn() {
        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            if(System.getenv("MY_ENVIRONMENT") == "development") {
                String hostname = "localhost";
                String username = "root";
                String password = "root";
            }
            conn = DriverManager.getConnection("jdbc:mysql:///mydb", username, password);
        } catch(Exception e) {
            // handle the exception in a meaningful way - do not just rethrow it!
        }
        return conn;
    }
    

    will satisfy Eclipse 🙂

    Update: As others have pointed out, re-throwing an exception in a catch block the way you did is not a good idea. The only situation when it is a decent solution is if you need to convert between different exception types. E.g. a method called throws an exception type which you can not or do not want to propagate upwards (e.g. because it belongs to a proprietary library or framework and you want to isolate the rest of your code from it).

    Even then, the proper way to rethrow an exception is to pass the original exception into the new one’s constructor (standard Java exceptions and most framework specific exceptions allow this). This way the stack trace and any other information within the original exception is retained. It is also a good idea to log the error before rethrowing. E.g.

    public void doSomething() throws MyException {
        try {
            // code which may throw HibernateException
        } catch (HibernateException e) {
            logger.log("Caught HibernateException", e);
            throw new MyException("Caught HibernateException", e);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 493k
  • Answers 493k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Looks like you don't have Commons Logging available in your… May 16, 2026 at 10:52 am
  • Editorial Team
    Editorial Team added an answer From the docs: CFBundleDevelopmentRegion (String - iOS, Mac OS X)… May 16, 2026 at 10:52 am
  • Editorial Team
    Editorial Team added an answer Not known yet, according to caniuse May 16, 2026 at 10:52 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Hey all, my Computational Science course this semester is entirely in Java. I was
Hey all. I have a server written in java using the ServerSocket and Socket
Hey all. Newbie question time. I'm trying to setup JMXQuery to connect to my
Hey all, I have something of an interesting requirement for my project. I need
Hey all. We're sending quite a few emails (around 23k) using IIS6 SMTP service
Hey all - I have an app where I'm authenticating the user. They pass
Hey all, I'm pulling my hair out on this one. I've checked all my
Hey all, I am trying to test if the argument passed into my function
Hey all. I am working on a project for school where we are given
Hey all. I'm trying to see about handling events in a console application. I

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.