M
Ming
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>
}
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>
}