Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Servlet connection

Here folks, I have the following codes which connects to a an access database directly and reads data, I want to do same connection in servlet or JSP, how do I do that
import java.sql.*;
public class Test

{
public static void main(String[] args)

{

try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// set this to a MS Access DB you have on your machine
String filename = "P:/javanew/mdbTEST.mdb";
String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
database+= filename.trim() + ";DriverID=22;READONLY=true}"; // add on to the end
// now we can get the connection from the DriverManager
Connection con = DriverManager.getConnection( database ,"","");
Statement s = con.createStatement();
//s.execute("create table TEST12345 ( column_name integer )"); // create a table
//s.execute("insert into TEST12345 values(1)"); // insert some data into the table
s.execute("select * from Protein"); // select the data from the table
ResultSet rs = s.getResultSet(); // get any ResultSet that came from our query
if (rs != null) // if rs == null, then there is no ResultSet to view
System.out.print("ProteinKey");
while ( rs.next() ) // this will step through our data row-by-row

{
/* the next line will get the first column in our current row's ResultSet
as a String ( getString( columnNumber) ) and output it to the screen */

System.out.println( rs.getString(1)+ " "+ rs.getString(2)+ " "+rs.getString(3)+ " "+rs.getString(4)+ " "+rs.getString(5) +" "+rs.getString(6) );
// System.out.println(ProteinKey + ". " + Author + ", " + ID + " (" + AC + ")");
}

s.execute("drop table Protein");
s.close(); // close the Statement to let the database know we're done with it
con.close(); // close the Connection to let the database know we're done with it
}

catch (Exception err) {
System.out.println("ERROR: " + err);
}

}

}
//save this code into a file called Test.java and compile it
[2394 byte] By [yaosman_1] at [2007-11-11 9:57:25]