1

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

maffo
  • 1,393
  • 4
  • 18
  • 35
  • Do you have the jar file marked for export in the Build Path? – Kevin Bowersox Sep 15 '13 at 09:16
  • @KevinBowersox When expanding the JAR in the classpath view I see the following: Source attachment(none) Javadoc location (none) Native Library Location (None) Access Rules (No restrictions) – maffo Sep 15 '13 at 09:37
  • There must be some scope, as when I remove the JAR from the project, the Type `PreparedStatement` can't be resolved. – maffo Sep 15 '13 at 09:40
  • The Build Path is not used for assembling the WAR file. See http://stackoverflow.com/a/4631238/53897 – Thorbjørn Ravn Andersen Sep 15 '13 at 09:46

3 Answers3

1

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

Sanjay
  • 11
  • 2
0

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

CodeSamurai777
  • 3,285
  • 2
  • 24
  • 42
0

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?

herry
  • 1,708
  • 3
  • 17
  • 30