gpt4 book ai didi

Why is the SQLite database not updating inside the jar file?(为什么SQLite数据库没有在JAR文件内部进行更新?)

转载 作者:bug小助手 更新时间:2023-10-25 19:27:16 25 4
gpt4 key购买 nike



I'm encountering an issue with my Java application developed in NetBeans. The application uses an SQLite database, and I've noticed that changes made to the database persist when I run the program from within NetBeans. However, when I build the JAR file and run it separately, the database changes are not reflected.

我在NetBeans中开发的Java应用程序遇到了一个问题。这个应用程序使用一个SQLite数据库,我注意到当我从NetBeans中运行该程序时,对数据库所做的更改仍然有效。但是,当我构建JAR文件并单独运行它时,数据库更改不会反映出来。


Here are some key details:

以下是一些关键细节:


The database file (customerDB.db) is stored in the "databases" folder, and the folder is added as a resource in my NetBeans project.
I'm confident that I'm modifying the correct database file in the "databases" folder.

数据库文件(CustomerDB.db)存储在“数据库”文件夹中,该文件夹作为资源添加到我的NetBeans项目中。我确信我修改的是“数据库”文件夹中的正确数据库文件。


NetBeans seems to copy the database file to a temporary location during the build process, which is used when running the JAR file.
I would appreciate any insights into why the JAR file's database isn't updating as expected and how I can ensure that changes persist when running the JAR file.

NetBeans似乎会在构建过程中将数据库文件复制到一个临时位置,该位置在运行MySQL文件时使用。如果您能提供一些见解,让我了解为什么这个文件的数据库没有按预期进行更新,以及我如何确保在运行这个文件时更改仍然存在,我将不胜感激。


Here's the program for connecting to the SQLite Database, and updating it:

以下是连接到SQLite数据库并对其进行更新的程序:



public class SQLiteDatabase {

private Connection con;
private final String tableName;

public SQLiteDatabase() throws SQLException, ClassNotFoundException, IOException {
tableName = "";
Class.forName("org.sqlite.JDBC");
Path dbFile = Files.createTempFile(null, ".db");

con = DriverManager.getConnection("jdbc:sqlite::resource:customerDB.db");

}

public SQLiteDatabase(String tableName, String tableFields) throws SQLException, ClassNotFoundException, IOException {
this.tableName = tableName;
Class.forName("org.sqlite.JDBC");
String query = String.format("CREATE TABLE IF NOT EXISTS %s %s", tableName, tableFields);

con = DriverManager.getConnection("jdbc:sqlite::resource:customerDB.db");

Statement st = con.createStatement();
st.executeUpdate(query);
}

public void showTables() throws SQLException {
try (ResultSet rs = con.getMetaData().getTables(null, null, null, null)) {
while (rs.next()) {
System.out.println(rs.getString("TABLE_NAME"));
}
}
}

public void insertData(int id, String name, String username, String password) throws SQLException {
String query = String.format("INSERT INTO %s VALUES(?,?,?,?)", tableName);
PreparedStatement p = con.prepareStatement(query);
p.setInt(1, id);
p.setString(2, name);
p.setString(3, username);
p.setString(4, password);

int count = p.executeUpdate();

System.out.println(String.format("%d row(s) added sucessfully", count));

p.close();
}

public void readData() throws SQLException {
String query = String.format("SELECT * FROM %s", tableName);
Formatter formatter = new Formatter();

try (Statement st = con.createStatement();
ResultSet rs = st.executeQuery(query)) {

formatter.format("%-5s %-25s %-25s %-25s\n", "id", "name", "username", "password");

while(rs.next())
{
int id = rs.getInt(1);
String name = rs.getString(2);
String username = rs.getString(3);
String password = rs.getString(4);

formatter.format("%-5s %-25s %-25s %-25s\n", id,name,username, password);

}
System.out.println(formatter);

}
}

public void deleteData(int id) throws SQLException {
String query = String.format("DELETE FROM %s WHERE id=?", tableName);
try (PreparedStatement p = con.prepareStatement(query)) {
p.setInt(1, id);

int count = p.executeUpdate();

System.out.println(String.format("%d row(s) deleted sucessfully", count));
}
}

public Boolean close() throws SQLException {
con.close();
}
}

And here's the driver code:

下面是驱动程序代码:


import sqlite.SQLiteDatabase;

public class BankManagementSystem {
public static void main(String[] args) {

try(Scanner sc = new Scanner(System.in))
{
SQLiteDatabase sql = new SQLiteDatabase("login_details", "(id int, name varchar(255), username varchar(255), password varchar(255))");


// sql.showTables();
// sql.insertData(1, "rohan", "rohan", "1234");
// sql.deleteData(1);
sql.readData();

}
catch(Exception e)
{
Logger.getLogger(BankManagementSystem.class.getName()).log(Level.SEVERE, null, e);
}
}
}

When ran with the sql.insertData(1, "rohan", "rohan", "1234"); statement, its shows the following output:

当使用sql.insertData(1,“rohan”,“rohan”,“1234”);语句运行时,其显示以下输出:


1 row(s) added successfully

Then after running with the sql.readData(); statement, it shows the data entry in NetBeans.
Here's the output in NetBeans:

然后,在使用sql.readData();语句运行之后,它显示NetBeans中的数据条目。以下是NetBeans的输出:


id    name                      username                  password
1 rohan rohan 1234

But when I build the JAR file (clean and build option) then run the JAR file, it doesn't show the updated database.
It just shows:

但是,当我构建JAR文件(清理并构建选项)然后运行JAR文件时,它没有显示更新后的数据库。它只是显示了:


id    name                      username                  password

Please help me out! I want the data to be consistent. When I update the database from NetBeans, it should also reflect in the JAR file also.

请帮帮我!我希望数据是一致的。当我从NetBeans更新数据库时,它也应该反映在NetBeans文件中。


更多回答

Obviously, the database you updated through NetBeans is not the same database as the JAR. Please provide a minimal reproducible example that includes the relevant build files.

显然,您通过NetBeans更新的数据库与JAR不是同一个数据库。请提供一个包含相关构建文件的最小可重现示例。

A JAR file is like a ZIP file, it is an archive of other files, but it is not trivial to (directly) change the contents of the files inside the JAR/ZIP (also not the intention of it)

JAR文件类似于ZIP文件,它是其他文件的存档,但(直接)更改JAR/ZIP中文件的内容并不是一件小事(也不是它的本意)

I simply want to build a terminal based application. If I just run the java file, it can't find the SQLite drivers, and I have to provide a class path every time. Is there a better way to build a terminal based application using Java, other than converting into a Jar, such that the data persistence issue is resolved. Please help me!

我只是想建立一个基于终端的应用程序。如果我只运行Java文件,它就找不到SQLite驱动程序,每次我都必须提供一个类路径。有没有更好的方法来使用Java构建基于终端的应用程序,而不是转换为Jar,以便解决数据持久性问题。请帮帮我!

Your situation isn't using jpackage but you have a resource path db which means it is readonly. See How to control SQLite 3 database files directory after installing my app (Packaged using JPackage)

您的情况不是使用jPackage,但您有一个资源路径db,这意味着它是只读的。请参见How to Control SQLite 3数据库文件目录After Install My App(使用JPackage打包)

优秀答案推荐
更多回答

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com