aspose file tools*
The moose likes JDBC and the fly likes Data Source Connection with MySQL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Data Source Connection with MySQL" Watch "Data Source Connection with MySQL" New topic
Author

Data Source Connection with MySQL

Jeanne Harris
Greenhorn

Joined: May 07, 2012
Posts: 3
I have been working my way through the book "Beginning Java EE 5" using eclipse and jboss with mysql. In Chap 7 they have an example that is using a DataSource and connection in a JSP. I want to have it connect to MySQL database. I created a database called DataSourceExample and added a table to it and two records.

I wanted to attaching the original DataSourceExample.jsp files but they are not allowed.

My question is what do I change to connect to the MySQL database?

Part of the code from DataSourceExample.jsp

<% InitialContext context = new InitialContext();
DataSource dataSource =
(DataSource) context.lookup("java:comp/env/jdbc/DataSourceExample");
Connection conn = null;
Statement stmt = null;
ResultSet rset = null;
try {
conn = dataSource.getConnection();

Here is the original web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5" >
<display-name>DataSourceExample</display-name>
<servlet>
<display-name>DataSourceExample</display-name>
<servlet-name>DataSourceExample</servlet-name>
<jsp-file>/DataSourceExample.jsp</jsp-file>
</servlet>
<resource-ref>
<res-ref-name>jdbc/DataSourceExample</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
<welcome-file-list>
<welcome-file>DataSourceExample.jsp</welcome-file>
</welcome-file-list>
</web-app>
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 859

you need a ???-ds.xml (datasource xml file) which contains the physical server address port, userid password etc.

something like (This is DB2): (Provide your own values for $VALUES$)


WP
Jeanne Harris
Greenhorn

Joined: May 07, 2012
Posts: 3
I am running with JBOSS 7.0

I created the file called my-ds.xml and put it into the WebContent\WEB-INF directory.

<datasources>
<local-tx-datasource>
<jndi-name>jdbc/DataSourceExample</jndi-name>
<connection-url>
jdbc:mysql://localhost:3306/DataSourceExample
</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>root</user-name>
<password>my password</password>
</local-tx-datasource>
</datasources>

When I run I get this error message.

Here is the whole jsp file.



07:08:23,802 INFO [org.jboss.as.deployment] (DeploymentScanner-threads - 2) Found 07-exp1.war in deployment directory. To trigger deployment create a file called 07-exp1.war.dodeploy
07:08:23,824 INFO [org.jboss.as.server.deployment] (MSC service thread 1-4) Starting deployment of "07-exp1.war"
07:08:24,058 INFO [org.jboss.as.server.controller] (DeploymentScanner-threads - 1) Deployment of "07-exp1.war" was rolled back with failure message {"Services with missing/unavailable dependencies" => ["jboss.deployment.unit.\"07-exp1.war\".jndiDependencyService missing [ jboss.naming.context.java.module.07-exp1.07-exp1.env/jdbc/DataSourceExample ]","jboss.naming.context.java.module.07-exp1.07-exp1.env/jdbc/DataSourceExample.jboss.deployment.unit.\"07-exp1.war\".module.07-exp1.07-exp1.0 missing [ jboss.naming.context.java.module.07-exp1.07-exp1.env/jdbc/DataSourceExample ]"]}
07:08:24,064 INFO [org.jboss.as.server.deployment] (MSC service thread 1-3) Stopped deployment 07-exp1.war in 6ms
07:08:24,066 ERROR [org.jboss.as.deployment] (DeploymentScanner-threads - 2) {"Composite operation failed and was rolled back. Steps that failed:" => {"Operation step-2" => {"Services with missing/unavailable dependencies" => ["jboss.deployment.unit.\"07-exp1.war\".jndiDependencyService missing [ jboss.naming.context.java.module.07-exp1.07-exp1.env/jdbc/DataSourceExample ]","jboss.naming.context.java.module.07-exp1.07-exp1.env/jdbc/DataSourceExample.jboss.deployment.unit.\"07-exp1.war\".module.07-exp1.07-exp1.0 missing [ jboss.naming.context.java.module.07-exp1.07-exp1.env/jdbc/DataSourceExample ]"]}}}

Jeanne
Jeanne Harris
Greenhorn

Joined: May 07, 2012
Posts: 3
For a test I created a simple java project with one file called Main.java and deployed it as a Java App and it runs with no errors. I accessed the database and display the two records and three fields as expected. I am missing or have an error in my jsp projects environment somewhere.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Data Source Connection with MySQL