Hi David,
Thanks a lot for your help.
I added the dependency to the geronimo-application.xml,
and then the next code works fine.
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = "jdbc:mysql:///hellodb?user=root&password=hello";
con = DriverManager.getConnection(url);
stmt = con.createStatement();
String sql = "SELECT * FROM PET";
rs = stmt.executeQuery(sql);
while (rs.next())
{
String name = this.getValue(rs, "NAME");
}
But the next code throws exception.
InitialContext ctx = new InitialContext();
ds = (DataSource)ctx.lookup("java:comp/env/jdbc/MyDataSource");
con = ds.getConnection("root", "hello");
stmt = con.createStatement();
String sql = "SELECT * FROM PET";
rs = stmt.executeQuery(sql);
I am trying to find out the reasons. Maybe I also need to
modify web.xml
and geronimo-web.xml.
Now I have two ways to access MySql database. One way is to setup
the EAR; another way is to setup WAR/web.xml and WAR/geronimo-web.xml.
Anyway it is great progress that I can access MySql database.
Thanks a lot.
javax.naming.NotContextException: jdbc/MyDataSource
at
org.apache.xbean.naming.context.AbstractContext.lookup
(AbstractContext.java:
167)
at
org.apache.xbean.naming.context.AbstractContext.lookup
(AbstractContext.java:
617)
at
org.apache.xbean.naming.context.AbstractContext.lookup
(AbstractContext.java:
158)
at
org.apache.xbean.naming.context.AbstractContext.lookup
(AbstractContext.java:
617)
at
org.apache.xbean.naming.context.AbstractContext.lookup
(AbstractContext.java:
158)
at
org.apache.xbean.naming.context.AbstractContext.lookup
(AbstractContext.java:
603)
at javax.naming.InitialContext.lookup(Unknown Source)
at
com.sn.web.db.mysql.demo.pet.ListPets.getConnectionByDataSource
(ListPets.jav
a:79)
at
com.sn.web.db.mysql.demo.pet.ListPets.LoadTableFromDatabase
(ListPets.java:24
8)
at com.sn.web.db.mysql.demo.pet.ListPets.LoadTable(ListPets.java:230)
at com.sn.web.db.mysql.demo.pet.ListPets.doGet(ListPets.java:47)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:693)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:806)
at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter
(Application
FilterChain.java:290)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter
(ApplicationFilterCh
ain.java:206)
at
org.apache.catalina.core.StandardWrapperValve.invoke
(StandardWrapperValve.ja
va:230)
at
org.apache.catalina.core.StandardContextValve.invoke
(StandardContextValve.ja
va:175)
at
org.apache.geronimo.tomcat.valve.DefaultSubjectValve.invoke
(DefaultSubjectVa
lve.java:56)
at
org.apache.geronimo.tomcat.GeronimoStandardContext
$SystemMethodValve.invoke(
GeronimoStandardContext.java:353)
at
org.apache.geronimo.tomcat.valve.GeronimoBeforeAfterValve.invoke
(GeronimoBef
oreAfterValve.java:47)
at
org.apache.catalina.core.StandardHostValve.invoke
(StandardHostValve.java:128
)
at
org.apache.catalina.valves.ErrorReportValve.invoke
(ErrorReportValve.java:104
)
at
org.apache.catalina.core.StandardEngineValve.invoke
(StandardEngineValve.java
:109)
at
org.apache.catalina.valves.AccessLogValve.invoke
(AccessLogValve.java:563)
at
org.apache.catalina.connector.CoyoteAdapter.service
(CoyoteAdapter.java:261)
at org.apache.jk.server.JkCoyoteHandler.invoke
(JkCoyoteHandler.java:190)
at org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:
283)
at org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:767)
at
org.apache.jk.common.ChannelSocket.processConnection
(ChannelSocket.java:697)
at
org.apache.jk.common.ChannelSocket$SocketConnection.runIt
(ChannelSocket.java
:889)
at
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run
(ThreadPool.jav
a:686)
at java.lang.Thread.run(Unknown Source)
----- Original Message -----
From: "David Jencks" <[EMAIL PROTECTED]>
To: <user@geronimo.apache.org>
Sent: Monday, January 07, 2008 12:04 PM
Subject: Re: Questions about MySql and Geronimo
On Jan 7, 2008, at 10:32 AM, jpmlpool wrote:
Hello
I am still struggling with Geronimo and MySql. I tried to change
the deploy plans following some articles, but I still can not access
MySql server from Geronimo.
Attached are related files, because I zipped Ear and War projects
created on Eclipse, but I cannot send it to this email address.
If you know my errors, please help me. At the bottom is my
previous
email with more details.
Environments or what I did
(1) Geronimo 2.0.2
(2) Database pool created from Geronimo Admin console
Database pool : QiMySql
Database : hellodb
a database Table : pet
(3) Eclipse 3.3.1 with WTP
(4) OS : Windows 2000
(5) SqlPetStoreEar and SqiPetStoreWar projects on Eclipse
Your application needs a dependency on the database pool you have set
up. Something like
====== SqlPetStoreEar/EarContent/METF-INF/geronimo-application.xml
======
<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://geronimo.apache.org/xml/ns/j2ee/
application-1.1"
xmlns:sec="http://geronimo.apache.org/xml/ns/security-1.1"
xmlns:sys="http://geronimo.apache.org/xml/ns/deployment-1.1"
application-name="SqlPetStoreEar">
<sys:environment>
<sys:moduleId>
<sys:groupId>com.sn.web.db.mysql.demo.pet</sys:groupId>
<sys:artifactId>MySqlPetStoreEarId</sys:artifactId>
<sys:version>1.0</sys:version>
<sys:type>ear</sys:type>
</sys:moduleId>
<sys:dependencies>
<sys:dependency>
<sys:groupId>console.dbpool</sys:groupId>
<sys:artifactId>QiMySql</sys:artifactId>
<sys:type>rar</sys:type>
</sys:dependency>
</sys:dependencies>
</sys:environment>
</application>
You should check that the dependency matches what the console comes
up with when it deploys the pool -- in particular I'm unsure of the
type.
Hope this helps
david jencks
John Qi
=============================
====== SqlPetStoreEar/EarContent/METF-INF/application.xml ======
<?xml version="1.0" encoding="UTF-8"?>
<application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:application="http://java.sun.com/xml/ns/javaee/
application_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/application_5.xsd"
id="Application_ID"
version="5">
<module>
<web>
<web-uri>SqlPetStoreWar.war</web-uri>
<context-root>SqlPetStoreWar</context-root>
</web>
</module>
</application>
====== SqlPetStoreEar/EarContent/METF-INF/geronimo-application.xml
======
<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://geronimo.apache.org/xml/ns/j2ee/
application-1.1"
xmlns:sec="http://geronimo.apache.org/xml/ns/security-1.1"
xmlns:sys="http://geronimo.apache.org/xml/ns/deployment-1.1"
application-name="SqlPetStoreEar">
<sys:environment>
<sys:moduleId>
<sys:groupId>com.sn.web.db.mysql.demo.pet</sys:groupId>
<sys:artifactId>MySqlPetStoreEarId</sys:artifactId>
<sys:version>1.0</sys:version>
<sys:type>ear</sys:type>
</sys:moduleId>
</sys:environment>
</application>
====== SqlPetStoreWar/WebContent/WEB-INF/geronimo-web.xml ======
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://geronimo.apache.org/xml/ns/j2ee/web-1.1"
xmlns:nam="http://geronimo.apache.org/xml/ns/naming-1.1"
xmlns:sec="http://geronimo.apache.org/xml/ns/security-1.1"
xmlns:sys="http://geronimo.apache.org/xml/ns/deployment-1.1">
<sys:environment>
<sys:moduleId>
<sys:groupId>com.sn.web.db.mysql.demo.pet</sys:groupId>
<sys:artifactId>SqlPetStoreWarId</sys:artifactId>
<sys:version>1.0</sys:version>
<sys:type>war</sys:type>
</sys:moduleId>
</sys:environment>
<context-root>/SqlPetStoreWar</context-root>
</web-app>
====== SqlPetStoreWar/EarContent/WEB-INF/web.xml ======
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID"
version="2.5">
<display-name>SqlPetStoreWar</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>ListPets</display-name>
<servlet-name>ListPets</servlet-name>
<servlet-class>com.sn.web.db.mysql.demo.pet.ListPets</servlet-
class>
</servlet>
<servlet-mapping>
<servlet-name>ListPets</servlet-name>
<url-pattern>/ListPets</url-pattern>
</servlet-mapping>
</web-app>
================ ListPets.java ===============
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
{
// TODO Auto-generated method stub
this.getPets(request, response);
}
private void getPets(HttpServletRequest request,
HttpServletResponse
response)
{
Statement stmt = null;
ResultSet rs = null;
try
{
InitialContext ctx = new InitialContext();
// error : can not find this data source
DataSource ds = (DataSource)ctx.lookup("java:comp/env/QiMySql");
// error : this line does not work on Geronimo, but works fine
in java
application.
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = "jdbc:mysql:///hellodb?user=root&password=hello";
Connection con = DriverManager.getConnection(url);
stmt = con.createStatement();
String sql = "SELECT * FROM PET";
rs = stmt.executeQuery(sql);
while (rs.next())
{
String no = rs.getString("NAME");
String lang = rs.getString("OWNER");
String msg = rs.getString("SEX");
System.out.println(no + " " + lang + " " + msg);
}
stmt.close();
con.close();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (rs != null)
{
try
{
rs.close();
}
catch (SQLException sqlEx)
{
}
}
if (stmt != null)
{
try
{
stmt.close();
}
catch (SQLException sqlEx)
{
}
}
rs = null;
stmt = null;
}
}
=============================
----- Original Message -----
From: "jpmlpool" <[EMAIL PROTECTED]>
To: <user@geronimo.apache.org>
Sent: Wednesday, January 02, 2008 9:40 AM
Subject: question about MySql and Geronimo
Happy New Year !
What I want to do are
(1) when I start Geronimo, MySql server is started automatically by
Geronimo.
(2) I can access MySql, just like now I can access Derby.
(3) if possible, for example, there are two Derbys and two MySqls,
I can
access them for different purpose. I do not know if this is
possible,
if
possible, HOW TO DO.
What I did are
(1) downloaded MySql 5.0.54 and installed it. (OK)
(2) from console I can access the MySql. (OK)
mysql> show tables;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| pet |
| wishtable |
+-------------------+
2 rows in set (0.00 sec)
mysql> SELECT * FROM pet;
+----------+-------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+----------+-------+---------+------+------------+------------+
| Fluffy | | Harold | c | 0000-00-00 | 1993-02-04 |
| Claws | | Gwen | c | 0000-00-00 | 1994-03-17 |
| Buffy | | Harold | d | 0000-00-00 | 1989-05-13 |
| Fang | | Benny | d | 0000-00-00 | 1990-08-27 |
| Bowser | | Diane | d | 0000-00-00 | 1979-08-31 |
| Chirpy | | Gwen | b | 0000-00-00 | 1998-09-11 |
| Whistler | Gwen | bird | | 1997-12-09 | NULL |
| Slim | | Benny | s | 0000-00-00 | 1996-04-29 |
+----------+-------+---------+------+------------+------------+
8 rows in set (0.00 sec)
(3) downloaded the MySql driver mysql-connector-java-3.1.12.jar
through Geronimo's console.
the driver is placed in repository\mysql\mysql-connector-java
\3.1.12
(4) From Eclipse I created a Java Application, and I can
access the MySql using Java code below. The results are
correct.
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = "jdbc:mysql:///hellodb?user=abc&password=xyz";
Connection con = DriverManager.getConnection(url);
(5) I created a Dynamic web project and a servlet, and used the
same code in the servlet as above, but I got next error
message.
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver in
classloader
com.sn.web.db.mysql.demo.pet/SqlDemoPetStoreId/1.0/war
at
org.apache.geronimo.kernel.config.MultiParentClassLoader.loadOptimize
d
Class(
MultiParentClassLoader.java:428)
at
org.apache.geronimo.kernel.config.MultiParentClassLoader.loadClass
(MultiPare
ntClassLoader.java:278)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at com.sn.web.db.mysql.demo.pet.ListPets.getPets(ListPets.java:82)
at com.sn.web.db.mysql.demo.pet.ListPets.doGet(ListPets.java:43)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:693)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:806)
.....
Do you have any ideas ?
Thanks in advance.
John Qi