0

I am getting the above error when trying to do an insert(or select) to a SQLite file from Java in Netbeans. I have created the db file manually from SQLite Database Browser and put it in the source package. Below is the code and logs:

public void DBInsertServerConfig(ServerConfig serverconfig) throws SQLException {
  Connection conn = DBConnect();
  Statement statement = null;
  conn.setAutoCommit(false);
  statement = conn.createStatement();
  String sql = "INSERT INTO serverconfig(ip,port,db_name,db_user,password,fcm_server_key) " +
                    "VALUES (?,?,?,?,?,?)"; //
  try{
        PreparedStatement pstm = conn.prepareStatement(sql);
        pstm.setString(1,serverconfig.getIp());
        pstm.setString(2,serverconfig.getPort());
        pstm.setString(3,serverconfig.getDb_name());
        pstm.setString(4,serverconfig.getDb_user());
        pstm.setString(5,serverconfig.getPassword());
        pstm.setString(6,serverconfig.getFcm_server_key());
        //statement.execute(sql);
        pstm.executeUpdate();
        statement.close();
        conn.commit(); 
        conn.close(); 

The database is opened correctly but it seems it doesn't find the table although it exist.

compile:run:
Opened database successfully
org.sqlite.SQLiteException: [SQLITE_ERROR] SQL error or missing database (no such table: serverconfig)
BUILD SUCCESSFUL (total time: 9 seconds)

Attached is a screenshot of the db file from SQLite Database Browser : enter image description here

I have seen and tried other posts like in here but I didn't get a solution.

Can anyone help me figuring out this?

4
  • Your SQL Statement indicates 6 table fields yet you are trying to supply 7 parameters (?). Remove one of the PreparedStatement parameters (?) and its related comma. Commented May 19, 2018 at 9:53
  • Yeah, I saw that, it's a mistype. But that's not the problem. Thanks Commented May 19, 2018 at 10:02
  • Are you sure you did File/Write Changes before copying the DB? Commented May 19, 2018 at 10:41
  • @MikeT , yeah I did. Thanks Commented May 19, 2018 at 11:29

3 Answers 3

7

I found the answer and I am posting if anyone run into the same problem/confusion. I had put my db file under the src package while the url path was pointing into the project root folder(outside src). An empty db file was created by netbeans, and of course it hadn't any table in it. That's what happen when you follow tutorials that haven't been tested by their own creators. :D

Sign up to request clarification or add additional context in comments.

Comments

0

The example that I used was

Connection conn = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\Tolga\\Documents\\NetBeansProjects\\dpmlzmtkb\\depom.sqlite");

Which is wrong.

I changed depom.SQLite to depom.db and it worked. So as I understand if the path is correct NetBeans creating another empty SQLite database to the given path.

Comments

-1

Please use the absolute path,like(on my Ubuntu)
private static String url ="jdbc:sqlite:/home/yourname/study/eclipse/hk/ss.db";

At first i get same error like yours, i can link sql in ordinary class,but in the servlet/jsp can't

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.