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;
}
}