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


Sign In
Create Account


Back to top










