DriverManager没有合适的驱动程序mysql [英] DriverManager no suitable driver mysql

查看:117
本文介绍了DriverManager没有合适的驱动程序mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们很难找出为什么在与DriverManager建立连接时收到错误消息的原因.

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

这是我们的代码

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)
        {}
    }
}

我们在这样的JSP页面中调用连接类:

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();
    %>

这发生在我们页面的顶部.第一次加载页面时,出现错误找不到适合jdbc mysql的驱动程序",但刷新后,查询信息将正确显示.

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.

有人可以帮助我们吗?

推荐答案

首先,您需要在连接之前加载JDBC驱动程序

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");

在运行应用程序时,请确保类路径上具有mysql-connector-java-5.x.x-bin.jar.

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

这篇关于DriverManager没有合适的驱动程序mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆