我在 Windows 7 中使用 eclipse 进行 JAVA 开发,并将我的项目放在 D:\workspace 中。
下面的代码尝试连接到SQLite数据库,虽然jdbc地址是jdbc:sqlite:sample.db,JAVA正在寻找sample.db的位置在哪里?
public class Sample{
public static void main(String[] args) throws ClassNotFoundException{
Class.forName("org.sqlite.JDBC");
Connection connection = null;
try{
connection = DriverManager.getConnection("jdbc:sqlite:sample.db");
System.out.println("I got connection.");
}catch(SQLException e){
System.out.println(e.getMessage());
}
}
}
Run Code Online (Sandbox Code Playgroud)
我看到有使用绝对路径的示例,但我想知道在使用相对路径时将数据库文件放在哪里。
另外,如果我将类放在某个包中,文件位置是否会有所不同?
我有一些功能与数据库一起工作.我在这里设置了一个try/catch错误处理,并显示一条消息,它工作正常.
现在,调用此删除函数的类需要知道是否存在错误.在我的情况下:如果成功则刷新GUI,如果失败则无需执行操作(因为已经显示消息消息对话框).
我想出了一个在这个函数中返回布尔值的想法.
public static Boolean delete(int id){
String id2 = Integer.toString(id);
try {
String sql =
"DELETE FROM toDoItem " +
"WHERE id = ?;";
String[] values = {id2};
SQLiteConnection.start();
SQLiteConnection.updateWithPara(sql, values);
} catch (SQLException e) {
Main.getGui().alert("Fail when doing delete in DataBase.");
System.out.println("Exception : "+ e.getMessage());
return false;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
不知道这是好还是坏,请告诉我.
编辑:
以下是我如何使用的更多细节:
假设上面的代码在Class A中,在B类中:
public boolean deleteItem(int id){
int i = index.get(id);
if(theList[i].delete()){ //<---- here is the function from Class A
theList[i] = null;
index.remove(id); …Run Code Online (Sandbox Code Playgroud)