I am not smart
I am not smart

Reputation: 1441

Can't add JDBC driver to Library scope

I am currently trying to contact a MySQL server. My goal is to have a servlet that will show some table data when run. My servelt is running on glassfish 3.1.

Here is the simple doGet method:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    PrintWriter out = response.getWriter(); 
    out.print("<h1>select * from userName:<h1>");

    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/people", "root", "r00tpw");
        System.out.println("Connected");

        PreparedStatement statement = (PreparedStatement) conn.prepareStatement("select * from userName");
        ResultSet result = statement.executeQuery();

        while(result.next()) {
            out.print(result.getString(0));
        }

    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

I have got the correct JDBC driver. The problem I have is with my source code finding it. I have tried right clicking JRE System Library->Build Path->Configure Build Path and adding an external JAR. Rather than adding the JAR to Referenced Libraries it seems to be added outside of any library scope. This means that Class.forName("com.mysql.jdbc.Driver"); is always throwing a ClassNotFoundException.

I have also tried adding the JAR to my Run Configurations->Glassfish 3.1 at [pcname]->Classpath->User Entries with no luck.

I have also tried using MS SQL Server and their JDBC driver. The same thing happens.

I have attached an image of my project structure to help.

What my project explorer looks like after including in claspath

Upvotes: 1

Views: 1130

Answers (3)

herry
herry

Reputation: 1738

May be you are not added mysql driver to a server folder. Therefore, did you check your glassfish-resources.xml or JDBC connection in Glassfish admin console?

Upvotes: 0

Sanjay
Sanjay

Reputation: 11

Put the mysql-connector-java-5.1.26 to your WebContent--->WEB-INF-->lib.inside lib folder.

Upvotes: 1

CodeSamurai777
CodeSamurai777

Reputation: 3355

First of all after you install jdbc driver you need to configure it in glassfish. Create a JDBC resource and JDBC connection pool.

PS Did you add the jdbc JAR to glassfish server folder?

For glassfish too see your connector it must reside in \Glassfish3\domains\domain1\lib\databases

Upvotes: 0

Related Questions