0

hi iam working on a project and i want to sent and access data in MS Access using jsp so,

Please tell me how can i do this

2
Contributors
1
Reply
3
Views
7 Years
Discussion Span
Last Post by musthafa.aj
0
<html>
 <head>
   <title>JSP MS Access Example</title>
 </head>
 <body>
   <%@ page import="javax.naming.*" %>
   <%@ page import="java.sql.*" %>
   <%@ page import="javax.sql.*" %>

   <h1>JSP MS Access Example</h1>

   <%

   Connection conn = null;
   Statement stmt = null;
   ResultSet rs = null;

   try {

       // Obtain our environment naming context
       Context initCtx = new InitialContext();
       Context envCtx = (Context) initCtx.lookup("java:comp/env");

       // Look up our data source
       DataSource ds = (DataSource) envCtx.lookup("jdbc/MyDB");

       // Allocate and use a connection from the pool
       conn = ds.getConnection();

       // Fetch and display data
       stmt = conn.createStatement();

       // You need to edit this query
       rs = stmt.executeQuery("SELECT CompanyName FROM suppliers");

       while (rs.next()) {
           // You need to edit this column name
           String s = rs.getString("CompanyName");
           out.print(s + "<br>");
       }

       rs.close();
       rs = null;
       stmt.close();
       stmt = null;
       conn.close(); // Return to connection pool
       conn = null;  // Make sure we do not close it twice
   } catch (SQLException e) {
       out.print("Throw e" + e);
   } finally {
     // Always make sure result sets and statements are closed,
     // and the connection is returned to the pool
     if (rs != null) {
       try { rs.close(); } catch (SQLException e) { ; }
       rs = null;
     }
     if (stmt != null) {
       try { stmt.close(); } catch (SQLException e) { ; }
       stmt = null;
     }
     if (conn != null) {
       try { conn.close(); } catch (SQLException e) { ; }
       conn = null;
     }
   }

   %>

 </body>
</html>
Votes + Comments
Ouch, database connectivity from JSP instead of servlet, bad, bad...
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.