here's a JDBC class:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.util.*;
public class JDBCconn2 extends HttpServlet
{
int iNumCols;
int iNumRows;
PrintWriter out;
Object value[];
String requestParam;
public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
{
String url;
Connection con;
Statement stmt;
ResultSet resultset;
out = response.getWriter();
response.setContentType("text/html");
requestParam = request.getParameter("hidRandomNum");
//out.println("mstest2 db");
url = "jdbc:odbc:MSTest2";
/*
String createString;
createString = "create table COFFEES " +
"(COF_NAME varchar(32), " +
"SUP_ID int, " +
"PRICE float, " +
"SALES int, " +
"TOTAL int)";
*/
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(java.lang.ClassNotFoundException e)
{
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
out.println(e.getMessage());
}
try
{
//con = DriverManager.getConnection(url,"Admin","duke1");
con = DriverManager.getConnection(url);
stmt = con.createStatement();
//stmt.executeUpdate("SELECT * FROM TableRND"); //this would update
the db if you were adding a table like from above.
resultset = stmt.executeQuery("SELECT * FROM TableRND");
displayTheResults(resultset);
stmt.close();
con.close();
}
catch(SQLException ex)
{
System.err.println("SQLException: " + ex.getMessage());
}
}
private void displayTheResults(ResultSet rs)
{
String strDBNumber;
try
{
//get some metadata: columns names, etc about the results
ResultSetMetaData metadata = rs.getMetaData();
iNumCols = metadata.getColumnCount();
iNumRows=1; //keep track of row count
//the arrays are NOT zero based
/*
for(int j=1;j<=iNumCols;j++)
{
out.println(metadata.getColumnLabel(j));
out.println(metadata.getColumnDisplaySize(j));
}
//out.println("\n");
*/
out.println("<!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.0
Transitional//EN>\n");
out.println("<HTML>");
out.println(" <HEAD>");
out.println(" </HEAD>");
out.println(" <BODY>");
out.println("<br>");
out.println("the hidden field random number is: " + requestParam);
out.println("<br>");
//out.println("the database number is: " +
rs.getObject("iRandNum").toString().trim());
out.println("the database number is: ");
rs.next();
strDBNumber = rs.getObject("iRandNum").toString().trim();
out.println(strDBNumber);
out.println("<br>");
if(requestParam != null && strDBNumber != null)
try
{
if(Integer.parseInt(strDBNumber) ==
Integer.parseInt(requestParam))
out.println("The numbers match");
else
out.println("The numbers don't match");
}
catch(NumberFormatException e)
{
out.println("<br>The argument you specify must be an integer.");
}
catch(Exception e)
{
out.println("<br>The exception is: " + e);
}
else
out.println("the requestParam or strDBNumber was null.");
/*
out.println(" <TABLE BORDER=2>");
//rows
while(rs.next())
{
out.println(" <TR>");
//columns
for(int c=1;c<=iNumCols;c++)
{
out.println(" <TD>\n");
out.println(rs.getObject(c).toString().trim());
out.println(" </TD>");
}
out.println(" </TR>");
}
out.println(" </TABLE>");
*/
}
catch(SQLException ex)
{
System.err.println("SQLException: " + ex.getMessage());
}
out.println(" </BODY>");
out.println("</HTML>");
}
public void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
{
doGet(request,response);
}
}
-----Original Message-----
From: Guilherme Zambon [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, March 14, 2001 5:29 PM
To: [EMAIL PROTECTED]; josé placide
Subject: MYSQL CONNECTION CLASS
Can someone who uses TOMCAT send me a sample class that conects to the
database? I have made one that initializes de driver to connect and returns
the connection, so it is very slow. What kind of solution do you have?