O D B C

Start Microsoft Access and create a new database mydb.mdb in jdbc subdirectory

Control Panel - 32 bit odbc - create a new dsn - dsn1 , the driver will be MS Access - file c:\jdbc\mydb.mdb

--------------------------------------------------

>cl -c o1.c

>link o1.obj odbc32.lib user32.lib

-----------------------------------------

o1.c

#include <windows.h>

#include <sqlext.h>

int ret_code;

void *henv;

void *hdbc;

void *hstmt;

char aa[100];

int _stdcall WinMain(HINSTANCE i, HINSTANCE j, char *k, int l)

{

ret_code = SQLAllocEnv(&henv);

sprintf(aa,"ret_code=%d..henv=%p\n",ret_code,henv);

MessageBox(0,aa,aa,0);

ret_code = SQLAllocConnect(henv,&hdbc);

sprintf(aa,"ret_code=%d..hdbc=%p\n",ret_code,hdbc);

MessageBox(0,aa,aa,0);

ret_code = SQLConnect(hdbc,"dsn1",-3,"",0,"",0);

sprintf(aa,"ret_code=%d\n",ret_code);

MessageBox(0,aa,aa,0);

ret_code=SQLAllocStmt(hdbc,&hstmt);

sprintf(aa,"ret_code=%d\n",ret_code);

MessageBox(0,aa,aa,0);

ret_code=SQLExecDirect(hstmt,"Create table sprod1 (pid int,pname char(10),price float)",-3);

sprintf(aa,"ret_code=%d\n",ret_code);

MessageBox(0,aa,aa,0);

}

o1.c

#include <windows.h>

#include <sqlext.h>

int ret_code;

void *henv;

void *hdbc;

void *hstmt;

char aa[100];

int _stdcall WinMain(HINSTANCE i, HINSTANCE j, char *k, int l)

{

ret_code = SQLAllocEnv(&henv);

sprintf(aa,"ret_code=%d..henv=%p\n",ret_code,henv);

MessageBox(0,aa,aa,0);

ret_code = SQLAllocConnect(henv,&hdbc);

sprintf(aa,"ret_code=%d..hdbc=%p\n",ret_code,hdbc);

MessageBox(0,aa,aa,0);

ret_code = SQLConnect(hdbc,"dsn1",-3,"",0,"",0);

sprintf(aa,"ret_code=%d\n",ret_code);

MessageBox(0,aa,aa,0);

ret_code=SQLAllocStmt(hdbc,&hstmt);

sprintf(aa,"ret_code=%d\n",ret_code);

MessageBox(0,aa,aa,0);

ret_code=SQLExecDirect(hstmt,"insert into sprod1 values (1,'Gold',100)",-3);

sprintf(aa,"ret_code=%d\n",ret_code);

MessageBox(0,aa,aa,0);

ret_code=SQLExecDirect(hstmt,"insert into sprod1 values (2,'Silver',200)",-3);

sprintf(aa,"ret_code=%d\n",ret_code);

MessageBox(0,aa,aa,0);

ret_code=SQLExecDirect(hstmt,"insert into sprod1 values (3,'Copper',300)",-3);

sprintf(aa,"ret_code=%d\n",ret_code);

MessageBox(0,aa,aa,0);

}

o1.c

#include <windows.h>

#include <sqlext.h>

int ret_code;int len1;

void *henv;void *hdbc;void *hstmt;

char aa[100];char bb[100];char cc[100];char dd[100];

int _stdcall WinMain(HINSTANCE i, HINSTANCE j, char *k, int l)

{

ret_code = SQLAllocEnv(&henv);

sprintf(aa,"ret_code=%d..henv=%p\n",ret_code,henv);

MessageBox(0,aa,aa,0);

ret_code = SQLAllocConnect(henv,&hdbc);

sprintf(aa,"ret_code=%d..hdbc=%p\n",ret_code,hdbc);

MessageBox(0,aa,aa,0);

ret_code = SQLConnect(hdbc,"dsn1",-3,"",0,"",0);

sprintf(aa,"ret_code=%d\n",ret_code);

MessageBox(0,aa,aa,0);

ret_code=SQLAllocStmt(hdbc,&hstmt);

sprintf(aa,"ret_code=%d\n",ret_code);

MessageBox(0,aa,aa,0);

ret_code=SQLExecDirect(hstmt,"Select pid,pname,price from sprod1 ",-3);

sprintf(aa,"ret_code=%d\n",ret_code);

MessageBox(0,aa,aa,0);

ret_code=SQLBindCol(hstmt,1,SQL_C_CHAR,bb,100,&len1);

sprintf(aa,"ret_code=%d\n",ret_code);

MessageBox(0,aa,aa,0);

ret_code=SQLBindCol(hstmt,2,SQL_C_CHAR,cc,100,&len1);

sprintf(aa,"ret_code=%d\n",ret_code);

MessageBox(0,aa,aa,0);

ret_code=SQLBindCol(hstmt,3,SQL_C_CHAR,dd,100,&len1);

sprintf(aa,"ret_code=%d\n",ret_code);

MessageBox(0,aa,aa,0);

while ((SQLFetch(hstmt)) != SQL_NO_DATA_FOUND)

{

MessageBox(0,bb,"Col1",0);

MessageBox(0,cc,"Col2",0);

MessageBox(0,dd,"Col3",0);

}

}

--------------------------------------------

 

 

 

 

 

J D B C

If not created earlier..............

c:\>md jdbc

c:\jdbc>

Start Microsoft Access and create a new database mydb.mdb in jdbc subdirectory

Control Panel - 32 bit odbc - create a new dsn - dsn1 , the driver will be MS Access - file c:\jdbc\mydb.mdb

---------------------------------------------

zzz.java

import java.sql.*;

public class zzz {

public static void main(String args[]) {

String url = "jdbc:odbc:dsn1";

Connection con;

String cStr;

cStr = "create table products " + "(pname varchar(32), prodid int, "

+ "price float)";

Statement stmt;

try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

} catch(java.lang.ClassNotFoundException e) {

System.err.print("ClassNotFoundException: ");

System.err.println(e.getMessage());

}

try {

con = DriverManager.getConnection(url,"uid", "pwd");

stmt = con.createStatement();

stmt.executeUpdate(cStr);

stmt.close();

con.close();

} catch(SQLException ex) {

System.err.println("SQLException: " + ex.getMessage());

}

}

}

javac zzz.java

java zzz

 

--------------------------------------------

yyy.java

import java.sql.*;

public class yyy {

public static void main(String args[]) {

String url = "jdbc:odbc:dsn1";

Connection con;

Statement stmt;

try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

} catch(java.lang.ClassNotFoundException e) {

System.err.print("ClassNotFoundException: ");

System.err.println(e.getMessage());

}

try {

con = DriverManager.getConnection(url,"Admin", "zzz");

stmt = con.createStatement();

stmt.executeUpdate("insert into products " +

"values('Gold', 00101, 7.99)");

stmt.executeUpdate("insert into products " +

"values('Silver', 00049, 8.99)");

stmt.close();

con.close();

} catch(SQLException ex) {

System.err.println("SQLException: " + ex.getMessage());

}

}

}

javac yyy.java

java yyy

----------------------------

xxx.java

import java.sql.*;

public class xxx {

public static void main(String args[]) {

String url = "jdbc:odbc:dsn1";

Connection con;

Statement stmt;

String query = "select pname, prodid, price from products";

try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

} catch(java.lang.ClassNotFoundException e) {

System.err.print("ClassNotFoundException: ");

System.err.println(e.getMessage());

}

try {

con = DriverManager.getConnection(url,"Admin", "zzz");

stmt = con.createStatement();

ResultSet rs = stmt.executeQuery(query);

System.out.println("Name ID Price");

while (rs.next()) {

String s = rs.getString("pname");

int i = rs.getInt("prodid");

float f = rs.getFloat("price");

System.out.println(s + " " + i + " " + f);

}

stmt.close();

con.close();

} catch(SQLException ex) {

System.err.println("SQLException: " + ex.getMessage());

}

}

}

javac xxx.java

java xxx

------------------------------------------