jQuery in Action, 3rd edition
The moose likes Tomcat and the fly likes Tomcat6, MySQL and a Servlet - Problem: NameAlreadyBoundException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Tomcat6, MySQL and a Servlet - Problem: NameAlreadyBoundException " Watch "Tomcat6, MySQL and a Servlet - Problem: NameAlreadyBoundException " New topic

Tomcat6, MySQL and a Servlet - Problem: NameAlreadyBoundException

Florian Muellerer

Joined: Oct 17, 2012
Posts: 2

A nice Hello to everybody here!

I tried for days now, to enable a connection to mysql with a Servlet on Tomcat 6.
What I already have:
Debian Squeeze, Apache2, Tomcat 6, MySQL example-database with
an user "peter", and his password "something" and
a Database called "datenbank1" with a table called "personen" and some textdata in it.
mysql is bound to (localhost) and the port 3306

I already copied mysql-connector-java-5.1.22-bin.jar to ~TOMCAT/lib
and did there this:
chmod 777 mysql-connector-java-5.1.22-bin.jar
chown root mysql-connector-java-5.1.22-bin.jar
chgrp root mysql-connector-java-5.1.22-bin.jar

then I installed the mysql-java with apt-get
and applied ini /etc/tomcat6/catalina.properties

restarted tomcat
--------------------- my servlet :
to make a war-file i set up the folder:
- - sqltest
- - - META-INF
- - - WEB-INF
- - - - - classes

My context.xml:

<Resource name="jdbc/meinedb" auth="Container" type="javax.sql.DataSource"
maxActive="50" maxIdle="30" maxWait="10000"
username="peter" password="something"


My web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

<description>DB Connection</description>

<description>Zeigt personen aus datenbank1 an</description>


My Servlet "EinTestServlet.java"


i compile the java with:
javac -cp /usr/share/tomcat6/lib/servlet-api.jar EinTestServlet.java

i build the war-file with ../sqltest/jar -cvf meintest.war .

I deploy the war-file with the tomcat-manager and I could run it.
but there are Errors: (tomcat..logs/catalina.out)

17.10.2012 13:33:07 org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive sqlbsp.war
17.10.2012 13:33:07 org.apache.catalina.core.NamingContextListener addResource
SCHWERWIEGEND: Failed to bind object: javax.naming.NameAlreadyBoundException: Name meinedb is already bound in this Context
java.sql.SQLException: Invalid value for getInt() - 'B7C5KKLJ901'
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2762)
at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2850)
at org.apache.commons.dbcp.DelegatingResultSet.getInt(DelegatingResultSet.java:275)
at org.apache.commons.dbcp.DelegatingResultSet.getInt(DelegatingResultSet.java:275)
at EinTestServlet.doGet(EinTestServlet.java:53)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:636)
17.10.2012 13:34:27 org.apache.catalina.startup.HostConfig checkResources
INFO: Undeploying context [/sqlbsp]

Please, can someone find the error in my source?
i try for some days now to get the stuff done, i tried already different konds of context and web files and a lot of codes - nothing seems to run. i dont see the error in my code.

G krishna kumar

Joined: Oct 10, 2012
Posts: 14
java.sql.SQLException: Invalid value for getInt() - 'B7C5KKLJ901' ....

check here what is value you are trying to supply....

or can you post what the values are you passing.

Florian Muellerer

Joined: Oct 17, 2012
Posts: 2

Thank You Mr. Kumar!
That was the right Advice.

I changed the code in the while-loop as followed:

And it works!

Because, the ID seems not to be a int-Number, its a mixed type and the rest was also able to be read as a String.
I thought all the time, there must something wrong with the Connection!

Thanks for this advice.
I agree. Here's the link: http://aspose.com/file-tools
subject: Tomcat6, MySQL and a Servlet - Problem: NameAlreadyBoundException
It's not a secret anymore!