Mini - interface to access the Databases from Java


The jetconnect software has been downloaded form http://www.xdb.com . To understand the basics you can flip over to the previous page.


zzz.java


import jet.connect.*;
import java.awt.*;
import java.applet.*;
import java.util.*;

public class zzz extends Applet
{
 Panel p1,p2,p3,p4,p5,p6,p7,p8;
 Panel q,q1,q2;
 Panel r1,r2,r3,r4,r5;
 Panel o1,o2;
 TextField t1,t2,t3,t4,t5,t6;
 List x1;
 Button b1,b2,b3,b4,b5,b6,b7,b8;
 List l1,l2,l3,l4,l5,l6,l9;
 Label a1,a11,a2,a3,a4,a5,a6,a7;
 Label a12,a13,a14;
 DbEnv e;
 DbDbc c;
 DbStmt s;
 DbInteger i;
 DbColDesc l;
 char c1[];
 String g;
 String h;
 String s1,s2,s3,s4,s5,s6,s7,s8,s9;
 String ss1,ss2;
 int n,n1,n2,n3,n4,ctr,n5,n6,n7;
 int ii;
 DbDataSource d;
 DbDriver d1;
 Dimension dd,dd1,dd2;
 boolean a;
 DbChar b;
 DbChar aa,bb;
 DbInteger cc;


 StringTokenizer st1;

 public void init()
 {
 e = DbEnv.SQLAllocEnv();
 

 
 l = new DbColDesc();
 b = new DbChar(200);
 setLayout(new GridLayout(4,1));
 aa = new DbChar(100);
 bb = new DbChar(100);
 cc = new DbInteger();
 ctr = 1;
  ii = 0;
 // **************************************

 p1 = new Panel();
 p1.setLayout(new FlowLayout(0));
 b4 = new Button("CONNECT DSN....");
 p1.add(b4);

 q1 = new Panel();
 q1.setLayout(new BorderLayout());
 b1 = new Button("        DataSources................(Drivers)                ");
 l1 = new List(4,false);

 q1.add("North",b1);
 q1.add("Center",l1);

 p1.add(q1);


 b2 = new Button("            Installed         Drivers               ");
 l2 = new List(4,false);
 q2 = new Panel();
 q2.setLayout(new BorderLayout());
 q2.add("North",b2);
 q2.add("Center",l2);
 
 p1.add(q2);


 add(p1);

 //    ***********************
 p2 = new Panel();
 p2.setLayout(new FlowLayout(0));
 r1 = new Panel();
 r1.setLayout(new BorderLayout());
 a1 = new Label ("Data Source Name");
 t1 = new TextField(15);
 t1.disable();
 b6 = new Button("Display Columns");
 r1.add("North",a1);
 r1.add("Center",t1);
 r1.add("South",b6);
 b6.disable();
 p2.add(r1);

 r4 = new Panel();
 r4.setLayout(new BorderLayout());
 a5 = new Label("Table Names -DSN");
 l3 = new List(4,false);

 r4.add("North",a5);
 r4.add("Center",l3);
 p2.add(r4);

 r2 = new Panel();
 r2.setLayout(new BorderLayout());
 a12 = new Label("Column Names ");
 l4 = new List(4,false);
 r2.add("North",a12);
 r2.add("Center",l4);
 p2.add(r2);

 r3 = new Panel();
 r3.setLayout(new BorderLayout());
 a13 = new Label("Column Types");
 l5 = new List(4,false);
 r3.add("North",a13);
 r3.add("Center",l5);
 p2.add(r3);
 
 r5 = new Panel();
 r5.setLayout(new BorderLayout());
 a14 = new Label("Column Width");
 l9 = new List(4,false);
 r5.add("North",a14);
 r5.add("Center",l9);
 p2.add(r5);


 add(p2);

//  ******************

 p3 = new Panel();
 p3.setLayout(new BorderLayout());

 a3 = new Label("     SQL statement - Insert , Update , Delete ");
 p3.add("North",a3);
 o1 = new Panel();
 o1.setLayout(new FlowLayout(0,25,10));
 t2 = new TextField(70);
 b3 = new Button("Execute SQL");
 b3.disable();

 o1.add(b3);
 o1.add(t2);

 p3.add("Center",o1);
 o2 = new Panel();
 o2.setLayout(new FlowLayout(0));
 a6 = new Label ("No. of records  affected"); 
 t3 = new TextField(5);
 a7 = new Label("Total no. of records in the table");
 t6 = new TextField(5);
 o2.add(a6);
 o2.add(t3);
 o2.add(a7);
 o2.add(t6);

 p3.add("South",o2);
 add(p3);

// ***********

 p7 = new Panel();
 p7.setLayout(new BorderLayout());
 p8 = new Panel();
 p8.setLayout(new FlowLayout(0,22,10));
 t4 = new TextField(70);
 b5 = new Button("Execute Select");
 p8.add(b5);
 b5.disable();
 p8.add(t4);
 p7.add("North",p8);
 x1 = new List(6,false);
 p7.add("Center",x1);
 add(p7);
 }
 
 public boolean handleEvent (Event v)
 {
 super.handleEvent(v);
 if(v.target instanceof TextField)
 {
  s2 = t2.getText();
  n1 = s2.length();
  if ( n1 > 0)
  b3.enable();
  else
  b3.disable();
  b5.enable();
 }

 return false;
}


 public boolean action (Event v,Object o)
 {
 if ("Display Columns".equals(o))
 {

 ii =  0;
 t3.setText("    ");
 t6.setText("    ");
 n = l4.countItems();
 for(int i = 0; i<n; i++)
 l4.delItem(0);

 n = l5.countItems();
 for(int i = 0; i<n; i++)
 l5.delItem(0);

 n = l9.countItems();
 for(int i = 0; i<n; i++)
 l9.delItem(0);

 n = x1.countItems();
 for(int i = 0; i<n; i++)
 x1.delItem(0);


 s8 = l3.getSelectedItem();
 s = c.SQLAllocStmt();
 a = s.SQLColumns(null,null,null,null);
 while (s.SQLFetch())
 {
  s.SQLGetData(3,aa);
  s.SQLGetData(4,bb);
  s.SQLGetData(5,cc);

  if (s8.equals(aa.toString()))
  {
  l4.addItem(bb.toString());
  n3 = cc.intValue();
 if (n3 == Db.SQL_CHAR)
   s5="CHAR";
 if (n3 == Db.SQL_VARCHAR)
   s5="VARCHAR";
 if (n3 == Db.SQL_SMALLINT)
   s5="SMALLINT" ;
 if (n3 == Db.SQL_INTEGER)
   s5="INTEGER";
 if (n3 == Db.SQL_DECIMAL)
   s5="DECIMAL";
 if (n3 == Db.SQL_NUMERIC)
   s5="NUMERIC";
 if (n3 == Db.SQL_REAL)
   s5="REAL" ;
 if (n3 == Db.SQL_FLOAT)
   s5="FLOAT";
 if (n3 == Db.SQL_DOUBLE)
   s5="DOUBLE";
 l5.addItem(s5);
 }
 }
 l4.select(0);
 l5.select(0);
 s.SQLFreeStmt(Db.SQL_DROP);

 s = c.SQLAllocStmt();
 s9 = "Select * from " + s8 ;
 s.SQLExecDirect(s9);
 n6=s.SQLNumResultCols().intValue();
 for(int i = 1; i <= n6 ; i++)
 {
  s.SQLDescribeCol(i,l);
  n7 = l.getPrecision();
  l9.addItem(".."+n7);
 }
 s.SQLFreeStmt(Db.SQL_DROP);

 s = c.SQLAllocStmt();
 s.SQLExecDirect(s9);
 t6.setText("  ");
 while (s.SQLFetch())
 {
 ii++;
 }
 t6.setText(".."+ii);
 s.SQLFreeStmt(Db.SQL_DROP);
 }

 if ("        DataSources................(Drivers)                ".equals(o))
 {
 b1.disable();
 n = l1.countItems();
 for(int i = 0; i<n; i++)
 l1.delItem(0);
 d=e.SQLDataSources(Db.SQL_FETCH_FIRST);
 g=d.getDSN();
 h = d.getDescription();
 ss1 = g+"......"+h;
 l1.addItem(ss1);
 d=e.SQLDataSources(Db.SQL_FETCH_NEXT);
 while(d != null)
 {
 g=d.getDSN();
 h = d.getDescription();
 ss1 = g+"......"+h;
 l1.addItem(ss1);
 d=e.SQLDataSources(Db.SQL_FETCH_NEXT);
 }
  l1.select(0);
 }
 if("            Installed         Drivers               ".equals(o))
 {
 b2.disable();
 n = l2.countItems();
 for(int i = 0; i<n; i++)
 l2.delItem(0);
 d1=e.SQLDrivers(Db.SQL_FETCH_FIRST);
 g=d1.getDriverDesc();

 l2.addItem(g);
 d1=e.SQLDrivers(Db.SQL_FETCH_NEXT);
 while(d1 != null)
 {
 g=d1.getDriverDesc();
 l2.addItem(g);
 d1=e.SQLDrivers(Db.SQL_FETCH_NEXT);
 }
 }
 if ("CONNECT DSN....".equals(o))
 {
 if (ctr > 1)
 {
 c.SQLDisconnect();
 c.SQLFreeConnect();
 }
 c=e.SQLAllocConnect();
 int y = l1.getSelectedIndex();
 if (y >= 0)
 {
 b6.enable();

 n = l3.countItems();
 for(int i = 0; i<n; i++)
 l3.delItem(0);

 n = l4.countItems();
 for(int i = 0; i<n; i++)
 l4.delItem(0);

 n = l5.countItems();
 for(int i = 0; i<n; i++)
 l5.delItem(0);

 n = l9.countItems();
 for(int i = 0; i<n; i++)
 l9.delItem(0);

 n = x1.countItems();
 for(int i = 0; i<n; i++)
 x1.delItem(0);

 t2.setText("");
 t4.setText("");
 t2.setText("");
 t6.setText("");
 s1 =  l1.getSelectedItem();
 n5 = s1.length();
 st1 = new StringTokenizer(s1,".");
 ss2 = st1.nextToken();

 t1.setText(ss2);
 c.SQLConnect(ss2,"","");
 ctr = 2;
 s = c.SQLAllocStmt();
 a = s.SQLTables(null,null,null,"TABLE");
 while (s.SQLFetch())
 {
  s.SQLGetData(3,aa);
  l3.addItem(aa.toString());
 }
 l3.select(0);
 s.SQLFreeStmt(Db.SQL_DROP);
 //b4.disable();

 }
 }
 if("Execute SQL".equals(o))
 {

  s2 = t2.getText();
  if (s2.length() > 0)
  {
  s= c.SQLAllocStmt() ;
  s.SQLExecDirect(s2);
  i=s.SQLRowCount();

  s3=".."+i.toString();
  t3.setText(s3);
  s.SQLFreeStmt(Db.SQL_DROP);
 ii=0 ;
 s = c.SQLAllocStmt();
 s9 = "Select * from " + s8 ;
 a = s.SQLExecDirect(s9);
 t6.setText("  ");
 while (s.SQLFetch())
 {
 ii++;
 }
 t6.setText(".."+ii);
 s.SQLFreeStmt(Db.SQL_DROP);
 }
 }


 if("Execute Select".equals(o))
 {
 s6=t4.getText();
 if (s6.length() > 0)
 {
 s = c.SQLAllocStmt();
 a = s.SQLExecDirect(s6);
 n4=s.SQLNumResultCols().intValue();
 n = x1.countItems();
 for(int i = 0; i<n; i++)
 x1.delItem(0);

 while (s.SQLFetch())
 {
 s7 = new String();
 for( int ii = 1; ii <= n4; ii++)
 {
  s.SQLGetData(ii,b);
  if ( ii > 1)
  s7=s7+",";
  s7=s7+b.toString() ;
 }
  x1.addItem(s7);
 }
 s.SQLFreeStmt(Db.SQL_DROP);
 }
 }
 return false;
 }
}


The expalnations to these pages will be put up as soon as possible. Thanks for visiting this page and very soon we will come with new simplified programs explaining this concept in greater detail. Visit our main page Vijay Mukhi's Technology Cornucopia and please tell us what you feel about the tutorials.

Email your comments, feedback, suggestions et al to us .


Vijay Mukhi's Computer Institute
B-13, Everest Building, Tardeo, Bombay 400 034, India.
http://www.neca.com/~vmis
e-mail : vmukhi@giasbm01.vsnl.net.in
1