users@glassfish.java.net

Glassfish - JSF - Mysql setup issues (Core JSF example)

From: <glassfish_at_javadesktop.org>
Date: Wed, 28 May 2008 08:37:41 PDT

I'm working through "Core JSF" book and now on chapter 10 - 'Configuring a Data Source'

However, I'm running into problems with MySql database.

In my Glassfish admin dashboard, I have setup a connection pool for MySql..
Datasource Classname: com.mysql.jdbc.jdbc2.optional.MysqlDataSource
Resource Type: javax.sql.Datasource
Additional Properties:
Server Name: localhost
PortNumber: 3306
databaseName: test
User: root
Password: ******

When I ping this, I get 'Ping Succeeded'

In my Glassfish admin dashboard, I have also setup a JDBC Resource for MySql
JNDI Name: jdbc/mydb
Pool Name: MysqlPool (see above)

[b]web.xml [/b]
<?xml version="1.0"?>
<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
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>

<resource-ref>
<res-ref-name>jdbc/mydb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>

[b] index.jsp [/b]
<html>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<f:view>
<head>
<title><h:outputText value="#{msgs.title}"/></title>
</head>
<body>
<h:form>
<h1><h:outputText value="#{msgs.enterNameAndPassword}"/></h1>
<h:panelGrid columns="2">
<h:outputText value="#{msgs.name}"/>
<h:inputText value="#{user.name}"/>

<h:outputText value="#{msgs.password}"/>
<h:inputSecret value="#{user.password}"/>
</h:panelGrid>
<h:commandButton value="#{msgs.login}" action="#{user.login}"/>
</h:form>
</body>
</f:view>
</html>

[b]faces-config.xml[/b]
<?xml version="1.0"?>
<faces-config 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
http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd"
version="1.2">
<navigation-rule>
<from-view-id>/index.jsp</from-view-id>
<navigation-case>
<from-outcome>loginSuccess</from-outcome>
<to-view-id>/welcome.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>loginFailure</from-outcome>
<to-view-id>/sorry.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>internalError</from-outcome>
<to-view-id>/error.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-view-id>/welcome.jsp</from-view-id>
<navigation-case>
<from-outcome>login</from-outcome>
<to-view-id>/index.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-view-id>/sorry.jsp</from-view-id>
<navigation-case>
<from-outcome>login</from-outcome>
<to-view-id>/index.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-view-id>/error.jsp</from-view-id>
<navigation-case>
<from-outcome>login</from-outcome>
<to-view-id>/index.jsp</to-view-id>
</navigation-case>
</navigation-rule>

<managed-bean>
<managed-bean-name>user</managed-bean-name>
<managed-bean-class>com.corejsf.UserBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>

<application>
<resource-bundle>
<base-name>com.corejsf.messages</base-name>
<var>msgs</var>
</resource-bundle>
</application>
</faces-config>

[b]UserBean.java[/b]

package com.corejsf;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.annotation.Resource;
import javax.sql.DataSource;

public class UserBean {
private String name;
private String password;
private boolean loggedIn;
private Logger logger = Logger.getLogger("com.corejsf");

@Resource(name="jdbc/mydb")
private DataSource ds;

/*
If you use Tomcat or JSF 1.1, remove the @Resource line and add this constructor:
public UserBean()
{
try {
Context ctx = new InitialContext();
ds = (DataSource) ctx.lookup("java:comp/env/jdbc/mydb");
} catch (NamingException ex) {
logger.log(Level.SEVERE, "DataSource lookup failed", ex);
}
}
*/

public String getName() { return name; }
public void setName(String newValue) { name = newValue; }

public String getPassword() { return password; }
public void setPassword(String newValue) { password = newValue; }

public String login() {
try {
doLogin();
}
catch (SQLException ex) {
logger.log(Level.SEVERE, "login failed", ex);
return "internalError";
}
if (loggedIn)
return "loginSuccess";
else
return "loginFailure";
}

public String logout() {
loggedIn = false;
return "login";
}

public void doLogin() throws SQLException {
if (ds == null) throw new SQLException("No data source");
Connection conn = ds.getConnection();
if (conn == null) throw new SQLException("No connection");

try {
PreparedStatement passwordQuery = conn.prepareStatement(
"SELECT password from Users WHERE username = ?");

passwordQuery.setString(1, name);

ResultSet result = passwordQuery.executeQuery();

if (!result.next()) return;
String storedPassword = result.getString("password");
loggedIn = password.equals(storedPassword.trim());
}
finally {
conn.close();
}
}
}

After deployment, when I try http://localhost:8080/db, I get the following...

HTTP Status 500 -

type Exception report

message

descriptionThe server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: com.sun.enterprise.InjectionException: Exception attempting to inject Res-Ref-Env-Property: jdbc/mydb_at_javax.sql.DataSource@ resolved as: jndi: jdbc/mydb_at_res principal: null_at_mail: null
No Runtime properties
Database Vendor : null
Create Tables at Deploy : false
Delete Tables at Undeploy : false into class com.corejsf.UserBean

root cause

javax.faces.FacesException: com.sun.enterprise.InjectionException: Exception attempting to inject Res-Ref-Env-Property: jdbc/mydb_at_javax.sql.DataSource@ resolved as: jndi: jdbc/mydb_at_res principal: null_at_mail: null
No Runtime properties
Database Vendor : null
Create Tables at Deploy : false
Delete Tables at Undeploy : false into class com.corejsf.UserBean

root cause

com.sun.enterprise.InjectionException: Exception attempting to inject Res-Ref-Env-Property: jdbc/mydb_at_javax.sql.DataSource@ resolved as: jndi: jdbc/mydb_at_res principal: null_at_mail: null
No Runtime properties
Database Vendor : null
Create Tables at Deploy : false
Delete Tables at Undeploy : false into class com.corejsf.UserBean

root cause

javax.naming.NameNotFoundException: mydb not found

note The full stack traces of the exception and its root causes are available in the Sun Java System Application Server 9.1.1 logs.

I have been frustrated by this for seven hours already - time for some friendly help. Any suggestions would be appreciated. Perhaps
this belongs in a Mysql forum. Let me know. I'm relatively new to all the technologies - but I'm running
out of the box on this - so I didn't anticipate problems.

I'm committed to Glassfish - but if there are easier approaches other than with JSF and/or MySql, I'm open to them.

Thanks

Dave C
[Message sent by forum member 'davidchapman' (davidchapman)]

http://forums.java.net/jive/thread.jspa?messageID=277023