我写了一个在Tomcat 7.0服务器上使用MYSQL的类。我将JDBC连接为外部JAR。当我运行我的类作为Java应用程序一切正常,我有我的连接和工作查询,但当我在服务器(Tomcat)上运行我的dinamic Web应用程序和脚本将进入我的类函数java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
来。< / p>
这是我的代码:
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;
}
}
这是使用该类
的功能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);
}
答案 0 :(得分:1)
您的jar文件需要可用于运行时(tomcat)。您需要将mysql驱动程序jar复制到tomcat lib文件夹中。