小编典典

DriverManager没有合适的驱动程序mysql

jsp

我们很难找出为什么在与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页面中调用连接类,如下所示:

    <%
        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的驱动程序”,但刷新后,查询信息将正确显示。

因此,我们在第一次加载时仅收到一条错误消息,此后,不会发生任何错误,并且一切正常。

谁能帮助我们?


阅读 368

收藏
2020-06-10

共1个答案

小编典典

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

// 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.xx-bin.jar。

2020-06-10