Bom dia Pessoal,
To fazendo o tutorial indicado pelo GUJ sobre Struts: http://www.j2eebrasil.com.br/jsp/artigos/artigo.jsp?idArtigo=0011 .
Mas to com a seguinte dúvida.
No tutorial, ele ensina o exemplo de Struts utilizando a versão 1.1 se não me engano. Mas eu to com a versão 1.2.9 aqui. Na versão 1.1, as alterações JDBC são feitas no server.xml , enquanto que na 1.2.9 é feita no struts-config. Alguém sabe sobre isso?
O problema que está acontecendo é algo parecido com este:
http://forum.java.sun.com/thread.jspa?threadID=723505&messageID=4171509 (que não foi postado por mim.)
O meu código está assim:
Parte do struts-config.xml onde fica as configurações de conexão.
<data-source key="StrutsDemoDS" type="org.apache.commons.dbcp.BasicDataSource">
<set-property property="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<set-property property="url" value="jdbc:oracle:thin:@servvec:1521:servvec"/>
<set-property property="maxActive" value="5" />
<set-property property="username" value="USR_TESTE" />
<set-property property="password" value="xxx" />
<set-property property="autoCommit" value="true" />
</data-source>
Classe onde é usada a conexão.
[code]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();
System.out.println("ok");
// se for tomcat
dataSource = (DataSource) ic.lookup("java:comp/env/jdbc/StrutsDemoDS");
System.out.println("db="+dataSource);
// no JBoss faça
// dataSource = (DataSource) ic.lookup("java:jdbc/StrutsDemoDS");
} catch (NamingException ex) {
System.out.println(ex.getMessage());
throw ex;
}
}
}
protected Connection getConnection() throws SQLException {
Connection conn = null;
try {
conn = dataSource.getConnection();
System.out.println("conn="+conn);
}
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();
}
}
}
}
[/code]
O erro que está retornando é:
[code]HTTP Status 500 -
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Name jdbc is not bound in this Context
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:535)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:433)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause
javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
org.apache.naming.NamingContext.lookup(NamingContext.java:768)
org.apache.naming.NamingContext.lookup(NamingContext.java:138)
org.apache.naming.NamingContext.lookup(NamingContext.java:779)
org.apache.naming.NamingContext.lookup(NamingContext.java:138)
org.apache.naming.NamingContext.lookup(NamingContext.java:779)
org.apache.naming.NamingContext.lookup(NamingContext.java:151)
org.apache.naming.SelectorContext.lookup(SelectorContext.java:136)
javax.naming.InitialContext.lookup(InitialContext.java:351)
strutsdemo.bean.AdminUsers.<init>(AdminUsers.java:24)
strutsdemo.action.ListUsersAction.execute(ListUsersAction.java:30)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
note The full stack trace of the root cause is available in the Apache Tomcat/5.0.28 logs.[/code]