Here is a snipet from a class I wrote in Java which interfaces with MS
Access. Most of it is probably gobbledeegupe for your purposes. But
there is a connection string in here that might be of interest to you,
and I am also using the Sun ODBC driver to connect to Access. Note:
you have to create a DSN for this to work. The rest of the variables
are just Java objects for Recordsets and Query statements. Also, a lot
of the code lines here are wordwrapping. In Java, the end of a code
line is defined by a semicolon ";", and routines are enclosed in curly
braces {}.
package bugs3;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.net.*;
import java.sql.*;
public class DBFunctionServ extends HttpServlet {
private static final String CONTENT_TYPE = "text/html";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
doPost(request, response);
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
String btn = request.getParameter("btn");
String priority = request.getParameter("priority");
String status = request.getParameter("status");
String hours = request.getParameter("hours");
int hour = 0;
int submitterID = 0;
int rowcount = 0;
if(hours == null)
hours = "0";
try{
hour = Integer.parseInt(hours);
}catch(NumberFormatException nfe){}
String description = request.getParameter("description");
String submitter = request.getParameter("submitter");
if(submitter == null)
submitter = "0";
try{
submitterID = Integer.parseInt(submitter);
}catch(NumberFormatException nfe){}
if(priority == null)
priority = "";
if(status == null)
status = "";
if(description == null)
description = "";
String bgcolor = null;
Cookie[] cookies = request.getCookies();
if(cookies != null){
for (int i=0; i<cookies.length; i++ ){
Cookie cookie = cookies[i];
if (cookie.getName().equals("bgcolor") ){
bgcolor = cookie.getValue();
System.out.println("bgcolor is " + bgcolor);
}
}
}
//BugDB is the ODBC DSN set in control pannel
String dsn = "jdbc:odbc:;DRIVER=Microsoft Access Driver
(*.mdb);DBQ=BugsDB";
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
out.println("<html>");
out.println("<head><title>DBFunctionServ</title></head>");
if(bgcolor != null)
out.println("<body bgcolor=" + bgcolor + ">");
else
out.println("<body>");
try{
Class.forName(driver);
Connection con;
con = DriverManager.getConnection(dsn);
Statement stmt =
con.createStatement(ResultSet.TYPE_SCROLL_INSENSIT IVE,ResultSet.CONCUR_R
EAD_ONLY);
if(btn.equals("Display") || btn.equals("Find"))
{
String sql = "SELECT * FROM BugsTbl";
if(btn.equals("Find"))
sql = "SELECT * FROM BugsTbl WHERE Priority = '" + priority +
"'";
ResultSet rs = stmt.executeQuery(sql);
//rs.setFetchDirection(rs.TYPE_SCROLL_INSENSITIVE);
rs.last();
rowcount = rs.getRow();
rs.beforeFirst();
ResultSetMetaData md = rs.getMetaData();
int nFields = md.getColumnCount();
//out.println("<html>");
//out.println("<head><title>DBFunctionServ</title></head>");
//if(bgcolor != null)
// out.println("<body bgcolor=" + bgcolor + ">");
//else
// out.println("<body>");
out.println("column count is " + nFields + "<br><br>");
out.println("<table border=1>");
for(int i = 1; i <= nFields; i++)
{
String str1 = md.getColumnName(i);
System.out.print(" *" + str1 + "* ");
out.println("<th>" + str1 + "</th>");
}
out.println("<th>RowCount</th>");
System.out.println("\n");
while(rs.next())
{
out.println("<tr>");
for(int i=1; i <= nFields; i++)
{
String str1 = rs.getString(i);
out.println("<td>" + str1 + "</td>");
}
out.println("<td>" + rs.getRow() + "</td>");
out.println("</tr>");
}
//int rowcount = rs.getRow();
out.println("</table><br>");
if(btn.equals("Find")){
if(priority.equals(""))
out.println("no records to display for selected priority
parameter of *" + priority + "*");
else
out.println("<br>Count of rows is " + rowcount + " for the
priority parameter of *" + priority + "*");
}else
out.println("Count of records is " + rowcount);
}
else
{
if(btn.equals("Add")){
stmt.executeUpdate("INSERT INTO BugsTbl(Priority, Status,
Hours, Description, SubmitterID) "
+ "VALUES('" + priority + "', '" + status +
"', " + hour + ", '"
+ description + "', " + submitterID + ")");
out.println("record added");
}
if(btn.equals("Remove")){
stmt.executeUpdate("DELETE FROM BugsTbl WHERE Hours = " +
hour);
out.println("Record removed for Hours = " + hour);
}
if(btn.equals("Update")){
stmt.executeUpdate("Update BugsTbl Set Description = '" +
description + "' WHERE BugsID = " + hour );
out.println("Record updated for BugsID = " + hour);
}
}
//out.println("<p>The servlet has received a POST. This is the
reply.</p>");
//out.println("</body></html>");
con.close();
System.out.println("btn is " + btn);
}catch(Exception e){e.printStackTrace();}
//out.println("<p>The servlet has received a POST. This is the
reply.</p>");
out.println("</body></html>");
}
//Clean up resources
public void destroy() {
}
}
Rich
*** Sent via Devdex
http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!