View Poll Results: JSP unable to access MySQL

Voters
2. You may not vote on this poll
  • error while fetching records from MySQL

    0 0%
  • Is it JSP/MySQL error..??

    1 50.00%
  • trouble acessing MySQL

    0 0%
  • JSP problem

    1 50.00%
Multiple Choice Poll.
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2003
    Location
    Bangalore
    Posts
    2

    Exclamation Unanswered: Trouble Connecting To MySQL from JSP

    This is the my JSP Coding to simple connect to database and retrieve the records in a table and display it. But it does seem to be able to connect to MySQL.

    Anyone who can help me solve this problem ASAP, their help will be appreciated.

    root cause for error:
    java.lang.ClassNotFoundException: org.gjt.mm.mysql.Driver

    ****** JSP Program********
    <%@ page language="java"%>
    <%@ page import="java.io.*"%>
    <%@ page import="java.net.*"%>
    <%@ page import="java.sql.*"%>
    <%@ page import="java.util.*"%>

    <%
    Connection conn;
    ResultSet rs;
    Statement st;
    PreparedStatement ps;
    %>

    <%
    try
    {
    Class.forName("org.gjt.mm.mysql.Driver").newInstan ce();
    conn = DriverManager.getConnection("jdbc:mysql://localhost/test","naaz","home");

    if(conn==null)
    {
    out.println("\n\nConnection not Established.....");
    }

    out.println("\nWe have a connection!");
    st=conn.createStatement();
    rs=st.executeQuery("Select * from test");
    ResultSetMetaData rsmd=rs.getMetaData();
    int n=rsmd.getColumnCount();
    for(int i=1;i<=n;i++)
    {
    out.println(rsmd.getColumnLabel(i)+"\t");
    }
    out.println("\n");
    boolean bb=rs.next();
    while(bb)
    {
    for(int i=1;i<=n;i++)
    {
    out.print(rs.getString(i)+"\t");
    }
    out.println();
    bb=rs.next();
    }
    st.close();
    conn.close();
    }

    catch (SQLException e)
    {
    out.println("\nDriver can't get a connection.");
    out.println("\nSQLExecption: " + e.getMessage());
    out.println("\nSQLState: " + e.getSQLState());
    }
    %>

    ****** JSP Program Ends********

  2. #2
    Join Date
    Apr 2003
    Posts
    6
    Without looking at your code, the error indicates that the JDBC-driver for MySQL cannot be found (the error is generated by the Class.forName() statement).

    Put the jar-file containing the driver in your classpath, e.g. the lib-directory of your servletengine. I believe most engines uses <base>/lib for classes that should be available to all applications. Otherwise you can always put it in WEB-INF/lib, to have it accessable only by your app.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •