Results 1 to 4 of 4
- 12-21-2011, 09:05 PM #1
Member
- Join Date
- Dec 2011
- Location
- Brazil
- Posts
- 26
- Rep Power
- 0
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); %>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!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; } }
Thanks in advance!Last edited by KingdomX; 12-21-2011 at 10:03 PM. Reason: removing personal info
- 12-21-2011, 09:11 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,386
- Blog Entries
- 7
- Rep Power
- 17
- 12-21-2011, 09:15 PM #3
Member
- Join Date
- Dec 2011
- Location
- Brazil
- Posts
- 26
- Rep Power
- 0
Re: Class constructor with boolean return possible?
Hmmmm, i see... so i can't really do this:
I see... Thanks, i'll make another method then and i'll use your info for future reference!Java Code:boolean test = new Db(); out.print(test);
Thanks a lot!
- 12-21-2011, 09:16 PM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Re: Class constructor with boolean return possible?
No.is class constructor with a boolean return possible?
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
-
Return Boolean
By FadedAura in forum New To JavaReplies: 2Last Post: 11-20-2011, 10:51 PM -
help with recursive return boolean
By Yakg in forum New To JavaReplies: 11Last Post: 01-11-2011, 05:54 PM -
random boolean in Constructor
By keo in forum New To JavaReplies: 3Last Post: 05-24-2010, 07:13 AM -
Return Statement and Boolean Help
By GhostShaman in forum New To JavaReplies: 8Last Post: 03-09-2010, 11:15 AM -
Boolean Return Value
By devstarter in forum New To JavaReplies: 5Last Post: 03-02-2010, 07:45 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks