• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Connection to a MySQL Database  RSS feed

 
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?


 
Sheriff
Posts: 12318
200
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.
 
Marshal
Posts: 6008
414
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.
 
Saloon Keeper
Posts: 9213
177
  • 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.
 
Sheriff
Posts: 5100
138
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.
 
Bartender
Posts: 19719
92
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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!