• Post Reply Bookmark Topic Watch Topic
  • New Topic

Connection to a MySQL Database  RSS feed

 
Andrew Beans
Greenhorn
Posts: 1
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I've written this MySQL database class for my Java application and I'd just like some feedback on it before moving onto making my next class.

The idea here is that I have a simple properties file which stores the hostname, username, password, and database name. I then connect to the database (if possible) and return the Connection.

Although I am not really sure what should happen if the properties file doesn't exist, or the MySQL connection fails, should I return Connection which would be null, throw a new exception, or re-throw the existing exception?


 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Throwing an exception is probably the best choice when you can't establish a connection in this class. Experiment with how you'd write the client code if this were the case. Then compare it with how you'd write the client code if you returned a null Connection instead. Between throwing a new exception that wraps the SQLException vs. just re-throwing the SQLException or IOException, experiment with how you'd write the client code to handle or propagate those exceptions. In the end, you have to consider how your decision here affects the code that uses this API. To paraphrase JFK (but with less eloquence): Think not only of what you must do in this class, think also of what other classes must do because of this class.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think also about having the logic to close connection when your job is done. Before closing connection, check if it was connected at all.
 
Stephan van Hulst
Saloon Keeper
Posts: 7974
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Andrew, welcome to CodeRanch!

I wouldn't make the Database class responsible for getting the connection properties. Pass the required properties to the getConnection() method. That said, your class is actually unnecessary if you use the DataSource interface, which JDBC has an implementation of.
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably don't need this line, if you have a modern JDBC driver.
 
Tim Holloway
Saloon Keeper
Posts: 18792
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:You probably don't need this line, if you have a modern JDBC driver.


Explanation. The Class.forName method was originally needed to force the JVM's classloader to locate, load and activate the JDBC driver class in question. For about 10 years more or less it has been possible to construct JARs with special Manifest directives (in the META-INF) directory that permit the same process to operate automatically. Which is why you don't need Class.forName anymore.

As a more abstract observation, actually manually constructing Connection objects is something that has limited utility in industrial-grade systems these days. Web applications should be using a server-constructed Database Connection Pool, and complex stand-alone applications - especially multi-threaded ones - also often perform better using a Connection Pool. So manual construction is mostly for single-user apps.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!