Connecting database using Websphere Datasource


protected void doGet(HttpServletRequest request,
          HttpServletResponse response) throws ServletException, IOException 
{
          response.setContentType("text/html");
          PrintWriter out = response.getWriter();
          out.println("<HTML><HEAD><TITLE>");
          out.println("WAS + MySQL Example");
          out.println("</TITLE></HEAD><BODY>");
          out.println("<H1>WAS + MySQL Example</H1>");
          try {
               Context ctx = new InitialContext();
               DataSource ds = (DataSource) ctx
                         .lookup("java:comp/env/jdbc/mysqldsref");
               Connection connection = ds.getConnection();
               Statement stmt = connection.createStatement();
               ResultSet resultSet = stmt.executeQuery("select * from testtbl");
               while (resultSet.next()) {
                    String ssn = resultSet.getString("SSN");
                    String lastName = resultSet.getString("LASTNAME");
                    String firstName = resultSet.getString("FIRSTNAME");
                    out.print(ssn + ": ");
                    out.print(firstName + " ");
                    out.println(lastName + "<BR>");
               }
          } catch (NamingException e) {
               e.printStackTrace();
          } catch (SQLException e) {
               e.printStackTrace();
          }
          out.println("</BODY></HTML>");
          out.close();
}

More info
http://www.devx.com/Java/Article/31571/1954

Advertisements