views:

93

answers:

5

I have these two classes in my database package:(DBManager and TaskManager class) and I create a new object in my main frame which is in the other package and I also imported the database package for my main frame and I call addBirth() on my object,and I want to insert these arguments in my table="birthsql" in MySQL but I found this exception and also was written "SERVER = NULL".

creating an object--> TaskManager tm = new TaskManager(); calling addBirth() method on my object-->, tm.addBirth(3, "Neda","Rahmani", "Mansour", "Sima","December","Tehran");

My TaskManager class:

public class TaskManager {
private int BirthID = 2;
Logger logger = Logger.getLogger(this.getClass().getName());
private Connection conn = DBManager.getConnection();

public int getID()
{
    return BirthID++;
}

public void addBirth(int BirthID, String name, String family, String fatherName, String motherName, String DateOfBirth, String PlaceOfBirth) {
    try {
        Statement stm = conn.createStatement();

        stm.executeUpdate("INSERT INTO birthsql (name," + "family," + "fatherName," + "motherName," + "DateOfBirth, " + "PlaceOfBirth)" + "VALUES (" + BirthID + ", '" + name + "', '" + family + "', '" + fatherName + "', '" + DateOfBirth + "', '" + PlaceOfBirth + "')");
    } catch (SQLException ex) {
        Logger.getLogger(TaskManager.class.getName()).log(Level.SEVERE, null, ex);
    }


}}

My DBManager class:

public class DBManager {

private static Logger log = Logger.getLogger(DBManager.class.getName());
private static Connection connection = null;
private final static String DB_URL = "jdbc:mysql://localhost:3306/assignment_2";
private final static String DB_USERID = "root";
private final static String DB_PASSWORD = "123";

public static Connection getConnection()
{
    if (connection == null)
    {
        try {
            /* Your code here */
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(DB_URL, DB_USERID, DB_PASSWORD);
        } catch (SQLException ex) {
            Logger.Dec 10, 2009 6:44:05 AM database.DBManager getConnection

        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    return connection;
}}

Stacktrace:

Dec 10, 2009 6:44:05 AM database.DBManager getConnection
SEVERE: null
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at database.DBManager.getConnection(DBManager.java:32)
    at database.TaskManager.<init>(TaskManager.java:21)
    at adminFrame.AdminFrame.<init>(AdminFrame.java:29)
    at adminFrame.AdminFrame$4.run(AdminFrame.java:239)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at database.TaskManager.addBirth(TaskManager.java:30)
    at adminFrame.AdminFrame.<init>(AdminFrame.java:46)
    at adminFrame.AdminFrame$4.run(AdminFrame.java:239)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:121
+1  A: 

Do you have the MySQL JDBC driver jar on your classpath?

Thilo
I have added it but I still have this exception.(with this stacktrace)
Johanna
You may have added them to the wrong place. The exception clearly states that the MySQL driver class is not found.
Thilo
You clearly don't have them in your classpath, Johanna, or you wouldn't be getting that Exception.
delfuego
how can i get that my driver jar is on my classpath??
Johanna
A: 

If you have the MySQL JARs, make sure they're in the classpath. If you don't, download them here.

Kaleb Brasee
+2  A: 

Just because your code compiles and can start running does not mean you have all the required stuff on your classpath. The code compiles because you're using the basic JDBC interfaces that are built into the Java language distribution, but when you run, JDBC tries to instantiate the MySQL specific connectors and can't (the Class.forName call). It looks like you've got a jar missing on your classpath. You can get one here:

http://dev.mysql.com/downloads/connector/j/5.1.html

Brent Nash
+2  A: 

I added Driver jar on my classpath

You thought you did it properly, but the JVM is telling you that you did not.

How did you do it? The right way is not a CLASSPATH environment variable. The JVM, IDEs and app servers all ignore it.

The right thing to do depends on how you're running your app.

If you're running on the command line, use the -cp option and provide the full path to the MySQL Connector-J JAR.

If you're using an IDE like IntelliJ or Eclipse, you'll have to know how to add the MySQL driver JAR to the build lib path.

If you're creating a web app, you should either put it in your app's WEB-INF/lib directory or, better yet, the /lib directory for your app server.

Most of all, believe the error message. You did it incorrectly. Figure out the right way.

UPDATE: Looks like a duplicate of 1878405.

duffymo
+1 - @Johanna needs to learn to read exception messages, to pay attention to the answers to his/her questions, and ultimately to start solving his/her own problems.
Stephen C
A: 

You can't write Java without knowing how to set CLASSPATH.

If you're using an IDE like IntelliJ or Eclipse, you'll have to add the JAR to your build library path.

If you're using an app server, you should put it in your WEB-INF/lib or, better yet, the /lib directory for your app server.

duffymo
I don't know if the /lib directory of the app server is preferable to WEB-INF/lib. It breaks the premise that a webapp should be self-contained, and it creates class visibility problems in some cases (a JDBC driver should be fine, though).
Thilo
I agree with your commend about being self-contained. Tomcat 6 recommends putting JDBC JARs in /lib now.
duffymo