Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package database;
- import java.io.*;
- import java.util.*;
- import javax.servlet.*;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.*;
- import java.sql.*;
- @SuppressWarnings("serial")
- @WebServlet("/DatabaseAccess")
- public class DatabaseAccess extends HttpServlet{
- static final String JDBC_DRIVER= "net.ucanaccess.jdbc.UcanaccessDriver";
- static final String DB_URL = "jdbc:ucanaccess://C:/Users/bkintzing/tomworkspace/database/test.accdb";
- static final String USER = "root";
- static final String PASS = "password";
- Connection conn;
- Statement stmt;
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- processRequest(req, resp);
- }
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- processRequest(req, resp);
- }
- public void processRequest(HttpServletRequest request,
- HttpServletResponse response)
- throws ServletException, IOException{
- response.setContentType("text/html");
- PrintWriter out = response.getWriter();
- String title = "Database Result";
- String docType =
- "<!doctype html public \"-//w3c//dtd html 4.0 " +
- "transitional//en\">\n";
- out.println(docType +
- "<html>\n" +
- "<head><title>" + title + "</title></head>\n" +
- "<body bgcolor=\"#f0f0f0\">\n" +
- "<h1 align=\"center\">" + title + "</h1>\n");
- try{
- Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
- conn = DriverManager.getConnection(DB_URL,USER,PASS);
- stmt = conn.createStatement();
- String sql;
- sql = "SELECT id, first, last, age, FROM Employees";
- ResultSet rs = stmt.executeQuery(sql);
- while(rs.next()){
- int id = rs.getInt("id");
- int age = rs.getInt("age");
- String first = rs.getString("first");
- String last = rs.getString("last");
- out.println("ID: " + id + "<br>");
- out.println(", Age: " + age + "<br>");
- out.println(", First: " + first + "<br>");
- out.println(", Last: " + last + "<br>");
- }
- out.println("</body></html>");
- rs.close();
- stmt.close();
- conn.close();
- }
- catch(Exception ex){
- ex.printStackTrace();
- }
- finally{
- try{
- if(stmt!=null)
- stmt.close();
- }
- catch(Exception ex2){
- ex2.printStackTrace();
- }
- try{
- if(conn!=null)
- conn.close();
- }
- catch(SQLException se){
- se.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement