tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From John <locu...@borg.apana.org.au>
Subject jdbc: can't connect to mysql server on port...
Date Thu, 02 Jan 2003 10:56:06 GMT
I'm running tomcat 4.0.3 on debian woody with apache and mysql on the
same box.  I can connect to mysql from another machine via tcp/ip on
port 3306, and from perl and php4 scripts.

When I try this code under tomcat, i get an error about 'can't find a
mysql server on port 3306'

%<-------------------------------------------------------
<HTML>
  <HEAD>
    <TITLE>JSP Test Code</TITLE>
  </HEAD>

  <BODY>

    <%@ page import="java.sql.*" %>

    <TABLE BORDER=1 WIDTH=75%>
      <TR>
        <TH>Last Name</TH>
	<TH>First Name</TH>
      </TR>

      <% 
      Connection conn = null;
      Statement  st   = null;
      ResultSet  rs   = null;

      try
      {
        Class.forName("org.gjt.mm.mysql.Driver").newInstance();
	conn = DriverManager.getConnection("jdbc:mysql://localhost/cartapp?user=root");
      }
      catch(Exception ex)
      {
        out.println("Exception handler!");
	out.println(ex);
      }
      finally
      {
        if(rs!=null)rs.close();
        if(st!=null)st.close();
        if(conn!=null)conn.close();
      }
    %>

  </BODY>
</HTML>

%<--------------------------------------------------------------------

but equivalent code (below) works just fine with pure java...

%<--------------------------------------------------------------------

import java.sql.*;

class HelloWorldApp {
    public static void main(String[] args) throws java.sql.SQLException{
        Connection conn = null;
	Statement  st   = null;
	ResultSet  rs   = null;

	try
	{
	  Class.forName("org.gjt.mm.mysql.Driver").newInstance();
	  conn = DriverManager.getConnection("jdbc:mysql://localhost/cartapp?user=root");
	  st   = conn.createStatement();
	  rs   = st.executeQuery("select * from employees");

	  while (rs.next()) {
	    System.out.println(rs.getString("lname_txt"));
	  }
	}
	catch (Exception ex)
	{
	  ex.printStackTrace();
	}
	finally
	{
	  if( rs   != null) rs.close();
	  if( st   != null) st.close();
	  if( conn != null) conn.close();
	}
	
        System.out.println("Hello World!"); // Display "Hello World!"
    }
}

%<--------------------------------------------------------------------

Has anyone had any similar issues, and can point me in the right direction?

-- 
Regards,

John Breen

+---------------------------------------------------------+-------------+
|  aaaa   pppppp   aaaa   n nnnn   aaaa   ww   ww  aaaa   |            
|
|     aa   pp   p     aa   nn  nn     aa  ww   ww     aa  | John Breen 
|
|  aaaaa   pp   p aaaaaa   nn  nn aaaaaa  ww w ww  aaaaa  | Coordinator
|
| a   aa   ppppp  a   aa   nn  nn a   aa  ww w ww a   aa  | APANA, Inc.
|
|  aaaa a  pp      aaaa a nnn nnn  aaaa a  wwwww   aaaa a | WA Region  
|
|         pppp                                            |            
|
+---------------------------------------------------------+-------------+
                      email: locutus@borg.apana.org.au


--
To unsubscribe, e-mail:   <mailto:tomcat-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:tomcat-user-help@jakarta.apache.org>


Mime
View raw message