Home arrow static arrow Java Programming [Archive] - mysql driver problem...
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - mysql driver problem...
This topic has 3 replies on 1 page.

Posts:73
Registered: 7/13/03
mysql driver problem...  
Jun 30, 2004 8:28 AM



 
Hey guys,

I've just written a simple class for connecting to my MySQL database. MySQL connector is in the classpath and the example is quite the same as described in the documentation of mysql. I can't load the driver. Seems that my brain cares about the weather...

public class DBConnection { 	private static DBConnection instance = nullprivate String DRIVER   = "";	private String HOST     = "";	private String DB       = "";	private String USER     = "";	private String PASSWORD = "";		private Connection conn = nullprivate DBConnection() throws DBConnectionException { 		Properties properties = new Properties();		try {			properties.load(				new FileInputStream("conf/db.properties"));		} catch (IOException e) {			throw new DBConnectionException("Could not load database settings!");		}				DRIVER = properties.getProperty("Driver");		HOST = properties.getProperty("Host");		DB = properties.getProperty("DB");		USER = properties.getProperty("User");		PASSWORD = properties.getProperty("Password"); 		// load the driver...		try {                        //Driver: com.mysql.jdbc.Driver			Class.forName(DRIVER).newInstance();		} catch (Exception e) {			throw new DBConnectionException("Could not load the driver!");		}	}		public static DBConnection getInstance() throws DBConnectionException {		if (instance == null) {			return new DBConnection();		} else {			return instance;		}	} 	public void open() throws DBConnectionException { 		// get a connection...		try {			conn =				DriverManager.getConnection(					"jdbc:mysql://" + HOST + "/" + DB + "?user=" + USER + "&password=" + PASSWORD);		} catch (SQLException e) {			throw new DBConnectionException("Could not connect to the Database!");		}	}		// testing...	public static void main(String[] inArgs) {		try {			System.out.println("Loading the driver...");			DBConnection db = DBConnection.getInstance();			System.out.println("Opening connection...");			db.open();		} catch (DBConnectionException e) {			System.out.println(e.getMessage());		}	}}


Output:
test_db:     [java] Loading the driver...     [java] Could not load the driver!

The properties are well parsed. Any ideas?

Thanks a ton!

 

Posts:24,036
Registered: 2/3/03
Re: mysql driver problem...  
Jun 30, 2004 8:34 AM (reply 1 of 3)



 
Things to check:

1) Add a System.out.println(DRIVER) call after you get it from the properties to ensure you're using the correct one.

2) Ensure the MySQL jar is in the runtime classpath by explicitly running from the commandline:
java -cp .;<path/to/yourMysqlConnector.jar> DBConnection


§
 

Posts:24,036
Registered: 2/3/03
Re: mysql driver problem...  
Jun 30, 2004 8:36 AM (reply 2 of 3)



 
Also, you're covering up the Exception - by at least doing a stack trace at this point, you'll get SOME useful information...
try {     //Driver: com.mysql.jdbc.Driver    Class.forName(DRIVER).newInstance();	} catch (Exception e) {    e.printStackTrace();    throw new DBConnectionException("Could not load the driver!");		}
 

Posts:73
Registered: 7/13/03
Re: mysql driver problem...  
Jun 30, 2004 9:00 AM (reply 3 of 3)



 
Outdoor programming is dangerous...
Working with an IDE and ANT in a console is even more dangerous, at least if you have set the wrong classpath in the build file :)

Thanks yawmark...
 
This topic has 3 replies on 1 page.