access database (newbie)

hi all, first time servelt and it's going ok.

I just have this minor problem when trying to connect the db:

import java.io.*;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import javax.servlet.http.*;

import javax.servlet.*;

publicclass HelloServletextends HttpServlet

{

private String url;

private Connection con;

publicvoid doGet (HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException

{

PrintWriter out = res.getWriter();

out.println("Hello, Brave new World 3 3523!");

try{

Class.forName("com.mysql.jdbc.Driver").newInstance();

url ="jdbc:mysql://"+getHost()+"/usgroups_db";

con = DriverManager.getConnection(url,"root","topsecret");

out.println("i'm done");

}catch(SQLException sqlex)

{

out.println("err 1");

}catch(Exception ex)

{

out.println("the value of getHost is: "+this.getHost());

out.println("err 2:"+ex.getMessage());

}

out.println("done!");

out.close();

}

public String getHost()

{

return"localhost";

}

}

the result is:

Hello, Brave new World 3 5423!

the value of getHost is: localhost

err 2:com.mysql.jdbc.Driver

done!

can someone guide me why the conn fails?

I added the mysql jar file to the project (using eclipse)

thanks

peter

[2748 byte] By [P_Primrosea] at [2007-10-2 4:37:48]
# 1
1) make sure db driver jar file under WEB-INF/ lib2) better u print stack trace for error u get :)and google with error code :)
what.why.howa at 2007-7-16 0:10:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

thanks

>> make sure db driver jar file under WEB-INF/ lib

yes, now i get an sql exception:

Hello, Brave new World 20xbx!

Hello, Brave 22

err 1:

Unable to connect to any hosts due to exception: java.security.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:3306 connect,resolve)

** BEGIN NESTED EXCEPTION **

java.security.AccessControlException

MESSAGE: access denied (java.net.SocketPermission 127.0.0.1:3306 connect,resolve)

STACKTRACE:

java.security.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:3306 connect,resolve)

at java.security.AccessControlContext.checkPermission(Unknown Source)

at java.security.AccessController.checkPermission(Unknown Source)

at java.lang.SecurityManager.checkPermission(Unknown Source)

at java.lang.SecurityManager.checkConnect(Unknown Source)

at java.net.Socket.connect(Unknown Source)

at java.net.Socket.connect(Unknown Source)

at java.net.Socket.<init>(Unknown Source)

at java.net.Socket.<init>(Unknown Source)

at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:124)

at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:225)

at com.mysql.jdbc.Connection.createNewIO(Connection.java:1783)

at com.mysql.jdbc.Connection.<init>(Connection.java:450)

at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:411)

at java.sql.DriverManager.getConnection(Unknown Source)

at java.sql.DriverManager.getConnection(Unknown Source)

at HelloServlet.doGet(HelloServlet.java:23)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)

at org.apache.catalina.core.ApplicationFilterChain.access$000(ApplicationFilterChain.java:98)

at org.apache.catalina.core.ApplicationFilterChain$1.run(ApplicationFilterChain.java:176)

at java.security.AccessController.doPrivileged(Native Method)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:172)

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)

at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)

at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)

at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)

at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)

at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)

at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)

at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2417)

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)

at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)

at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)

at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)

at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)

at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)

at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)

at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)

at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)

at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)

at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:193)

at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:781)

at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:549)

at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:589)

at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:666)

at java.lang.Thread.run(Unknown Source)

** END NESTED EXCEPTION **

done!

P_Primrosea at 2007-7-16 0:10:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Did you specify the correct username and password?

Did you 'grant' the user access to the database tables (may need to do this for localhost and remote access, e.g.

GRANT SELECT,INSERT,UPDATE,DELETE ON MYDB.MYTABLE TO 'myuser'@'localhost' IDENTIFIED BY 'password, myuser'@'%' IDENTIFIED BY 'password';

SteveNaivea at 2007-7-16 0:10:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Slightly off topic but there are a few other things worth pointing out...

private String url;

private Connection con;

:

:

url = "jdbc:mysql://"+getHost()+"/usgroups_db";

con = DriverManager.getConnection(url, "root", "topsecret");

Servlets are not automatically threadsafe. Mutliple requests from different users can re-use the same servlet instance concurrently. If you have two request in close proximity, request B could overwrite the connection created by request A causing all sorts of nastiness. I know it's just a HelloWorld servet, but thought it was worth pointing out in case you didn't realise.

And you should always close your connection in a finally block to avoid connection leaks.

SteveNaivea at 2007-7-16 0:10:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
Verify that the database is actually running on localhost and accepting requests on the specified port. telnet comes in quite handy for this test.
cknelsena at 2007-7-16 0:10:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

1. the database is actually running on localhost and accepting requests on the specified port. (when query through a java application - i get the result).

2. Did you specify the correct username and password?

yes, i did

3. the problem still exist. anyone?

thanks

peter

P_Primrosea at 2007-7-16 0:10:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

1. the database is actually running on localhost and accepting requests on the specified port. (when query through a java application - i get the result).

2. Did you specify the correct username and password?

yes, i did

3. the problem still exist. anyone?

thanks

peter

P_Primrosea at 2007-7-16 0:10:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8

java.security.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:3306 connect,resolve)

This is not involved with the database. It means that java has recognized that you are attempting to connect to something that the JVM says you have no rights to. In this case it looks like the socket.This exception normally occurs on applets but in this case for some reason I believe your server says you have no rights to connect from the servlet to 127.0.0.1:3306. Why I don't know. Anyone else got any ideas.

MexicoClivea at 2007-7-16 0:10:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...