import com.almworks.sqlite4java.*;
import java.io.*;
import java.util.Vector; 
import static java.lang.Math.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
    class Stringdata
	{
	public String x[];	
	public Stringdata(String[] xi)
	{
	int n=xi.length;
	x=new String[n];
	for(int i=0;i<n;i++) {x[i]=xi[i];}
	}	
	public String toString() {return ""+x[0]+" "+x[1];}
	}
public class SQLiteDB
{SQLiteConnection db;
public SQLiteDB(String f) throws SQLiteException
{ ConnectDB(f);}
public SQLiteDB(File f) throws SQLiteException
{ ConnectDB(f);}
public void ConnectDB(File f) throws SQLiteException
{ db = new SQLiteConnection(f);
  db.open(true);
}
public void ConnectDB(String f) throws SQLiteException
{ 
  db = new SQLiteConnection(new File(f));
  db.open(true);
}

public void dropTable(String tablename) throws SQLiteException
{ String s="drop table if exists "+tablename;
 db.exec(s);
}
public void createTable(String tablename,String field) throws SQLiteException
{ String s="create table "+tablename+"("+field+")";
  db.exec(s);
}

public void createTable(String tablename,String fieldname[],String fieldtype[]) throws SQLiteException
{ String s="create table "+tablename+"(";
int i=0;
  for(i=0;i<fieldname.length-1;i++)
  {s+=fieldname[i]+","+fieldtype[i]+",";}
  s+=fieldname[i]+","+fieldtype[i]+")";
  db.exec(s);
}
    public static String[][] inputString(String filename,String delim) throws IOException
    { BufferedReader fin=new BufferedReader(new FileReader(filename));
	 ArrayList<Stringdata> a=new ArrayList<Stringdata>();
	 int mmax=0;
	 String b[][];
	 int n=0,m=0;
	 String s1="";	 
	 try{
       while(fin!=null)
       { s1=fin.readLine();
       if(s1==null)break;
         StringTokenizer token=new StringTokenizer(s1,delim);
         m=token.countTokens();
         if(m>mmax) mmax=m;
         String aa[]=new String[m];
         int j=0;           
         while(token.hasMoreTokens())
         { aa[j++]=token.nextToken();}
         Stringdata vx=new Stringdata(aa);
         a.add(vx);
       n++;
	   }
       } catch(EOFException e_eof)
          {
          //close ffile
          try{fin.close();
      } 
      catch(NullPointerException e) {}   
      catch(IOException e)
             {
             System.err.println("Error Closing File\n"+e.toString());
             System.exit(1);
             }
          } //End of EOFException
     b=new String[n][mmax];
     int k=0;
     Iterator<Stringdata> i=a.iterator();
     while(i.hasNext()) {Stringdata vx=i.next();b[k++]=vx.x;}
     String a1[][]=new String[b[0].length][b.length];
     /*
     for(int ii=0;ii<b[0].length;ii++)
     {for(int jj=0;jj<b.length;jj++)
       {a1[jj][ii]=b[jj][ii];}
     }
     */
     return b;
     }

public void loadTable(String tablename,String filename,String delim) throws SQLiteException,IOException
{  String a[][]=inputString(filename,delim);
   int n=a.length;
   int m=a[0].length;
   int i,j;
   for(i=0;i<n;i++)
   {
   String s="insert into "+tablename+" values(";   
   for(j=0;j<m-1;j++)
   {s+=a[i][j]+",";}
    s+=a[i][j]+");";
    System.out.println(s);
    db.exec(s);
   }
}

public void loadTable(String tablename,String filename) throws SQLiteException,IOException
{  loadTable(tablename,filename," ");}

public void insertIntoTable(String tablename,String query[][]) throws SQLiteException,IOException
{  int n=query.length;
   int m=query[0].length;
   int i,j;
   for(i=0;i<n;i++)
   {
   String s="insert into "+tablename+" values(";   
   for(j=0;j<m-1;j++)
   {s+=query[i][j]+",";}
    s+=query[i][j]+");";
    System.out.println(s);
    db.exec(s);
   }
}
public void insertIntoTable(String tablename,String query[]) throws SQLiteException,IOException
{  
   int m=query.length;
   int i,j;
   String s="insert into "+tablename+" values(";   
   for(j=0;j<m-1;j++)
   {s+=query[j]+",";}
    s+=query[j]+");";
    System.out.println(s);
    db.exec(s);
}

public void exec(String query) throws SQLiteException
{db.exec(query);}

public void insertIntoTable(String tablename,String query) throws SQLiteException,IOException
{  String s="insert into "+tablename+" values( "+query+");";
    db.exec(s);
}

public void updateTable(String tablename,String query) throws SQLiteException,IOException
{  String s="update "+tablename+" set "+query+";";
    db.exec(s);
}
public void selectTable(String tablename,String query) throws SQLiteException
{SQLiteStatement statement = db.prepare("select "+query+" from "+tablename+";");
String isim[];
Vector<Object> cache=new Vector<Object>();
Object value;
Object a[][];
int m=0;
int n=0;
try { n=statement.columnCount();
      isim=new String[n];
      for (int i=0;i<n;i++)
      {isim[i]=statement.getColumnName(i);}
      while (statement.step()) 
      {m++;Object[] record=new Object[n];
      for(int i=0;i<n;i++)
        {record[i]=statement.columnValue(i);}
      cache.add(record);
      } //while'in sonu
      a=new Object[m][n];
      for(int i=0;i<m;i++)
      {for(int j=0;j<n;j++)
       {a[i][j]=((Object[])cache.elementAt(i))[j];}
      } 
      } finally {statement.dispose();}
      JTableP.print(a,isim,"Veri tabanı SORGU Çıktı ");  
}

public void query(String s) throws SQLiteException
{SQLiteStatement statement = db.prepare(s);
String isim[];
Vector<Object> cache=new Vector<Object>();
Object value;
Object a[][];
int m=0;
int n=0;
try { n=statement.columnCount();
      isim=new String[n];
      for (int i=0;i<n;i++)
      {isim[i]=statement.getColumnName(i);}
      while (statement.step()) 
      {m++;Object[] record=new Object[n];
      for(int i=0;i<n;i++)
        {record[i]=statement.columnValue(i);}
      cache.add(record);
      } //while'in sonu
      a=new Object[m][n];
      for(int i=0;i<m;i++)
      {for(int j=0;j<n;j++)
       {a[i][j]=((Object[])cache.elementAt(i))[j];}
      } 
      } finally {statement.dispose();}
      JTableP.print(a,isim,"Veri tabanı SORGU Çıktı ");  
}

public static void main(String arg[]) throws SQLiteException,IOException
{ SQLiteDB sqdb=new SQLiteDB("/temp");
  sqdb.dropTable("KAHVE");
  sqdb.createTable("KAHVE","isim String,kot int,fiyat double,miktar int,toplam double");
  sqdb.loadTable("KAHVE","kahve.txt");
  sqdb.insertIntoTable("KAHVE","\'Argantina\',123,16.15,45,0"); 
  sqdb.updateTable("KAHVE","toplam=fiyat*miktar");
  sqdb.query("select avg(fiyat) as T from KAHVE"); 
  sqdb.selectTable("KAHVE","*"); 
}

} 