评论编辑 :我想我已经解决了原来的问题(无法找到驱动程序),现在我遇到了以下问题:
编辑2 :当我使用完整路径(url
)替换context.xml
中的url=jdbc:sqlite:C:/Dev/workspace/jerseyrestdemo/resources/greetings.sqlite
时,它可以正常工作。我现在怎样才能使这条路径相对??
编辑3 :我按照how to configure JNDI datasource for db connection in tomcat中的说明操作:1。删除项目特定的context.xml并将内容放入{{1} }。在那里我指定了名称空间tomcat7/conf/context.xml
name=jdbc/greetings.sqlite
在DAO课程中,我通过
获得连接 <Resource name="jdbc/greetings.sqlite"
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
driverClassName="org.sqlite.JDBC"
url="jdbc:sqlite:resources/greetings.sqlite"
factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory">
</Resource>
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource) envCtx.lookup("jdbc/greetings.sqlite");
但我仍然收到错误conn = ds.getConnection();
编辑4 :最后,我将数据库文件存储在catalina主页中
正如here所解释的org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (path to 'resources/greetings.sqlite': 'C:\Dev\eclipse\resources' does not exist)
。我想你不应该将你的db文件存储在web应用程序项目中
我的项目文件夹下的/${catalina.home}/databases/greetings.sqlite
中有一个SQLite数据库文件,但是当我在tomcat上运行我的webapp时,找不到db文件:
resources/greetings.sqlite
我已将jdbc jar复制到tomcat / lib目录中,并在java.sql.SQLException: path to 'resources/greetings.sqlite': 'C:\Dev\eclipse\resources' does not exist
at org.sqlite.core.CoreConnection.open(CoreConnection.java:192)
at org.sqlite.core.CoreConnection.<init>(CoreConnection.java:76)
at org.sqlite.jdbc3.JDBC3Connection.<init>(JDBC3Connection.java:25)
at org.sqlite.jdbc4.JDBC4Connection.<init>(JDBC4Connection.java:24)
at org.sqlite.SQLiteConnection.<init>(SQLiteConnection.java:45)
at org.sqlite.JDBC.createConnection(JDBC.java:114)
at org.sqlite.JDBC.connect(JDBC.java:88)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
at demo.rest.jersey.spring.dao.JdbcGreetingDAO.getRowCount(JdbcGreetingDAO.java:29)
at demo.rest.jersey.spring.GreetingService.randomGreeting(GreetingService.java:29)
at demo.rest.jersey.spring.GreetingService.greet(GreetingService.java:18)
at demo.rest.jersey.spring.SpringRequestResource.getHello(SpringRequestResource.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
和META-INF/context.xml
中添加了以下代码。
这是我放入web.xml的内容
WEB-INF/web.xml
我在<resource-ref>
<description>Greetings Database</description>
<res-ref-name>jdbc/greetings.sqlite</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
创建了一个文件。我假设WebContent/META-INF/context.xml
的{{1}}字段中的相对路径不是它应该如何。
url
我想避免的是将db文件放在tomcat子目录中,但只是让它成为项目的一部分,所以其他人不必费心将文件移动到他们的tomcat目录。
这是DAO类
Resource
DAO由<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/greetings.sqlite"
auth="Container"
type="javax.sql.DataSource"
driverClassName="org.sqlite.JDBC"
url="jdbc:sqlite:resources/greetings.sqlite"
factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory">
</Resource>
</Context>
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcGreetingDAO implements GreetingDAO
{
private Connection getConn() throws SQLException
{
Connection conn = null;
try
{
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/greetings.sqlite");
conn = ds.getConnection();
}
catch(NamingException ne)
{
System.out.println("Cannot get connection");
ne.printStackTrace();
}
return conn;
}
@Override
public void insert(String greeting) throws SQLException
{
Connection conn = getConn();
Statement st = conn.createStatement();
st.executeUpdate(String.format("INSERT INTO greetings (greeting) VALUES (%s)", greeting));
}
}
感谢您的帮助