tags:

views:

1200

answers:

2

This Code is working fine with simple application so the drivers are fine. so why the connection object is not able to initialise with drivers.

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.sql.Statement;

import com.opensymphony.xwork2.ActionSupport;

public class Insert extends ActionSupport {

public String execute() throws Exception, SQLException {

 String sql = "";

 String url = "jdbc:mysql://localhost:3306/test";

 //String dbName = "test";

 String driverName = "org.gjt.mm.mysql.Driver";

 String userName = "root";

 String password = "root";

 Connection con=null;

 Statement stmt=null;

 try {

  Class.forName(driverName).newInstance();

  con = DriverManager.getConnection(url, userName, password);

  stmt = con.createStatement();

 } catch (Exception e) {

  System.out.println(e.getMessage());

 }

}

A: 

What does the exception say? A NullPointerException on createStatement?

In any case, has the class been loaded; is the class on the classpath (i.e. the MySQL jar on the classpath?)

Check the object returned from Class.forName() to check whether the class has been found.

After your comment it is clear that you have a classpath problem. The mysql jar is not on the classpath. I assume you are talking about a web app deliverable (war file), as changing the build path in eclipse is trivial.

In a web application deployed in, for example, tomcat you can look into <webapp-name>/WEB-INF/lib. The file WEB-INF/lib/mysql-connector-java-5.0.5.jar, or something similar, should be there.

If you have a war file (not yet deployed) you can extract it using the command line tool "jar", or get a file listing from it. If you do (on a command line) jar tf | grep mysql the jarfile should be visible. If you use windows; WinRAR (and probably WinZip) can also open warfiles. In WEB-INF/lib a MySQL jar should be visible.

If you use maven to build your web app; don't forget to add a dependency to the mysql jar. If you use ant to build; don't forget to copy the mysql jar file into WEB-INF/lib before creating the war file.

Please note that currently the recommended driver to ask for is 'com.mysql.driver.Driver', and not 'org.gjt.mm.mysql.Driver'. You can try to load that Driver class in stead of the older driver; further; check whether this driver is actually in the mysql jar (jar tf mysq.jar | grep Driver or so). If the Driver is in the mysql jar and the mysql jar is on the classpath of the webapp (in WEB-INF/lib) and there is only one mysql jar there (version conflicts are no fun), and it still does not work I really don't know what could be wrong. I think I'd download the driver jar again from MySQL and try again.

extraneon
Class.forName() is returning null. class has been loaded. how to work out this? Thanks in Advance
Jugal
eventhough there is a mysql connector jar file in WEB-INF/lib it is throwing an class not found exception of com.mysql.jdbc.Driver. i am using ant to build and also copied jar file before creating war file.
Jugal
Thank You.The problem is solved and the error was the extension of mysql jar file.
Jugal
A: 

I am trying to connect to an oracle database using struts 2 action class, but everytime I run the program, I get this class not found exception(for the Class.forName(driver)). An action class has no main() method...is this the cause of the issue (The connection with the database occurs with no difficulty or error when I use a normal java program - with main and stuffs)...Thank you.

zeeman