MySqlTalk.com  

Go Back   MySqlTalk.com > Programming Languages and MySQL > Java
User Name
Password
FAQ Members List Calendar Search Today's Posts Mark Forums Read


Reply
 
Thread Tools Search this Thread
Old 04-30-2005, 08:39 AM   #1
gertcuppens
Member
 
Join Date: Feb 2005
Location: ANtwerp - Belgium
Posts: 34 gertcuppens is on a distinguished road
How to connect to MySQL (2)

If you're building java web applications, it is best you write them following the Model View Controller pattern design.
Therefor, you will need DAO's or Data Access Objects. These are classes which contain all the SQL statements you need to look up the information of your MySQL database.

And a very good trick is to use an abstract class GeneralDao. Here you define which driver you will use. All other DAO's will inherit from this abstract class. The advantage of this is that you will have to change in just one class if you are changing the database driver.

here's the code

Code:
package org.gertcuppens.cluifDao; /** * @author GC1494 * * de GeneralDao is de algemene klasse waar alle andere DAO's of Data Access * Objects van erven. Het is de bedoeling om in deze klasse éénmaal de * getConnection te definiëren * <p> * getDatabaseConnection() connecteert met de databank afhankelijk van de JDBC-driver * die voorhanden is. Deze methode zorgt ervoor dat alle afgeleide DAO's van * dezelfde driver gebruik maken terwijl er toch maar op één plaats de naam * van de driver gedefinieerd is. * </p> * * */ //import java.util.*; import java.util.ResourceBundle; import java.sql.*; /* import org.apache.log4j */ import org.apache.log4j.Logger; /* import org.gertcuppens.algemeen */ import org.gertcuppens.algemeen.Melding; public abstract class GeneralDao { protected Connection getDatabaseConnection() throws Melding { Connection dbconn = null; ResourceBundle resBundle; Logger gcoLogger = Logger.getLogger("gco.log"); System.out.println("GeneralDao - start connectie databank " ); gcoLogger.debug("GeneralDao - start connectie databank " ); try { Class.forName("com.mysql.jdbc.Driver").newInstance(); resBundle = ResourceBundle.getBundle("gcoConfig"); gcoLogger.debug("ophalen resourceBundle (gcoConfig) " ); String dbConnectie = resBundle.getString("databaseconnection"); gcoLogger.debug("lezen databaseconnection in resourceBundle " ); //dbconn = DriverManager.getConnection("jdbc:mysql://localhost/gco"); dbconn = DriverManager.getConnection(dbConnectie,"gert", "sesam"); gcoLogger.debug("maken connectie databank " ); } catch (InstantiationException exc) { System.out.println("GeneralDao - Fout bij getConnection - instantiation " ); gcoLogger.fatal("GeneralDao - Fout bij getConnection - instantiation " ); // exc.printStackTrace(); Melding melding = new Melding(1,"instantiëring is niet gelukt bij getConnection()"); throw melding ; } catch (ClassNotFoundException exc) { System.out.println("GeneralDao - Fout bij getConnection - class not found " ); gcoLogger.fatal("GeneralDao - Fout bij getConnection - class not found " ); Melding melding = new Melding(2,"class is niet gevonden bij getConnection()"); throw melding; } catch (IllegalAccessException exc) { System.out.println("GeneralDao - Fout bij getConnection - illegal acces " ); gcoLogger.fatal("GeneralDao - Fout bij getConnection - illegal acces " ); Melding melding = new Melding(3,"toegang is niet toegelaten bij getConnection()"); throw melding; } catch (SQLException exc) { System.out.println("GeneralDao - Fout bij getConnection - SQLException " ); gcoLogger.fatal("GeneralDao - Fout bij getConnection - SQLExeption " ); Melding melding = new Melding(exc); throw melding; } return dbconn; } } /* public abstract class GeneralDao */
gertcuppens is offline   Fork this post Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Import Access, Excel and data of other formats to MySQL by Navicat NavicatGuy Articles 0 08-17-2004 01:08 AM
3.2 Entering Queries Administrator Documentation 0 06-09-2004 02:58 AM
1.4.3.2 Using the MySQL Software for Free Under GPL Administrator Documentation 0 06-08-2004 02:20 AM
1.2.2 The Main Features of MySQL Administrator Documentation 0 06-08-2004 01:55 AM
1.2 Overview of the MySQL Database Management System Administrator Documentation 0 06-07-2004 10:11 PM



All times are GMT -4. The time now is 05:56 AM.



Powered by: vBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Limited.
Google
  Web http://www.mysqltalk.com
DISCLAIMERS:
1. We have no commercial interest in this site.
Banner Ads and Subscriptions will only be used to help pay for hosting and maintenance costs.
2. MySQLTalk.com is NOT affiliated with MySQL AB in any way.
3. MySQLTalk.com is NOT endorsed by MySQL AB in any way.
4. Please do not post any content that is harmful to MySQL or MySQL AB, meaning no misleading or obsolete information will be tolerated.
Well-founded constructive criticism meant to help the community is permitted.
5. This website is founded with the goal of improving the MySQL community.
We not only tolerate newbies, we encourage them.
Please do not ask newbies to "read the manual".