import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.*;
import java.util.Vector; 
   
// This class reads, writes and plot data into/from a given SQL Database table
// 		        
public class SQLite1
{
// Reads given data column from a specified Lite database table
// databasename : name of the database file program adds ".db" to the name
// tablei database table name	
// column[]: database column names
String column[];
String result[][];
String databasename;
String table1;
public SQLite1(String databasenamei,String table1i) throws IOException,ClassNotFoundException
{   databasename=databasenamei;
    table1=table1i;
    // load the sqlite-JDBC driver using the current class loader
    String a[][];
    int n=1,m=1;
    Class.forName("org.sqlite.JDBC");   
    Connection connection = null;
    try
    {
      // create a database connection
      connection = DriverManager.getConnection("jdbc:sqlite:"+databasename+".db");
      Statement statement = connection.createStatement();
      statement.setQueryTimeout(30);  // set timeout to 30 sec.      
      //statement.executeUpdate("drop table if exists H9Ex3");
      ResultSet rs = statement.executeQuery("select * from "+table1);
      int i=0;
      ResultSetMetaData rsmd = rs.getMetaData();
      n=rsmd.getColumnCount();
      column=new String[n];
      for (int h=0;h<n;h++)
      {column[h]=rsmd.getColumnName(h+1);}
      String x;
      while(rs.next())
      {x=rs.getString(column[0]);i++;}
      m=i;
    }
    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);
      }
    }
    a=new String[n][m];
    try
    {
      // create a database connection
      connection = DriverManager.getConnection("jdbc:sqlite:"+databasename+".db");
      Statement statement = connection.createStatement();
      statement.setQueryTimeout(30);  // set timeout to 30 sec.      
      //statement.executeUpdate("drop table if exists H9Ex3");
      String s="select ";
      ResultSet rs = statement.executeQuery("select * from "+table1);
      ResultSetMetaData rsmd = rs.getMetaData();
      String column[]=new String[n];
      for (int h=0;h<n;h++)
      {
      column[h]=rsmd.getColumnName(h+1);
      }
      for(int i=0;i<m;i++)
      {rs.next();
       for(int j=0;j<n;j++)
        {   
	        a[j][i]=rs.getString(column[j]);
        }        
      }
    }
    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);
      }
    }
    result=a;
}

public void print()
{Text.printT(result,column,"data base  : "+databasename+"  table : "+table1);}

}
