2

We're having some trouble finding out why we are getting an error message when creating a connection with DriverManager.

Here is our code

package Databank;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.List;

public class Connectie_Databank
{
    //Eigenschappen databank
    private String connectieString = "";
    private Connection connectie = null;
    private PreparedStatement prepStatement = null;
    private Statement statement = null;
    private ResultSet inhoudQuery = null;

    //Inloggegevens PhpMyAdmin
    private String gebruikersnaam, wachtwoord;

    //Constructor met standaardinstellingen
    public Connectie_Databank()
    {
        this.connectieString = "jdbc:mysql://localhost/groep2_festivals";
        this.gebruikersnaam = "root";
        this.wachtwoord = "";
    }

    //Constructor met nieuwe data
    public Connectie_Databank(String connectionString, String gebruikersnaam, String wachtwoord)
    {
        this.connectieString = connectionString;
        this.gebruikersnaam = gebruikersnaam;
        this.wachtwoord = wachtwoord;
    }

    /**
     * Deze methode zorgt ervoor dat er verbinding gemaakt wordt me de databank
     */
    public void maakConnectie()
    {
        try
        {
            connectie = DriverManager.getConnection(connectieString, gebruikersnaam, wachtwoord);
        }
        catch(Exception e)
        {
            System.err.println("FOUTMELDING: " + e.getMessage());
        }
    }

    public void voerQueryUit(String query, List<String> parameters)
    {
        try
        {
            if(parameters.size() > 0)
            {
                //Reden preparedStatement: geen SQL-Injectie!
                prepStatement = connectie.prepareStatement(query);

                //Lijst met parameters uitlezen om de preparedStatement op te vullen
                for(int i=1; i<=parameters.size(); i++)
                {
                   prepStatement.setString(i, parameters.get(i-1));
                }
                inhoudQuery = prepStatement.executeQuery();
            }
            else
            {
                statement = connectie.createStatement();
                inhoudQuery = statement.executeQuery(query);
            }
        }
        catch(Exception e)
        {}
    }

    public ResultSet haalResultSetOp()
    {
        return inhoudQuery;
    }

    public void sluitConnectie()
    {
        //ConnectieString leegmaken en alle objecten die te maken hebben met de connectie sluiten
        try
        {
            connectieString = "";
            if(connectie != null)
            {
                connectie.close();
            }
            if(prepStatement != null)
            {
                prepStatement.close();
            }
            if(inhoudQuery != null)
            {
                inhoudQuery.close();
            }
        }
        catch(Exception e)
        {}
    }
}

We call our connection class from within a JSP page like this:

    <%
        Connectie_Databank connectie;
        ResultSet res;
        connectie  = new Connectie_Databank();

        connectie.maakConnectie ();

        List<String> lijstParams = new ArrayList<String>();
        connectie.voerQueryUit ("SELECT * FROM festivals", lijstParams);

        res  = connectie.haalResultSetOp();
    %>

This happens in the head of our page. The first time the page loads, we get an error "no suitable driver found for jdbc mysql" but when we refresh, the information we query is shown correctly.

So, we only get the one error message on first load, after that, no errors occur and everything works normally.

Can anyone help us?

Roman C
  • 49,761
  • 33
  • 66
  • 176
DerpyNerd
  • 4,743
  • 7
  • 41
  • 92

1 Answers1

2

First you need to load JDBC driver before connection

// Notice, do not import com.mysql.jdbc.*
// or you will have problems!

//Load Driver
try {
  // The newInstance() call is a work around for some
  // broken Java implementations
  Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception ex) {
  // handle the error
}

con=DriverManager.getConnection(connectieString);
System.out.println ("Database connection established");

make sure you have mysql-connector-java-5.x.x-bin.jar on the classpath when you run your application.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • 1
    Thanks a lot! This solved our problems. We've tried that before but must have made a mistake. We thought it wasn't needed anymore after java jdk6 :/ – DerpyNerd Apr 06 '13 at 15:06
  • 3
    @RobbieVercammen: it's not needed if the driver registers as "a service provider", but for that it needs to provide that information in the manifest file. Probably the MySQL driver doesn't do that. –  Apr 06 '13 at 15:23
  • The comment about `newInstance()` in the code is by the way wrong. It has utterly nothing to do with Java implementations, but more so with JDBC driver implementations. The current MySQL JDBC driver isn't one of them. See also http://stackoverflow.com/questions/2092659/what-is-difference-between-class-forname-and-class-forname-newinstance/2093236#2093236 – BalusC Apr 06 '13 at 16:22
  • @a_horse_with_no_name Recent MySQL Connector/J versions do include the information in `META-INF/services/java.sql.Driver`. – Mark Rotteveel Apr 07 '13 at 08:15