我正在尝试运行一个连接到MySQL数据库的程序,但是我收到错误“找不到合适的驱动程序”。
/usr/share/tomcat8/bin/setenv.sh
JAVA_HOME=/usr/lib/jvm/java-8-oracle
CLASSPATH=/opt/java/mysql-connector-java-5.1.40-bin.jar
我的代码的一部分:
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
...
...
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException ex) {
out.println("error: class not found <br>");
}
con = null;
try {
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mybase", "root", "123456");
} catch (SQLException sqlEx) {
out.println("Error message:" + sqlEx.getMessage());
out.println("error: connection to database <br>");
if (con != null) {
try { con.close(); } catch(SQLException se) { }
}
return;
}
当它运行时我有一个错误:
error: class not found
Error message:No suitable driver found for jdbc:mysql://localhost:3306/mybase error: connection to database
我想,tomcat8由于某种原因没有得到CLASSPATH变量。