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". Knowing that I test the method in java program and that's work fine. I have one connection class in ejb project(session bean)there is the code here.
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
I have fixed the problem:
Added SQLite jar file in this path:
D:\\jboss-as-7.1.1.Final\\modules\\javax\\activation\\api\\main
In JBoss Server Runtime > Deployments > Manage Deployments added SQLite jar file an then enable it.
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
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
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:
MyConnectionTest
test class and run it as a JUnit test.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.