
i have connected my java code with the MS access database and this is my code, can anyone tell me how to show the table in an applet...pls
import java.sql.*;
public class TestDBDriver {
static Connection con;
static Statement stmt;
static ResultSet rs;
public static void main(String[] args) {
//step 1: load driver
loadDriver();
//step 3: establish connection
makeConnection();
//create a table
createTable();
//insert data
insertData();
//use precompiled statement to update data
usePreparedStatement();
//retrieve data
retrieveData();
//close all resources
closeAll();
}
// load a driver
static void loadDriver() {
try {
//step 2: Define connection URL
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
}
// make a connection step 3: establish connection
static void makeConnection() {
//for how to set up data source name see below.
String dsn = "judydriver";
String url =
"jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};DBQ=faltu.mdb";
// String url = "jdbc:odbc:" + dsn;
try {
con = DriverManager.getConnection(url);
}catch(SQLException ex) {
System.err.println("database connection: " + ex.getMessage());
}
}
//create a table
static void createTable() {
String createString = "create table COFFEES " +
"(COF_NAME VARCHAR(32), " +
"SUP_ID INTEGER, " +
"PRICE FLOAT, " +
"SALES INTEGER, " +
"TOTAL INTEGER)";
try {
//step 4: create a statement
stmt = con.createStatement();
//step 5: execute a query or update.
stmt.execute("drop table COFFEES");//if exists, drop it, get new one
stmt.executeUpdate(createString);
}catch(SQLException ex) {
System.err.println("CreateTable: " + ex.getMessage());
}
}
//insert data to table COFFEES
static void insertData() {
try {
stmt.executeUpdate("INSERT INTO COFFEES " + "VALUES ('Colombian', 101, 7.99, 0, 0)");
stmt.executeUpdate("INSERT INTO COFFEES " + "VALUES ('French_Roast', 49, 8.99, 0, 0)");
stmt.executeUpdate("INSERT INTO COFFEES " + "VALUES ('Espresso', 35, 5.99, 0, 0)");
stmt.executeUpdate("INSERT INTO COFFEES " + "VALUES ('Colombian_Decaf', 101, 4.99, 0, 0)");
stmt.executeUpdate("INSERT INTO COFFEES " + "VALUES ('French_Roast_Decaf', 49,6.99, 0, 0)");
}catch(SQLException ex) {
System.err.println("InsertData: " + ex.getMessage());
}
}
//use PreparedStatement to precompile sql statement
static void usePreparedStatement() {
try {
PreparedStatement updateSales;
String updateString = "update COFFEES " +
"set SALES = ? where COF_NAME like ?";
updateSales = con.prepareStatement(updateString);
int [] salesForWeek = {175, 150, 60, 155, 90};
String [] coffees = {"Colombian", "French_Roast", "Espresso",
"Colombian_Decaf", "French_Roast_Decaf"};
int len = coffees.length;
for(int i = 0; i < len; i++) {
updateSales.setInt(1, salesForWeek[i]);
updateSales.setString(2, coffees[i]);
updateSales.executeUpdate();
}
}catch(SQLException ex) {
System.err.println("UsePreparedStatement: " + ex.getMessage());
}
}
//retrieve data from table COFFEES
static void retrieveData() {
try {
String gdta="SELECT COF_NAME, PRICE FROM COFFEES WHERE PRICE < 9.00";
//step 6: process the results.
rs = stmt.executeQuery(gdta);
while (rs.next()) {
String s = rs.getString("COF_NAME");
float n = rs.getFloat("PRICE");
System.out.println(s + " " + n);
}
}catch(SQLException ex) {
System.err.println("RetrieveData: " + ex.getMessage());
}
}
//close statement and connection
//step 7: close connection, etc.
static void closeAll() {
try {
stmt.close();
con.close();
} catch(SQLException ex) {
System.err.println("closeAll: " + ex.getMessage());
}
}
}
Thanks;