Olá amigos tenho um tutorial de Struts + Framework em que estou tentando concluir a aplicação mas esta dando erros na conexao do datasource, ele me gera a mensagem de erro nos Beans e na hora que eu mando compilar o projeto ele me gera um erro no DataSource dizendo que meu caminho esta errado , segue o codigo:
package strutsdemo.bean;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class AdminUsers {
protected static DataSource dataSource;
public AdminUsers() throws Exception {
if (dataSource == null) {
try {
//InitialContext ic = new InitialContext();
//dataSource = (DataSource) ic.lookup("java:comp/env/jdbc/StrutsDemoDS");
Connection conn = null;
InitialContext ic = new InitialContext();
DataSource dataSource = (DataSource) ic.lookup("java:comp/env/jdbc/StrutsDemoDS");
if (dataSource != null) {
conn = dataSource.getConnection();
}
} catch (NamingException ex) {
System.out.println(ex.getMessage());
throw ex;
}
}
}
protected Connection getConnection() throws SQLException {
Connection conn = null;
try {
conn = dataSource.getConnection();
} catch (SQLException e) {
throw e;
}
return conn;
}
protected void closeConnection(
Connection conn,
PreparedStatement stmt,
ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
}
}
}
public LinkedList getUserList() throws SQLException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
LinkedList users = new LinkedList();
try {
conn = getConnection();
stmt = conn.prepareStatement("select * from usuario");
rs = stmt.executeQuery();
while (rs.next()) {
UserData user = new UserData();
user.setIdUsuario(rs.getInt("id_usuario"));
user.setNome(rs.getString("nome"));
user.setLogin(rs.getString("login"));
user.setSenha(rs.getString("senha"));
user.setSexo(rs.getString("sexo"));
user.setAtivo(rs.getBoolean("ativo"));
user.setFaixaIdade(rs.getInt("faixa_idade"));
users.add(user);
}
} catch (SQLException e) {
throw e;
} finally {
closeConnection(conn, stmt, rs);
}
return users;
}
public void insertUser(UserData user) throws SQLException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = getConnection();
stmt = conn.prepareStatement(
"insert into usuario \n" +
"(id_usuario, nome, login, senha, sexo, ativo, faixa_idade) \n" +
"values (?, ?, ?, ?, ?, ?, ?)");
stmt.setInt(1, user.getIdUsuario());
stmt.setString(2, user.getNome());
stmt.setString(3, user.getLogin());
stmt.setString(4, user.getSenha());
stmt.setString(5, user.getSexo());
stmt.setBoolean(6, user.getAtivo());
stmt.setInt(7, user.getFaixaIdade());
stmt.executeUpdate();
} catch (SQLException e) {
throw e;
} finally {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
}
}
public void updateUser(UserData user) throws SQLException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = getConnection();
stmt = conn.prepareStatement(
"update usuario set \n" +
"nome = ?, login = ?, senha = ?, sexo = ?, ativo = ?, faixa_idade = ? \n" +
"where id_usuario = ?");
stmt.setString(1, user.getNome());
stmt.setString(2, user.getLogin());
stmt.setString(3, user.getSenha());
stmt.setString(4, user.getSexo());
short ativo = (short) (user.getAtivo()? 1: 0);
stmt.setShort(5, ativo);
stmt.setInt(6, user.getFaixaIdade());
stmt.setInt(7, user.getIdUsuario());
stmt.executeUpdate();
} catch (SQLException e) {
throw e;
} finally {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
}
}
public void deleteUser(int idUsuario) throws SQLException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = getConnection();
stmt = conn.prepareStatement(
"delete from usuario where id_usuario = ?");
stmt.setInt(1, idUsuario);
stmt.executeUpdate();
} catch (SQLException e) {
throw e;
} finally {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
}
}
}
o meu codigo de conexao é este galera e o meu struts-config.xml é o seguinte:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<!-- ========== Data Source Configuration =============================== -->
<data-sources>
<!--
<data-source key="org.apache.struts.action.DATA_SOURCE">
<set-property property="autoCommit" value="false"/>
<set-property property="description" value="Struts DataSource"/>
<set-property property="driverClass" value="com.mysql.jdbc.Driver"/>
<set-property property="url" value="jdbc:mysql://192.168.10.68/strutsdemo "/>
<set-property property="maxCount" value="4"/>
<set-property property="minCount" value="2"/>
<set-property property="user" value="root"/>
<set-property property="password" value=""/>
</data-source>
-->
</data-sources>
<!-- ========== Form Bean Definitions ================================== -->
<form-beans>
<form-bean dynamic="false" name="saveEditUserForm" type="strutsdemo.form.SaveEditUserForm" />
<form-bean dynamic="true" name="saveInsertUserForm" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="idUsuario" type="java.lang.String" />
<form-property name="login" type="java.lang.String" />
<form-property name="nome" type="java.lang.String" />
<form-property name="faixaIdade" type="java.lang.String" />
<form-property name="sexo" type="java.lang.String" />
<form-property name="ativo" type="java.lang.String" />
<form-property name="senha" type="java.lang.String" />
<form-property name="confirmacaoSenha" type="java.lang.String" />
</form-bean>
</form-beans>
<!-- ================================= Global Exception Definitions -->
<global-exceptions>
<!-- sample exception handler
<exception key="expired.password" type="app.ExpiredPasswordException" path="/changePassword.jsp"/>
end sample -->
</global-exceptions>
<!-- =================================== Global Forward Definitions -->
<global-forwards>
<forward
name="welcome"
path="/Welcome.do"/>
<forward
name="failure"
path="/error.jsp"
redirect="true"
contextRelative="false" />
</global-forwards>
<!-- =================================== Action Mapping Definitions -->
<action-mappings>
<action
path="/Welcome"
type="org.apache.struts.actions.ForwardAction"
parameter="/pages/Welcome.jsp"/>
<action
path="/listUsers"
scope="session"
type="strutsdemo.action.ListUsersAction"
unknown="false"
validate="false">
<forward
name="success"
path="/pages/ListUsers.jsp"
redirect="false"
contextRelative="false" />
</action>
<action
path="/editUser"
scope="session"
type="strutsdemo.action.EditUserAction"
unknown="false"
validate="false">
<forward
name="success"
path="/pages/EditUser.jsp"
redirect="false"
contextRelative="false" />
</action>
<action
attribute="saveEditUserForm"
input="/pages/EditUser.jsp"
name="saveEditUserForm"
path="/saveEditUser"
scope="session"
type="strutsdemo.action.SaveEditUserAction"
unknown="false"
validate="true">
<forward
name="success"
path="/pages/listUsers.jsp"
redirect="false"
contextRelative="false" />
</action>
<action
path="/insertUser"
scope="session"
type="strutsdemo.action.InsertUserAction"
unknown="false"
validate="false">
<forward
name="success"
path="/pages/insertUser.jsp"
redirect="false"
contextRelative="false" />
</action>
<action
attribute="saveInsertUserForm"
input="/pages/insertUser.jsp"
name="saveInsertUserForm"
path="/saveInsertUser"
scope="session"
type="strutsdemo.action.SaveInsertUserAction"
unknown="false"
validate="true">
<forward
name="success"
path="/pages/listUsers.jsp"
redirect="false"
contextRelative="false" />
<forward
name="error"
path="/pages/insertUser.jsp"
redirect="false"
contextRelative="false" />
</action>
<action
path="/deleteUser"
scope="session"
type="strutsdemo.action.DeleteUserAction"
unknown="false"
validate="false">
<forward
name="success"
path="/pages/listUsers.jsp"
redirect="false"
contextRelative="false" />
</action>
</action-mappings>
<!-- ===================================== Controller Configuration -->
<controller
processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>
<!-- ==================== ============ Message Resources Definitions -->
<message-resources parameter="com.myapp.struts.ApplicationResource" null="true" />
<!-- ======================================= Plug Ins Configuration -->
<!-- comment following if struts1.0.x -->
<plug-in className="org.apache.struts.tiles.TilesPlugin" >
<set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />
<set-property property="moduleAware" value="true" />
<set-property property="definitions-parser-validate" value="true" />
</plug-in>
<!-- end comment if struts1.0.x -->
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>
</struts-config>
e o erro que meu log gera é o seguinte:
ERRO GERADO PELO GERENCIADOR DE LOG
16/11/2006 08:04:31 org.apache.catalina.core.ApplicationContext log
SEVERE: action: Erro carregando a lista de usuários
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:780)
Amigos nao sei mais o que faco, meu amigo (luis.soares) me deu um help nos outros chanu que estavam dando e o cara é show de bola, agora so ficou estas zicas pra eu tentar resolver, se alguem ae puder me ajudar eu agradeço de coração, tenham todos uma boa semana.
Kleber Gracia
Equipe Kfreelancer.eng.br