Accessing the Databases from Java

The ODBC programs were tried out in 'C'. We have used Visual C++ (2.1) and created the 'C' application programs (ODBC - 32bit).

The same programs have been tried out from JAVA. For the java programs we have

o1.c

#include <windows.h>
#include <sqlext.h>

int r;
char aa[100];
void * e;
int _stdcall WinMain(void * i, void * j, char * k, int l)
{
 r=SQLAllocEnv(&e);
 sprintf(aa,"r=%d..e=%p",r,e) ;
 MessageBox(0,aa,"SQLAllocEnv",0);
}

zzz.java
import jet.connect.*;
import java.applet.*;
public class zzz  extends Applet
{
 public void init()
 {
  DbEnv e;
  try
  {
   e = DbEnv.SQLAllocEnv();
   System.out.println("SQLAllocEnv..."+e);
  }
  catch(DbSqlException x)
  {
   System.out.println(x.getErrorMsg());
   return;
  }
 }
}


o2.c

#include <windows.h>
#include <sqlext.h>

int r;
char aa[100];
void * e;
void * c;

int _stdcall WinMain(void * i, void * j, char * k, int l)
{
 r=SQLAllocEnv(&e);
 sprintf(aa,"r=%d..e=%p",r,e) ;
 MessageBox(0,aa,"SQLAllocEnv",0);

 r=SQLAllocConnect(e,&c);
 sprintf(aa,"r=%d..c=%p",r,c) ;
 MessageBox(0,aa,"SQLAllocConnect",0);
}

zzz.java

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

public class zzz extends Applet
{
 public void init()
 {
  DbEnv e;
  DbDbc c;
  try
  {
   e = DbEnv.SQLAllocEnv();
   System.out.println("SQLAllocEnv..."+e);
   c = e.SQLAllocConnect();
   System.out.println("SQLAllocConnect..."+c);
  }
  catch(DbSqlException x)
  {
   System.out.println(x.getErrorMsg());
   return;
  }
 }
}


o3.c

#include <windows.h>
#include <sql.h>
#include <sqlext.h>

int r;
char aa[100];
void * e;
void * c;

int _stdcall WinMain(void * i, void * j, char * k, int l)
{
 r=SQLAllocEnv(&e);
 sprintf(aa,"r=%d..e=%p",r,e) ;
 MessageBox(0,aa,"SQLAllocEnv",0);

 r=SQLAllocConnect(e,&c);
 sprintf(aa,"r=%d..c=%p",r,c) ;
 MessageBox(0,aa,"SQLAllocConnect",0);

r=SQLConnect(c,"saccess",-3,"",0,"",0);
 sprintf(aa,"r=%d..",r) ;
 MessageBox(0,aa,"SQLConnect",0);

}

zzz.java

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

public class zzz extends Applet
{
 public void init()
 {
  boolean i;
  DbEnv e;
  DbDbc c;
  try
  {
  e = DbEnv.SQLAllocEnv();
  System.out.println("SQLAllocEnv..."+e);
  c = e.SQLAllocConnect();
  System.out.println("SQLAllocConnect..."+c);
  i=c.SQLConnect("sdbase","","");
  System.out.println("SQLConnect..."+i);
  }
  catch(DbSqlException x)
  {
  System.out.println(x.getErrorMsg());
  return;
  }
 }
}


o4.c

#include <windows.h>
#include <sqlext.h>
#include <sql.h>

int r,len,len2;
char aa[100],bb[255],ee[512];
void * e;
void * c;

int _stdcall WinMain(void * i, void * j, char * k, int l)
{
 r=SQLAllocEnv(&e);
 sprintf(aa,"r=%d..e=%p",r,e) ;
 MessageBox(0,aa,"SQLAllocEnv",0);

 r=SQLAllocConnect(e,&c);
 sprintf(aa,"r=%d..c=%p",r,c) ;
 MessageBox(0,aa,"SQLAllocConnect",0);

 r=SQLDataSources(e,SQL_FETCH_FIRST,bb,255,&len,ee,512,&len2);
 sprintf(aa,"r=%d bb = %s len = %d ee = %s len2 = %d",r,bb,len,ee,len2);
 MessageBox(0,aa,"SQLDataSources",0);

 while((SQLDataSources(e,SQL_FETCH_NEXT,bb,255,&len,ee,512,&len2))!=SQL_NO_DATA_FOUND)
 {
  sprintf(aa,"r=%d bb = %s len = %d ee = %s len2 = %d",r,bb,len,ee,len2);
  MessageBox(0,aa,"SQLDataSources",0);
 }
}


zzz.java

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

public class zzz extends Applet
{
 public void init()
 {
 DbEnv e;
 DbDbc c;
 String g;
 DbDataSource d;
 try
 {
 e = DbEnv.SQLAllocEnv();
 System.out.println("SQLAllocEnv..."+e);

 c = e.SQLAllocConnect();
 System.out.println("SQLAllocConnect..."+c);

 d=e.SQLDataSources(Db.SQL_FETCH_FIRST);
 g=d.getDSN();
 }
 catch(DbSqlException x)
 {
 System.out.println(x.getErrorMsg());
 return;
 }
 System.out.println("Data Source Name..."+g);
 d=e.SQLDataSources(Db.SQL_FETCH_NEXT);
 while(d != null)
 {
 g=d.getDSN();
 System.out.println("Data Source Name..."+g);
 d=e.SQLDataSources(Db.SQL_FETCH_NEXT);
 }
}
}

zzz.java
import jet.connect.*;
import java.applet.*;

public class zzz extends Applet
{
 public void init()
 {
 DbEnv e;
 DbDbc c;
 String g;
 String h;
 DbDataSource d;
 try
 {
 e = DbEnv.SQLAllocEnv();
 System.out.println("SQLAllocEnv..."+e);

 c = e.SQLAllocConnect();
 System.out.println("SQLAllocConnect..."+c);

 d=e.SQLDataSources(Db.SQL_FETCH_FIRST);
 g=d.getDSN();
 h=d.getDescription();
 System.out.println("Data Source Name..."+g);
 System.out.println("Data Source Description..."+h);
 }
 catch(DbSqlException x)
 {
 System.out.println(x.getErrorMsg());
 return;
 }
 d=e.SQLDataSources(Db.SQL_FETCH_NEXT);
 while(d != null)
 {
  g=d.getDSN();
  h=d.getDescription();
  System.out.println("Data Source Name..."+g);
  System.out.println("Data Source Description..."+h);
  d=e.SQLDataSources(Db.SQL_FETCH_NEXT);
 }
 }
}


o5.c

#include <windows.h>
#include <stdio.h>
#include <sqlext.h>
#include <sql.h>

int r,len,len2;
char aa[100],bb[255],cc[255];
void * e;
void * c;

int _stdcall WinMain(void * i, void * j, char * k, int l)
{
 r=SQLAllocEnv(&e);
 sprintf(aa,"r=%d..e=%p",r,e) ;
 MessageBox(0,aa,"SQLAllocEnv",0);

 r=SQLAllocConnect(e,&c);
 sprintf(aa,"r=%d..c=%p",r,c) ;
 MessageBox(0,aa,"SQLAllocConnect",0);

 r=SQLDrivers(e,SQL_FETCH_FIRST,bb,255,&len,cc,255,&len2);
 sprintf(aa,"r=%d bb = %s len = %d cc = %s len2 = %d",r,bb,len,cc,len2);
 MessageBox(0,aa,"SQLDrivers",0);

 while((SQLDrivers(e,SQL_FETCH_NEXT,bb,255,&len,cc,255,&len2))!=SQL_NO_DATA_FOUND)
 {
 sprintf(aa,"r=%d bb = %s len = %d cc = %s len2 = %d",r,bb,len,cc,len2);
 MessageBox(0,aa,"SQLDrivers",0);
 }
}

zzz.java

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

public class zzz extends Applet
{
 public void init()
 {
 DbEnv e;
 DbDbc c;
 String g;
 String h;

 DbDriver d;
 try
 {
 e = DbEnv.SQLAllocEnv();
 System.out.println("SQLAllocEnv..."+e);

 c = e.SQLAllocConnect();
 System.out.println("SQLAllocConnect..."+c);

 d=e.SQLDrivers(Db.SQL_FETCH_FIRST);
 g=d.getDriverDesc();
 h=d.getDriverAttributes();
 System.out.println("Driver Description..."+g);
 System.out.println("Driver Attributes..."+h);
 }
 catch(DbSqlException x)
 {
 System.out.println(x.getErrorMsg());
 return;
 }
 d=e.SQLDrivers(Db.SQL_FETCH_NEXT);
 while(d != null)
 {
 g=d.getDriverDesc();
 h=d.getDriverAttributes();
 System.out.println("Driver Description..."+g);
 System.out.println("Driver Attributes..."+h);
 d=e.SQLDrivers(Db.SQL_FETCH_NEXT);
 }
}
}


o6.c

#include <windows.h>
#include <sqlext.h>
#include <sql.h>

int r;
char aa[100];
void * e;
void * c;
void * s;


int _stdcall WinMain(void * i, void * j, char * k, int l)
{
 r=SQLAllocEnv(&e);
 sprintf(aa,"r=%d..e=%p",r,e) ;
 MessageBox(0,aa,"SQLAllocEnv",0);

 r=SQLAllocConnect(e,&c);
 sprintf(aa,"r=%d..c=%p",r,c) ;
 MessageBox(0,aa,"SQLAllocConnect",0);

r=SQLConnect(c,"saccess",-3,"",-3,"",-3);
sprintf(aa,"r=%d ",r);
MessageBox(0,aa,"SQLConnect",0);

r = SQLAllocStmt(c, &s);
sprintf(aa,"r=%d ",r);
MessageBox(0,aa,"SQLAllocStmt",0);

r = SQLExecDirect(s,"Create table aaa ( vno number ,name char (20))", -3);
sprintf(aa,"r=%d ",r);
MessageBox(0,aa,"SQLExecDirect",0);


}

zzz.java

import jet.connect.*;
import java.applet.*;
public class zzz extends Applet
{
 public void init()
 {
  DbChar aa;
  DbEnv e;
  DbDbc c;
  DbStmt s;
  boolean a;
  try
  {
   e = DbEnv.SQLAllocEnv();
   c=e.SQLAllocConnect();
   c.SQLConnect("saccess","","");
   s = c.SQLAllocStmt();
   a = s.SQLExecDirect("Create table ccc ( vno number ,name char (20))");
   System.out.println("Table created ..."+a);
  }
  catch(DbSqlException x)
  {
   System.out.println(x.getErrorMsg());
   return;
  }
 }
}


o7.c

#include <windows.h>
#include <sqlext.h>
#include <sql.h>

int r,ncols, ii,jj, len,err_code,len1,len2,len3;
char aa[100],bb[255],cc[512];
void * e;
void * c;
void * s;


int _stdcall WinMain(void * i, void * j, char * k, int l)
{
 r=SQLAllocEnv(&e);
 sprintf(aa,"r=%d..e=%p",r,e) ;
 MessageBox(0,aa,"SQLAllocEnv",0);

 r=SQLAllocConnect(e,&c);
 sprintf(aa,"r=%d..c=%p",r,c) ;
 MessageBox(0,aa,"SQLAllocConnect",0);

r=SQLConnect(c,"saccess",-3,"",-3,"",-3);
sprintf(aa,"r=%d ",r);
MessageBox(0,aa,"SQLConnect",0);

r = SQLAllocStmt(c, &s);
sprintf(aa,"r=%d ",r);
MessageBox(0,aa,"SQLAllocStmt",0);

r = SQLExecDirect(s,"Insert into  aaa values( 1001 ,'aaaa')", -3);
sprintf(aa,"r=%d ",r);
MessageBox(0,aa,"SQLExecDirect",0);


r = SQLExecDirect(s,"Insert into  aaa values( 1002 ,'bbbb')", -3);
sprintf(aa,"r=%d ",r);
MessageBox(0,aa,"SQLExecDirect",0);


r = SQLExecDirect(s,"Insert into  aaa values( 1003 ,'cccc')", -3);
sprintf(aa,"r=%d ",r);
MessageBox(0,aa,"SQLExecDirect",0);

}

zzz.java

import jet.connect.*;
import java.applet.*;
public class zzz extends Applet
{
 public void init()
 {
  DbChar aa;
  DbEnv e;
  DbDbc c;
  DbStmt s;
  boolean a;
  try
  {
   e = DbEnv.SQLAllocEnv();
   c=e.SQLAllocConnect();
   c.SQLConnect("saccess","","");
   s = c.SQLAllocStmt();
   a = s.SQLExecDirect("Insert into  bbb values( 1001 ,'aaaa')");
   System.out.println("Insert record ..."+a);
  }
  catch(DbSqlException x)
  {
   System.out.println(x.getErrorMsg());
   return;
  }
 }
}


o8.c

#include <windows.h>
#include <stdio.h>
#include <sqlext.h>
#include <sql.h>

int r,ncols, ii,jj, len,err_code,len1,len2,len3;
char aa[100],bb[255],cc[512];
void * e;
void * c;
void * s;


int _stdcall WinMain(void * i, void * j, char * k, int l)
{
 r=SQLAllocEnv(&e);
 sprintf(aa,"r=%d..e=%p",r,e) ;
 MessageBox(0,aa,"SQLAllocEnv",0);

 r=SQLAllocConnect(e,&c);
 sprintf(aa,"r=%d..c=%p",r,c) ;
 MessageBox(0,aa,"SQLAllocConnect",0);

r=SQLConnect(c,"saccess",-3,"",-3,"",-3);
sprintf(aa,"r=%d ",r);
MessageBox(0,aa,"SQLConnect",0);

r = SQLAllocStmt(c, &s);
sprintf(aa,"r=%d ",r);
MessageBox(0,aa,"SQLAllocStmt",0);

r = SQLExecDirect(s,"Select * from aaa", -3);
sprintf(aa,"r=%d ",r);
MessageBox(0,aa,"SQLExecDirect",0);


while (SQLFetch(s) != SQL_NO_DATA_FOUND)
{
 r = SQLGetData(s, 1,SQL_C_CHAR, aa,100, &len);
 r = SQLGetData(s, 2,SQL_C_CHAR, bb,100, &len);
 MessageBox(0,aa,bb,0);

}
}

zzz.java
import jet.connect.*;
import java.applet.*;
public class zzz extends Applet
{
 public void init()
 {
  DbChar aa,bb;
  DbEnv e;
  DbDbc c;
  DbStmt s;
  boolean a;
  aa = new DbChar(100);
  bb = new DbChar(100);
  try
  {
   e = DbEnv.SQLAllocEnv();
   c=e.SQLAllocConnect();
   c.SQLConnect("saccess","","");
   s = c.SQLAllocStmt();
   a = s.SQLExecDirect("Select * from bbb");
   System.out.println("Select..."+a);
   while (s.SQLFetch())
   {
    s.SQLGetData(1,aa);
    s.SQLGetData(2,bb);
    System.out.println("aa ..." + aa);
    System.out.println("bb ...." + bb); 
   }
  }
  catch(DbSqlException x)
  {
   System.out.println(x.getErrorMsg());
   return;
  }
 }
}


o9.c

#include <windows.h>
#include <sqlext.h>
#include <sql.h>

int r,n;
char aa[100];
void * e;
void * c;
void * s;

int _stdcall WinMain(void * i, void * j, char * k, int l)
{
 r=SQLAllocEnv(&e);
 sprintf(aa,"r=%d..e=%p",r,e) ;
 MessageBox(0,aa,"SQLAllocEnv",0);

 r=SQLAllocConnect(e,&c);
 sprintf(aa,"r=%d..c=%p",r,c) ;
 MessageBox(0,aa,"SQLAllocConnect",0);

r=SQLConnect(c,"saccess",-3,"",-3,"",-3);
sprintf(aa,"r=%d ",r);
MessageBox(0,aa,"SQLConnect",0);

r=SQLAllocStmt(c,&s);
sprintf(aa,"r=%d ",r);
MessageBox(0,aa,"SQLConnect",0);

r=SQLExecDirect(s,"Select * from aaa",-3);
sprintf(aa,"r=%d ",r);
MessageBox(0,aa,"SQLExecDirect",0);

r=SQLNumResultCols(s,&n);
sprintf(aa,"Number of cols=%d ",n);
MessageBox(0,aa,"SQLNumCols",0);

}

zzz.java

import jet.connect.*;
import java.applet.*;
public class zzz extends Applet
{
public void init()
{
int n;
DbEnv e;
DbDbc c;
DbStmt s;
try
{
 e = DbEnv.SQLAllocEnv();
 c = e.SQLAllocConnect();
 c.SQLConnect("saccess","","");
 s = c.SQLAllocStmt();
 s.SQLExecDirect("Select * from aaa");
}
catch(DbSqlException x)
{
 System.out.println(x.getErrorMsg());
 return;
}
n=s.SQLNumResultCols().intValue();
System.out.println("Number of columns"+ n);
}
}


o10.c

#include <windows.h>
#include <sqlext.h>
#include <sql.h>

int r,n, ii, len;
char aa[100],bb[255],cc[512];
void * e;
void * c;
void * s;


int _stdcall WinMain(void * i, void * j, char * k, int l)
{
 r=SQLAllocEnv(&e);
 sprintf(aa,"r=%d..e=%p",r,e) ;
 MessageBox(0,aa,"SQLAllocEnv",0);

 r=SQLAllocConnect(e,&c);
 sprintf(aa,"r=%d..c=%p",r,c) ;
 MessageBox(0,aa,"SQLAllocConnect",0);

r=SQLConnect(c,"saccess",-3,"",-3,"",-3);
sprintf(aa,"r=%d ",r);
MessageBox(0,aa,"SQLConnect",0);

r = SQLAllocStmt(c, &s);
sprintf(aa,"r=%d ",r);
MessageBox(0,aa,"SQLAllocStmt",0);

r = SQLExecDirect(s,"Select * from aaa", -3);
sprintf(aa,"r=%d ",r);
MessageBox(0,aa,"SQLExecDirect",0);

r = SQLNumResultCols(s,&n);
sprintf(aa,"r=%d ",r);
MessageBox(0,aa,"SQLExecDirect",0);


while (SQLFetch(s) != SQL_NO_DATA_FOUND)
{
 for( ii = 1; ii <= n ; ii++)
 {
  r = SQLGetData(s, ii,SQL_C_CHAR, aa,100, &len);
  MessageBox(0,aa,"SQLGetData",0);
 }
}
}

zzz.java

import jet.connect.*;
import java.applet.*;
public class zzz extends Applet
{
 public void init()
 {
  DbChar aa;
  DbEnv e;
  DbDbc c;
  DbStmt s;
  boolean a;
  int n, ii;
  aa = new DbChar(100);
  try
  {
   e = DbEnv.SQLAllocEnv();
   c=e.SQLAllocConnect();
   c.SQLConnect("saccess","","");
   s = c.SQLAllocStmt();
   a = s.SQLExecDirect("Select * from bbb");
   System.out.println("Select..."+a);
   n=s.SQLNumResultCols().intValue();
   System.out.println("Number of columns .."+n);
   while (s.SQLFetch())
   {
    for( ii = 1; ii <= n; ii++)
    {
     s.SQLGetData(ii,aa);
     System.out.println("aa ..." + aa);
    }
   }
  }
  catch(DbSqlException x)
  {
   System.out.println(x.getErrorMsg());
   return;
  }
 }
}


Take a coffee break and pass by our Java pageor you can move on to the next program in which we have used all the above syntaxes and developed a mini-interface for the databases.

Spare us a minute and fling your comments, feedback, suggestions et al to us right offhand...


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