IntelliJ 找不到驱动程序 class
JntelliJ can't find driver class
我使用 IntelliJ IDEA 14.0、Tomcat 服务器和 h2 数据库来创建简单的 Web 应用程序。
不幸的是,当我 运行 应用程序时,我收到异常消息
java.lang.ClassNotFoundException: org.h2.Driver
我将 h2 jar 文件存储在名称为 "db" 的文件夹中并设置了 "Add as library" 功能。
我这样连接到数据库:
public class DBConn {
private static final String DRIVER = "org.h2.Driver";
private static final String URL = "jdbc:h2:tcp://localhost/~/BigPicture";
private static final String USERNAME = "doncho";
private static final String PASS = "";
private static DBConn instance;
private static Connection conn;
private DBConn(){
}
public static DBConn getInstance(){
if(instance == null){
instance = new DBConn();
}
return instance;
}
public Connection getConnectivity(){
try {
Conn();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
private void Conn() throws SQLException{
if(conn == null){
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
System.out.println("No Driver Found");
e.printStackTrace();
}
DriverManager.getConnection(URL, USERNAME, PASS);
}
}
public void Disconnect(){
if(conn != null){
try {
conn.close();
conn = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
我在Servlet中调用数据库。
public class DBServlet extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Connection conn = DBConn.getInstance().getConnectivity();
System.out.println("It Work's");
DBConn.getInstance().Disconnect();
}
输出显示 "No Driver Found" 并抛出 java.lang.ClassNotFoundException: org.h2.Driver.
重要的是,当我在 Main
方法 IntelliJ fInd h2
驱动程序中调用 class DBConn
() 时,但 Tomcat 仍然可以't。
请帮忙,因为我是 IntelliJ 的新手,在 Eclipse 中这个应用程序工作正常,但我想在我的项目中使用 IntelliJ。
此致。
将驱动程序的 jar 文件放在 $TOMCAT_HOME/lib 或 yourapp/WEB-INF/lib
上
我使用 IntelliJ IDEA 14.0、Tomcat 服务器和 h2 数据库来创建简单的 Web 应用程序。 不幸的是,当我 运行 应用程序时,我收到异常消息
java.lang.ClassNotFoundException: org.h2.Driver
我将 h2 jar 文件存储在名称为 "db" 的文件夹中并设置了 "Add as library" 功能。 我这样连接到数据库:
public class DBConn {
private static final String DRIVER = "org.h2.Driver";
private static final String URL = "jdbc:h2:tcp://localhost/~/BigPicture";
private static final String USERNAME = "doncho";
private static final String PASS = "";
private static DBConn instance;
private static Connection conn;
private DBConn(){
}
public static DBConn getInstance(){
if(instance == null){
instance = new DBConn();
}
return instance;
}
public Connection getConnectivity(){
try {
Conn();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
private void Conn() throws SQLException{
if(conn == null){
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
System.out.println("No Driver Found");
e.printStackTrace();
}
DriverManager.getConnection(URL, USERNAME, PASS);
}
}
public void Disconnect(){
if(conn != null){
try {
conn.close();
conn = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
我在Servlet中调用数据库。
public class DBServlet extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Connection conn = DBConn.getInstance().getConnectivity();
System.out.println("It Work's");
DBConn.getInstance().Disconnect();
}
输出显示 "No Driver Found" 并抛出 java.lang.ClassNotFoundException: org.h2.Driver.
重要的是,当我在 Main
方法 IntelliJ fInd h2
驱动程序中调用 class DBConn
() 时,但 Tomcat 仍然可以't。
请帮忙,因为我是 IntelliJ 的新手,在 Eclipse 中这个应用程序工作正常,但我想在我的项目中使用 IntelliJ。
此致。
将驱动程序的 jar 文件放在 $TOMCAT_HOME/lib 或 yourapp/WEB-INF/lib
上