import com.almworks.sqlite4java.*;
import java.io.*;
import java.util.Vector;    
import javax.swing.*;
import javax.swing.table.*;

public class QueryTableModel1 extends AbstractTableModel
{
 Vector<Object> cache;
 int colCount;
 String[] headers;
 SQLiteConnection db;
 SQLiteStatement rs;
 String currentURL;
static final long serialVersionUID = 908537987L;
 public QueryTableModel1()
 {
  cache=new Vector<Object>();
 }

 public String getColumnName(int i) {return headers[i];}
 public int getColumnCount() {return colCount;}
 public int getRowCount() {return cache.size();}

 public Object getValueAt(int row,int col)
 {
  return ((String[])cache.elementAt(row))[col];
 }

 public void setHostURL(String url)
 {
   if(url.equals(currentURL))
   {return;}
   closeDB();
   initDB(url);
   currentURL=url;
 }

 public void setQuery(String q)
 {
 cache= new Vector<Object>();
 try{
	rs = db.prepare(q);
	colCount=rs.columnCount();
    //ResultSet rs=statement.executeQuery(q);
    //ResultSetMetaData meta=rs.getMetaData();
    //colCount=meta.getColumnCount();
    headers=new String[colCount];
    for (int h=0;h<colCount;h++)
      {
      headers[h]=rs.getColumnName(h);
      }
    while(rs.step())
      {
      String[] record=new String[colCount];
      for(int i=0;i<colCount;i++)
        {record[i]=rs.columnString(i);}
      cache.add(record);
      } //while'in sonu
    fireTableChanged(null);
    } //try'in sonu
    catch(Exception e)
    {
    cache=new Vector<Object>();
    e.printStackTrace();
    }
 } // setQuery sonu

 public void initDB(String url)
 {
 try {
     db=new SQLiteConnection(new File("/"+url));
     db.open(true);
     }
     catch(Exception e)
     {
       System.out.println("veri tabanı kapatılamadı");
       e.printStackTrace();
     }
 } //initDB sonu

 public void closeDB()
 {
   try {
       if(rs!= null) {rs.dispose();}
       }
       catch(Exception e)
       {
       System.out.println("veri tabanı kapatılamadı");
       e.printStackTrace();
       }
 }  //closeDB sonu

}
