java连接sqlite数据库 相对路径怎么写,sqlite jdbc Eclipse数据库的相对路径

I'm trying to use Sqlite, jdbc under Eclipse Luna & Windows 7.

Everything works fine when I use absolute path to the Sqlite database but when relative path used I get this error:

java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database.

I spent some time Googling this problem and the answer is: Yes you can use relative path with jdbc connection. However it does not work for me.

My code:

package PortiaMoxy;

import static net.mindview.util.Print.*;

String inPath; // incoming file

String outPath; // converted file from incoming file

public File() {

// Connect to Sqlite db

Connection c = null;

Statement stmt = null;

ResultSet rs = null;

try {

Class.forName("org.sqlite.JDBC");

//

// This connection works fine

//

//c = DriverManager.getConnection("jdbc:sqlite:/JavaProjects/workspace/Polymorphism/sources/PortiaMoxy/moxyimport.sqlite");

//

// This connection doesn't work. ???

//

c = DriverManager.getConnection("jdbc:sqlite:moxyimport.sqlite");

c.setAutoCommit(false);

print("File(): Opened database successfully.");

stmt = c.createStatement();

String query = "select ID \"id\", VALUE \"value\"";

query += "from infiles;";

rs = stmt.executeQuery(query);

if (!rs.isBeforeFirst() ) {

System.out.println("No data");

}

while(rs.next()) {

String id = rs.getString(1);

String value = rs.getString(2);

print(" ID = " + id + " Value = " + value);

} // end of while

rs.close();

stmt.close();

c.close();

} // end of try

catch (SQLException ex) {

print(ex.getClass().getName() + ": " + ex.getMessage());

System.exit(0);

}

catch (Exception e ) {

print(e.getClass().getName() + ": " + e.getMessage());

System.exit(0);

}

} // end of class

public int convert() {

print(what());

print("Generic convert");

return 0;

}

public String what() {

return "File";

}

}

解决方案

Put your database within the following folder and try again with relative path:

/JavaProjects/workspace/Polymorphism

This should work.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值