简体   繁体   English

错误[stderr](EJB缺省-8)java.lang.ClassNotFoundException:org.sqlite.JDBC

[英]ERROR [stderr] (EJB default - 8) java.lang.ClassNotFoundException: org.sqlite.JDBC

I'm trying to make a connection with sqlite database in j2ee project, but i keep getting this error "ERROR [stderr] (EJB default -8)java.lang.ClassNotFoundException: org.sqlite.JDBC". 我正在尝试与j2ee项目中的sqlite数据库建立连接,但我不断收到此错误“ ERROR [stderr](EJB默认为-8)java.lang.ClassNotFoundException:org.sqlite.JDBC”。 Knowing that I test the method in java program and that's work fine. 知道我在Java程序中测试了方法,就可以了。 I have one connection class in ejb project(session bean)there is the code here. 我在ejb项目(会话bean)中有一个连接类,这里有代码。

public Connection getConnection(){
    Connection connection= null;


    try {
        Class.forName("org.sqlite.JDBC");
        String dataFolder = System.getProperty("user.home") + "\\AppData\\Local";
        String b = System.getProperty("user.home") ;



        String file = b + "\\Desktop\\file";
        connection = DriverManager.getConnection("jdbc:sqlite:"+ file );
       System.out.println("Connection completed.");
    } catch (Exception e) {
        e.printStackTrace();
    }

    return connection;
 }

then I test it with junit test in java project but everytime i get the same error :java.Lang.ClassNotFoundException: org.sqlite.JDBC PS: I have incuded sqlite.jar like this (right click on the project->configure build path-> library-> add external jar) but always facing the same error 然后我在Java项目中用junit test对其进行测试,但是每次遇到相同的错误时:java.Lang.ClassNotFoundException:org.sqlite.JDBC PS:我像这样引入了sqlite.jar(右键单击project-> configure build path ->库->添加外部jar),但始终面临相同的错误

I have fixed the problem: 我已经解决了这个问题:

  1. Added SQLite jar file in this path: 在此路径中添加了SQLite jar文件:

    D:\\jboss-as-7.1.1.Final\\modules\\javax\\activation\\api\\main D:\\ jboss-as-7.1.1.Final \\ modules \\ javax \\ activation \\ api \\ main

  2. In JBoss Server Runtime > Deployments > Manage Deployments added SQLite jar file an then enable it. JBoss Server运行时 > 部署 > 管理部署中添加了SQLite jar文件,然后将其启用。

Assuming that you have the following project structure: 假设您具有以下项目结构:

src/main/java
     |
      - MyConnection.java
src/test/java
     |
      - MyConnectionTest.java

With the following elements: 具有以下元素:

Base class MyConnection 基类MyConnection

public class MyConnection {

    public Connection getConnection() {
        Connection connection = null;

        try {
            Class.forName("org.sqlite.JDBC");
            connection = DriverManager.getConnection("jdbc:sqlite:");
            System.out.println("Connection completed.");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return connection;
    }
}

Test class MyConnectionTest 测试类MyConnectionTest

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.sql.Connection;
import java.sql.SQLException;

import org.junit.Test;

public class MyConnectionTest {

    @Test
    public void isValid() throws SQLException {
        MyConnection myConn = new MyConnection();
        Connection conn = myConn.getConnection();
        assertTrue(conn.isValid(0));
        conn.close();
        assertFalse(conn.isValid(0));
    }
}

In your Eclipse project you just need to do the following: 在Eclipse项目中,您只需执行以下操作:

  1. Right click on your project; 右键单击您的项目;
  2. Navigate to Build Path and select the option Configure Build Path... ; 导航至构建路径,然后选择选项配置构建路径...
  3. On your right side menu, press Add Library , select JUnit (JUnit4) and click Finish ; 在右侧菜单上,按Add Library ,选择JUnit (JUnit4)并单击Finish
  4. Finally, on the Configure Build Path... menu, select the option Add External JARs... , browse to the location where your SQLite JDBC jar file is, select it and click on Apply . 最后,在“ 配置构建路径...”菜单上,选择“ 添加外部JAR ... ”选项,浏览至SQLite JDBC jar文件所在的位置,选择它,然后单击“ 应用”
  5. Go to your MyConnectionTest test class and run it as a JUnit test. 转到MyConnectionTest测试类,然后将其作为JUnit测试运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM