I wrote a class which working with MYSQL on Tomcat 7.0 server. I connected JDBC as external JAR. When i ran my class as Java Application all works good, i have my connection, and working queryes but when i run my dinamic web application on server(Tomcat) and script is going to my class function java.lang.ClassNotFoundException: com.mysql.jdbc.Driver is come.
Here is my code:
public class db {
public static ResultSet GetUsers() throws Exception
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/test","root","");
PreparedStatement statement = con.prepareStatement("select * from `person`");
ResultSet result = statement.executeQuery();
return result;
}
public Connection OpenConnection() throws Exception
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/test","root","");
return con;
}
}
This is function which use that class
private void Exist(String name, String password) throws Exception
{
db data = new db();
Connection con = data.OpenConnection();
PreparedStatement statement = con.prepareStatement("SELECT * FROM `person` WHERE `name`= "+name+
" AND password = " + password);
ResultSet result = statement.executeQuery();
System.out.println(result);
}
Your jar file need to available for Runtime (tomcat). You need to copy mysql driver jar into tomcat lib folder.