import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.*;

public class COFFEE
{
  public static void main(String[] args) throws ClassNotFoundException
  {
    // load the sqlite-JDBC driver using the current class loader
    Class.forName("org.sqlite.JDBC");   
    Connection connection = null;
    try
    {
      // create a database connection
      connection = DriverManager.getConnection("jdbc:sqlite:COFFEE.db");
      Statement statement = connection.createStatement();
      statement.setQueryTimeout(30);  // set timeout to 30 sec.
      
      statement.executeUpdate("drop table if exists COFFEE");
      statement.executeUpdate("create table COFFEE(COFFEE_NAME String, CODE int, PRICE double, AMOUNT int,TOTAL double)");
      statement.executeUpdate("insert into COFFEE values('espresso',150,9.99,110,0);");
      statement.executeUpdate("insert into COFFEE values('French',49,8.99,130,0);");
      statement.executeUpdate("insert into COFFEE values('decafeniated',50,9.99,105,0);");
       statement.executeUpdate("insert into COFFEE values('colombian',101,7.99,959,0);");
      statement.executeUpdate("update COFFEE set TOTAL=PRICE*AMOUNT;");     
      ResultSet rs = statement.executeQuery("select * from COFFEE");
      String s="";
      while(rs.next())
      {
        // read the result set
        
        s+="COFFEE_NAME = " + rs.getString("COFFEE_NAME")+"\n";
        s+=" CODE = " + rs.getInt("CODE")+"\n";
        s+=" PRICE = " + rs.getDouble("PRICE")+"\n";
        s+=" AMOUNT = " + rs.getInt("AMOUNT")+"\n";
        s+=" TOTAL = " + rs.getDouble("TOTAL")+"\n";
        
      }
      JOptionPane.showMessageDialog(null,s);
    }
    catch(SQLException e)
    {
      // if the error message is "out of memory", 
      // it probably means no database file is found
      System.err.println(e.getMessage());
    }
    finally
    {
      try
      {
        if(connection != null)
          connection.close();
      }
      catch(SQLException e)
      {
        // connection close failed.
        System.err.println(e);
      }
    }
  }
}
