Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
 
Which jar did you add? –  Sotirios Delimanolis Oct 26 at 17:05
 
Try without Class.forName, just using DriverManager as documentation say here: dev.mysql.com/doc/refman/5.0/en/… –  kordirko Oct 27 at 9:38
 
I downloaded and installed "mysql java connector" from here: dev.mysql.com/downloads/mirror.php?id=414247 Then I added the jar-file located in the installation folder of the mysql connector. I even tried to copy the jar-file into WEB-INF/lib and add this as external jar-file. It didn't work. –  elementzero23 Oct 27 at 10:46
 
When removing the Class.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
 
Maybe I should also say that I tried the whole thing in a "normal" java class before and there it worked. Now I want to display my selection from the database on a website (through jsp) and I get this error message... –  elementzero23 Oct 27 at 10:52
add comment

1 Answer

Did you insert your jar file to lib folder of Webinf?

share|improve this answer
 
Are you using tomcat? –  Govan Nov 4 at 8:51
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.