Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

JDBC: ClassNotFoundException  Class.forName()

Posted on 2004-08-03
7
Medium Priority
?
2,541 Views
Last Modified: 2007-10-27
When trying to access my database I get the following error message:

INFO: JK2: ajp13 listening on /0.0.0.0:8010
Aug 3, 2004 9:07:06 AM org.apache.jk.server.JkMain start
INFO: Jk running ID=1 time=1/43  config=null
Aug 3, 2004 9:07:07 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 3733 ms
java.lang.ClassNotFoundException: usr/local/JDBCmySQL/mysql*/mysql/jdbc/Driver
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:219)
        at rtpm.EAKServlets.DBHandler.doPost(DBHandler.java:36)

Here is my database handler:

package rtpm.EAKServlets;

import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import rtpmEAKBeans.FormBean;

public class DBHandler extends HttpServlet {

    public void doPost (HttpServletRequest request,
      HttpServletResponse response) {
      try { // OBTAIN A DATABASE CONNECTION
         
          // Step 1: Load the JDBC driver          
          String driverName = "usr.local.JDBCmySQL.mysql*.mysql.jdbc.Driver";
          Class.forName(driverName, true, this.getClass().getClassLoader()).newInstance();
          // Class.forName("driverName", true, this.getClass().getClassLoader());
          Properties p = new Properties();
          p.put("user", "rmwijage");
          p.put("password", "NahumCh1");
                 
          // Step 2: Establish A connection to the database          
          String URL = "jdbc:mysql:///localhost/rtpmKenyaDB";
          Connection conn = null;  // conn is a Connection object
          conn = DriverManager.getConnection(URL,p);
                         
          String sql = "INSERT INTO rtpm_user(firstname, surname, " +
          "username, password1, password2, email, " +
          "sdp_code) VALUES (?, ?, ?, ?, ?, ?, ?)";
          PreparedStatement ps = conn.prepareStatement(sql);
         
          // Retreave the bean properties to be inserted into table rtpm_user
          FormBean f = (FormBean) request.getAttribute("formHandler");
         
          // Set the parameters in the prepared statement
          ps.setString(1, f.getFirstName());
          ps.setString(2, f.getLastName());
          ps.setString(3, f.getUserName());
          ps.setString(4, f.getPassword1());
          ps.setString(5, f.getPassword2());
          ps.setString(6, f.getEmail());
          ps.setString(7, f.getSdp());
         
          // Insert the row in the table
          ps.executeUpdate();

          // Close the connection
          ps.close();
          conn.close();    
      }
      catch (SQLException e) {
          // handle any errors
          System.out.println("SQLException: " + e.getMessage());
          System.out.println("SQLState: " + e.getSQLState());
          System.out.println("VendorError: " + e.getErrorCode());
          }
      catch (Exception e) {
      e.printStackTrace();
      }
     
      // Send the request back to success.jsp  
      try {
          getServletConfig().getServletContext().
            getRequestDispatcher("/success.jsp").
             forward(request, response);
      }
      catch (Exception e){}
     
   }
}


0
Comment
Question by:Rrugemalira
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
7 Comments
 
LVL 11

Assisted Solution

by:sompol_kiatkamolchai
sompol_kiatkamolchai earned 2000 total points
ID: 11706928
usr.local.JDBCmySQL.mysql*.mysql.jdbc.Driver

This may not a correct driver class.

You can get driver form

http://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-3.0.14-production.zip/from/http://gd.tuwien.ac.at/db/mysql/
0
 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
ID: 11707033
after you d/l mysql-connector-java-3.0.14-production.zip then extract the file mysql-connector-java-3.0.14-production-bin.jar to any directory in your hard drive then include that jar file into classpath.

then you can use this code to get a connection to database

import java.sql.*;

public class DatabaseConnection {
  public DatabaseConnection() {
  }

  public static Connection getConnection() {
    Connection con = null;
    try {
      Class.forName("com.mysql.jdbc.Driver");
      con = DriverManager.getConnection("jdbc:mysql:///localhost/rtpmKenyaDB", "rmwijage", "NahumCh1");
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
    return con;
  }
}
0
 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
ID: 11707070
for database url it should be

"jdbc:mysql://localhost/rtpmKenyaDB"
or
"jdbc:mysql://localhost/rtpmKenyaDB?user=rmwijage&password=NahumCh1"
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
ID: 11707075
for more information, please read REAEME.txt in that zip file.

0
 
LVL 11

Accepted Solution

by:
sompol_kiatkamolchai earned 2000 total points
ID: 11707125
Or try this. if you want to use your code. and open connection in jsp file.

package rtpm.EAKServlets;

import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import com.mysql.jdbc.Driver;

import rtpmEAKBeans.FormBean;

public class DBHandler extends HttpServlet {

    public void doPost (HttpServletRequest request,
      HttpServletResponse response) {
      try { // OBTAIN A DATABASE CONNECTION
         
          // Step 1: Load the JDBC driver          
          Class.forName("com.mysql.jdbc.Driver");
                 
          // Step 2: Establish A connection to the database          
           Connection conn = DriverManager.getConnection("jdbc:mysql:///localhost/rtpmKenyaDB", "rmwijage", "NahumCh1");
                         
          String sql = "INSERT INTO rtpm_user(firstname, surname, " +
          "username, password1, password2, email, " +
          "sdp_code) VALUES (?, ?, ?, ?, ?, ?, ?)";
          PreparedStatement ps = conn.prepareStatement(sql);
         
          // Retreave the bean properties to be inserted into table rtpm_user
          FormBean f = (FormBean) request.getAttribute("formHandler");
         
          // Set the parameters in the prepared statement
          ps.setString(1, f.getFirstName());
          ps.setString(2, f.getLastName());
          ps.setString(3, f.getUserName());
          ps.setString(4, f.getPassword1());
          ps.setString(5, f.getPassword2());
          ps.setString(6, f.getEmail());
          ps.setString(7, f.getSdp());
         
          // Insert the row in the table
          ps.executeUpdate();

          // Close the connection
          ps.close();
          conn.close();    
      }
      catch (SQLException e) {
          // handle any errors
          System.out.println("SQLException: " + e.getMessage());
          System.out.println("SQLState: " + e.getSQLState());
          System.out.println("VendorError: " + e.getErrorCode());
          }
      catch (Exception e) {
      e.printStackTrace();
      }
     
      // Send the request back to success.jsp  
      try {
          getServletConfig().getServletContext().
            getRequestDispatcher("/success.jsp").
             forward(request, response);
      }
      catch (Exception e){}
     
   }
}
0
 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
ID: 11707151
sorry. replace

          // Step 2: Establish A connection to the database          
           Connection conn = DriverManager.getConnection("jdbc:mysql:///localhost/rtpmKenyaDB", "rmwijage", "NahumCh1");

with this

          // Step 2: Establish A connection to the database          
           Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/rtpmKenyaDB", "rmwijage", "NahumCh1");

or this if above does not work

          // Step 2: Establish A connection to the database          
           Connection conn = DriverManager.getConnection"jdbc:mysql://localhost/rtpmKenyaDB?user=rmwijage&password=NahumCh1", "rmwijage", "NahumCh1");

0
 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
ID: 11707170
Hope all my comments would help you. :)
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes Administrators rights are not enough. These cases call for the SYSTEM account. The process in this article outlines the steps required to execute commands using the SYSTEM account.
With so many activities to perform, Exchange administrators are always busy in organizations. If everything, including Exchange Servers, Outlook clients, and Office 365 accounts work without any issues, they can sit and relax. But unfortunately, it…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question