Ok so what i want to be able to do is connect to a mysql database. Here's my code so far:

Connection connection = null; 
		try { 
		// Load the JDBC driver 
		String driverName = "postgresql-8.4-701.jdbc4.jar"; // MySQL MM JDBC driver 
		Class.forName(driverName); 

		// Create a connection to the database 
		String serverName = "mysql10.000webhost.com"; 
		String mydatabase = "a2362883_test"; 
		String url = "jdbc:mysql://" + serverName + "/" + mydatabase; // a JDBC url 
		String username = "a2362883_admin"; 
		String password = "test"; 
		connection = (Connection) DriverManager.getConnection(url, username, password);
		System.out.println("Connected");		
		}
		catch (ClassNotFoundException e) { 
		System.out.println("Could not find the database driver");
		} catch (SQLException e) { 
		System.out.println("Could not connect to the database");
		}

The problem is though that it keeps saying it could not find the database driver, even though i put postgresql-8.4-701.jdbc4.jar in the project file. So therefore it should find it, or is this not the driver? Im using java 1.6, snow leopard os and eclipse. By the way, the password isnt test but im not going to tell you it :P

Thanks,

jakx12.

Recommended Answers

All 4 Replies

You don't load the jar, you load the Driver class and include the jar on the classpath. See the Tutorial and the MySQL manual for MySQL specific examples

ok thanks, but were would i find the driver class?

The MySQL documentation shows you which class, at the very beginning of the page I linked to, and the tutorials (and the MySQL docs) show you how to use it. Read those.

Edit: Besides, why would you be loading a postgresql Driver (jar) when it is mysql (according to your title) that you want to connect to?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.