DriverManager没有合适的驱动程序mysql

我们在查找为什么在使用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 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 lijstParams = new ArrayList(); connectie.voerQueryUit ("SELECT * FROM festivals", lijstParams); res = connectie.haalResultSetOp(); %> 

这发生在我们页面的头部。 第一次加载页面时,我们收到错误“没有为jdbc mysql找到合适的驱动程序”,但是当我们刷新时,我们查询的信息会正确显示。

因此,我们只在第一次加载时收到一条错误消息,之后,没有错误发生,一切正常。

有人可以帮助我们吗?

首先,您需要在连接之前加载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。