Jump to content

Connecting to MySQL with Java

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
Coldhearth

Coldhearth

    Learning Programmer

  • Members
  • PipPipPip
  • 88 posts
Hi,

I'm looking for a way to connect to mysql with my java program...
But when I use the Class.forName("com.mysql.jdbc.Driver"); driver it keeps giving an error when I try to run the program.

This is my code for my Database connection class:
package controller;



import java.sql.*;


import javax.swing.JOptionPane;


public class DatabaseConnectie {

	

	private Connection connection;

	

	private static DatabaseConnectie connectie;

	

	public static DatabaseConnectie getInstance(){

		if(connectie == null)

			connectie = new DatabaseConnectie();

		return connectie;

		

	}

	

	

	public DatabaseConnectie(){

//	database connectie maken

	try {

		Class.forName("com.mysql.jdbc.Driver");

		

		connection = DriverManager.getConnection("jdbc:mysql://localhost:8889/testDB", "root", "root");

	}catch(SQLException sqlException ){

		JOptionPane.showMessageDialog(null, sqlException.getMessage()

				,"Database Error",JOptionPane.ERROR_MESSAGE);

		

		System.exit(1);

	}

	

	catch (ClassNotFoundException classNotFound) {

		// TODO Auto-generated catch block

		JOptionPane.showMessageDialog(null,classNotFound.getMessage(),

				"Driver not Found",JOptionPane.ERROR_MESSAGE);

		

		System.exit(1);

	}

	}

    

	public Connection getConnection(){

		return connection;

	}

	public void closeConnection(){

		try{

			connection.close();

			connectie = null;

		}

		catch( SQLException sqlException){

			JOptionPane.showMessageDialog(null,

					sqlException.getMessage(),"Database error",

					JOptionPane.ERROR_MESSAGE);

			System.exit(1);

		}

	}



	public ResultSet voerSelectQueryUit(String SQL) {

		Statement statement;

		ResultSet result = null;

		try {

			statement = connection.createStatement();

			result = statement.executeQuery(SQL);

		} catch (SQLException e) {

			e.printStackTrace();

		}

		return result;

	}

	

	public void voerUpdateQueryUit(String SQL) {

		Statement statement = null;

		try {

			statement = connection.createStatement();

			statement.executeUpdate(SQL);

		} catch (SQLException e) {

			e.printStackTrace();

		}

	}


	

}


Can you guys help me out here?
Thx, Coldhearth

#2
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts
Never used MySQL, but I have tried out javas own db, so can't give any help :(
Posted Image

#3
Stu_328

Stu_328

    Learning Programmer

  • Members
  • PipPipPip
  • 92 posts
Whats yoru error?

#4
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts

Stu_328 said:

Whats yoru error?

What is your problem ?

Fix'd it for you also are you sleepy :D?
Posted Image

#5
Arkie

Arkie

    Learning Programmer

  • Members
  • PipPipPip
  • 92 posts
If you still need help, take a look at my tutorial that i wrote some time ago: http://www.engineeri...avaTutorial.pdf