'Java - SQLITE_BUSY; database file is locked

My application reads a html table and then a script (TableToCSV) converts it into a .csv format. After that I convert that .csv into a sqlite database. After that I run queries on the database. Problem is that upon executing, it shows that SQLITE_BUSY; database file is locked.

Whats the reason of this and how can I fix this?

Here is my code -

        final JFileChooser  fileDialog = new JFileChooser();
    JButton btnInputFile = new JButton("Input File");
    btnInputFile.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            int returnVal = fileDialog.showOpenDialog(rootPane);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
               java.io.File file = fileDialog.getSelectedFile();

               String name = file.getName();
               name = name.substring(0, name.lastIndexOf("."));
               name += ".html";
               File newFile = new File(file.getParentFile(), name);
               if (file.renameTo(newFile)) {
                   try {
                    TableToCSV tableToCSV = new TableToCSV(newFile, ',', '\"', '#', CSV.UTF8Charset );
                    System.out.println("action");
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
               }

               try
               {
                BufferedReader br=new BufferedReader(new FileReader("v.csv"));
                String line;

                while((line=br.readLine())!=null)
                {
                    System.out.println(line);
                    String[]value = line.split(",");
                    System.out.println(value.length);

                    String sql = "INSERT into main ([Ticket #], Status, Priority, Department, [Account Name]) "
                     + "values ('"+value[0]+"','"+value[1]+"','"+value[2]+"','"+value[3]+"','"+value[4]+"')";

                     PreparedStatement pst = DatabaseConnection.ConnectDB().prepareStatement(sql);
                     pst.executeUpdate();

                }
                br.close();

               }

               catch(Exception e)
               {
                JOptionPane.showMessageDialog(null, e);
               }

            }

        }
    });

UPDATE - DatabaseConnection.java

public class DatabaseConnection {
Connection conn = null;
Statement stmt = null;

public static Connection ConnectDB() {

    try {
        Class.forName("org.sqlite.JDBC");
        Connection conn = DriverManager.getConnection("jdbc:sqlite:database.db");
        conn.setAutoCommit(true);
        return conn;
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);
        return null;
    }
}
}


Solution 1:[1]

It's probably due to you having multiple open references to the sqlite database. I'd start by closing your PreparedStatement in a finally block inside your while loop.

PreparedStatement pst = null;
try{   
  pst = DatabaseConnection.ConnectDB().prepareStatement(sql);
  pst.executeUpdate();
}finally{
  if(pst != null) {
    pst.close();
  }
}

You should also close the database connection at the end of everything. If that doesn't fix it, then please explain more about how you "convert that .csv into a sqlite database"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 bradvido