The solutions I found for this error were "add the jar file via build path" and "put the jar file containing the driver in WEB-INF/lib" (and similar formulations) but I already tried the two of them.
Back to my problem. Here is my Code:
jsp-file
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="db.DBAccess" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="dba" class="db.DBAccess"></jsp:useBean>
<jsp:setProperty name="dba" property="selection" value="s" />
</body>
</html>
javabean
package db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBAccess {
public DBAccess() {
}
private String selection = "";
public void setSelection(String s) {
final String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/test";
String user = "user1";
String pswd = "user1pswd";
Connection con = null;
Statement stmt = null;
try {
// initialize connection
Class.forName(driver);
con = DriverManager.getConnection(url, user, pswd);
stmt = con.createStatement();
// execute select
ResultSet results = stmt.executeQuery("SELECT * FROM t");
results.next();
selection = results.getString(1);
// close connection
stmt.close();
con.close();
} catch (ClassNotFoundException cnfe) {
System.out.println(cnfe.toString());
} catch (SQLException e) {
System.out.println(e.toString());
}
}
}
The content of t is one column with the label "string" and the content "hello world"
Why am I getting this exception even though I linked the jar file correctly?
Class.forName
, just usingDriverManager
as documentation say here: dev.mysql.com/doc/refman/5.0/en/… – kordirko Oct 27 at 9:38Class.forName
line, I get the following error message:java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/test
– elementzero23 Oct 27 at 10:47