Jndi, Tomcat, Mysql settup and Servlet test

Discussion in 'Java' started by Ming, Nov 18, 2007.

  1. Ming

    Ming Guest

    Tomcat version 5.5.25

    I am trying to set up and test mysql jndi connection but cannot make
    it.

    Can only display:
    Servlet NewServlet at /BeerV1
    000000000000000000000000000
    111111111111111111111111111

    Why Statement st = con.createStatement(); and ResultSet rs =
    st.executeQuery("select * from users"); cannot be executed?

    Thanks


    ****************Code Snippets**********************

    Code snippet from $tomcat/conf/server.xml

    <GlobalNamingResources>
    <Environment
    name="simpleValue"
    type="java.lang.Integer"
    value="30"/>
    <Resource
    name="jdbc/test"
    type="javax.sql.DataSource"
    maxActive="4"
    maxIdle="2"
    username="test"
    maxWait="5000"
    driverClassName="com.mysql.jdbc.Driver"
    validationQuery="select * from users"
    password="test"
    url="jdbc:mysql://localhost:3306/test/>
    <Resource
    auth="Container"
    description="User database that can be updated and saved"
    name="UserDatabase"
    type="org.apache.catalina.UserDatabase"
    factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
    pathname="conf/tomcat-users.xml"/>
    </GlobalNamingResources>

    Code from $myapplication/WEB-INF/web.xml

    <description>MySQL Test App</description>
    <resource-ref>
    <description>DB Connection</description>
    <res-ref-name>jdbc/test</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    </resource-ref>

    Test code:

    import java.io.*;
    import java.net.*;

    import javax.servlet.*;
    import javax.servlet.http.*;

    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.naming.NameClassPair;
    import javax.naming.NamingEnumeration;
    import javax.naming.NamingException;
    import javax.sql.DataSource;

    public class NewServlet extends HttpServlet {

    /**
    * Processes requests for both HTTP <code>GET</code> and
    <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    protected void processRequest(HttpServletRequest request,
    HttpServletResponse response)
    throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {

    out.println("<html>");
    out.println("<head>");
    out.println("<title>Servlet NewServlet</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<h1>Servlet NewServlet at " +
    request.getContextPath () + "</h1>");
    try {
    // TODO code application logic here

    Context ctx = new InitialContext();
    out.println("000000000000000000000000000<br>");

    DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/
    test");
    out.println("111111111111111111111111111<br>");

    Connection con = ds.getConnection();
    out.println("222222222222222222222222222<br>");

    Statement st = con.createStatement();
    out.println("333333333333333333333333333<br>");

    ResultSet rs = st.executeQuery("select * from users");
    out.println("444444444444444444444444444<br>");

    while (rs.next()) {
    out.println(rs.getString("first_name"));
    }
    rs.close();
    st.close();
    con.close();
    }
    catch (SQLException ex) {

    Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null,
    ex);
    } catch (NamingException ex) {

    Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null,
    ex);
    }

    out.println("</body>");
    out.println("</html>");

    } finally {
    out.close();
    }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet
    methods. Click on the + sign on the left to edit the code.">
    /**
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request,
    HttpServletResponse response)
    throws ServletException, IOException {
    processRequest(request, response);
    }

    /**
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request,
    HttpServletResponse response)
    throws ServletException, IOException {
    processRequest(request, response);
    }

    /**
    * Returns a short description of the servlet.
    */
    public String getServletInfo() {
    return "Short description";
    }
    // </editor-fold>
    }
    Ming, Nov 18, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sean Clarke
    Replies:
    1
    Views:
    1,881
    Sudsy
    Jan 7, 2004
  2. circuit_breaker
    Replies:
    2
    Views:
    1,989
    Jack Jia
    Apr 4, 2004
  3. Mike S
    Replies:
    0
    Views:
    22,987
    Mike S
    Sep 16, 2004
  4. Mick
    Replies:
    2
    Views:
    9,503
    Marcin Grunwald
    Feb 23, 2005
  5. Mer Gilmartin

    mysql ruby settup

    Mer Gilmartin, Aug 28, 2006, in forum: Ruby
    Replies:
    7
    Views:
    122
    Mer Gilmartin
    Sep 4, 2006
Loading...

Share This Page