Results 1 to 4 of 4
  1. #1
    KingdomX is offline Member
    Join Date
    Dec 2011
    Location
    Brazil
    Posts
    26
    Rep Power
    0

    Question Class constructor with boolean return possible?

    As the title states, is class constructor with a boolean return possible? I've created a Database class and i was wondering how could i return a successful connection to the database... here's the code:

    index.jsp
    Java Code:
                    <%
                    Db test = new Db();
                    out.print(test);
                    %>
    Java Code:
    package Database;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.sql.ResultSet;
    
    public class Db {
        private String driver = "com.mysql.jdbc.Driver";
        private String URL = "jdbc:mysql://localhost/seuBanco";
        private String USE = "root";
        private String SENHA = "";
        private Connection conn;   
      
        public boolean Db(){  
            try{ Class.forName(driver);  
                Connection con = DriverManager.getConnection(URL, USE, SENHA);  
                Statement stmt = con.createStatement();  
                ResultSet rs = stmt.executeQuery("QUERY");  
            }catch(Exception e){  
               return false;
            }  
            return true;
        }
    }
    Could any give me some help with this one? I'm a little lost at Java, just started developing with it and got a LOT to learn still!

    Thanks in advance!
    Last edited by KingdomX; 12-21-2011 at 10:03 PM. Reason: removing personal info

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,368
    Blog Entries
    7
    Rep Power
    20

    Default Re: Class constructor with boolean return possible?

    Quote Originally Posted by KingdomX View Post
    As the title states, is class constructor with a boolean return possible?
    Nope, constructors don't return anything and so they don't have a return type. Why not use another method for that?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    KingdomX is offline Member
    Join Date
    Dec 2011
    Location
    Brazil
    Posts
    26
    Rep Power
    0

    Default Re: Class constructor with boolean return possible?

    Hmmmm, i see... so i can't really do this:
    Java Code:
    boolean test = new Db();
    out.print(test);
    I see... Thanks, i'll make another method then and i'll use your info for future reference!

    Thanks a lot!

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Class constructor with boolean return possible?

    is class constructor with a boolean return possible?
    No.

    Your constructor can, however, throw an exception. Or you can not have a public constructor at all and use a factory method instead.

    The code in Oracle's JDBC Tutorial has the class provide a public method that *returns* a Connection (or throws an exception). That tutorial would be a good place to start.

Similar Threads

  1. Return Boolean
    By FadedAura in forum New To Java
    Replies: 2
    Last Post: 11-20-2011, 10:51 PM
  2. help with recursive return boolean
    By Yakg in forum New To Java
    Replies: 11
    Last Post: 01-11-2011, 05:54 PM
  3. random boolean in Constructor
    By keo in forum New To Java
    Replies: 3
    Last Post: 05-24-2010, 07:13 AM
  4. Return Statement and Boolean Help
    By GhostShaman in forum New To Java
    Replies: 8
    Last Post: 03-09-2010, 11:15 AM
  5. Boolean Return Value
    By devstarter in forum New To Java
    Replies: 5
    Last Post: 03-02-2010, 07:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •