Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I know that there have been so many questions on this theme, but I feel the situation I am facing is really different. The question: I have created an RCP project, in which I need to connect to my Mysql database. Before I wrote my program, I wrote a test program to test the configuration needed, and I connected to my Mysql database successfully. The test program can be found in Appendix 1. Then I began to write my program in the same project, but when I called the function in it, the exception java.lang.ClassNotFoundException: com.mysql.jdbc.Driver happened. It really disturbed me. The program can be seen in Appendix 2.

Appendix 1 Sql.java

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Sql
{

    private static final String MYSQL_DRIVER = "com.mysql.jdbc.Driver";
    private static final String URL ="jdbc:mysql://localhost/manager?useUnicode=true&characterEncoding=utf8";
    private static final String USERNAME = "root"; 
    private static final String PASSWORD = "yhl0821";

    public static void main(String[] args)
    {
        Connection con = null;
        Statement sm = null;
        ResultSet rs=null;

        try
        {
            Class.forName(MYSQL_DRIVER);
            con =DriverManager.getConnection(URL, USERNAME, PASSWORD);
            System.out.println("Successed to connect the mysql!");
        } catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        } catch (SQLException e)
        {
            e.printStackTrace();
        }       
    }
}

Appendix 2 EventEntity.java

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class EventEntity
{
    private static final String MYSQL_DRIVER = "com.mysql.jdbc.Driver";
    private static final String URL = "jdbc:mysql://localhost/manager?useUnicode=true&characterEncoding=utf8";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "yhl0821";
public static List<Object> getEventEntity()
    {
        List<Object> list = null;//
        Connection con = null;
        Statement sm = null;
        ResultSet rs = null;

        try
        {
            Class.forName(MYSQL_DRIVER); //this is the setence creating exception
            con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
            System.out.println("Successed to connect the mysql!");
            sm = con.createStatement();
            rs = sm.executeQuery("select * from systemevent ");//
        } 
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        } 
        catch (SQLException e)
        {
            e.printStackTrace();
        } 

        return list;

    }
}
share|improve this question
    
possible duplicate of Eclipse rcp - how to load jdbc driver? –  Robin Green Dec 29 '13 at 14:44

2 Answers 2

The class com.mysql.jdbc.Driver is not on your CLASSPATH when you run the program in Appendix 2.

share|improve this answer
    
but how can i put the com.mysql.jdbc.Driver on the CLASSPATH when i run the program in Appendix 2. Is there any different from the program in Appendix 1 Thanks! –  user3143652 Dec 29 '13 at 11:33
    
That depends on how you are running the program in Appendix 2. For example, from a terminal, from Eclipse, from Netbeans, etc. –  Robin Green Dec 29 '13 at 11:42
    
probably you need to include the jar file, containing this class to class path or build library path ( mostly like mysql-connector-java***.jar) –  Devaraj Mahesan Dec 29 '13 at 11:44
    
Thans for your attention. I run the two program both with Eclipse.EventEntity.java is one file of my RCP project,i use it when i debug the rcp project as "Eclipse Application",and i run the "Sql.java" as a java Application to test whether it can connect to my database.Beg your advices .Thanks! –  user3143652 Dec 29 '13 at 14:37
    
yes ,I am sure i have done what you adviced ,but the problem still exists.haha... –  user3143652 Dec 29 '13 at 14:44

Prior to now,i did not understand the meaning of Classpath in the RCP project run from Eclipse. if you want to write and run a RCP project with Eclipse ,you should not only put the *.jars and other files you need in build path(they are needed when the program is compiled),but also configure Classpath in "Runtime" .you can configure it like this:1、dobble click the plugin.xml.2、choose Runtime and find the Classpath.3、click ‘Add’ to put the files what the RCP project needs when you run it.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.