é a minha classe dao…dê uma olhada nas actions…
**** DAO*****
package br.com.ricardo.jdbc.dao;
import br.com.ricardo.jdbc.jdbc.ConnectionFactory;
import br.com.ricardo.jdbc.modelo.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class ContatoDAO {
// a conexão com o banco de dados
private Connection connection;
// construtor que recebe a conex�o
public ContatoDAO(Connection con) {
this.connection = con;
}
public ContatoDAO() throws SQLException {
this.connection = ConnectionFactory.getConnection();
}
public void adiciona(Contato contato) throws SQLException {
// prepared statement para inser��o
PreparedStatement stmt = this.connection.prepareStatement("insert into contatos (nome,email,endereco) values (?, ?, ?)");
// seta os valores
stmt.setString(1, contato.getNome());
stmt.setString(2, contato.getEmail());
stmt.setString(3, contato.getEndereco());
// executa
stmt.execute();
stmt.close();
}
public List<Contato> getLista() throws SQLException {
PreparedStatement stmt = this.connection.prepareStatement("select * from contatos");
ResultSet rs = stmt.executeQuery();
List<Contato> list = new ArrayList<Contato>();
while (rs.next()) {
// criando o objeto Contato
Contato contato = new Contato();
contato.setId(rs.getLong("id"));
contato.setNome(rs.getString("nome"));
contato.setEmail(rs.getString("email"));
contato.setEndereco(rs.getString("endereco"));
// adicionando o objeto � lista
list.add(contato);
}
rs.close();
stmt.close();
return list;
}
public void altera(Contato contato) throws SQLException {
PreparedStatement stmt = connection.prepareStatement("update contatos set nome=?, email=?, endereco=? where id=?");
stmt.setString(1, contato.getNome());
stmt.setString(2, contato.getEmail());
stmt.setString(3, contato.getEndereco());
stmt.setLong(4, contato.getId());
stmt.execute();
stmt.close();
}
public void remove(Contato contato) throws SQLException {
PreparedStatement stmt = connection.prepareStatement("delete from contatos where id=?");
stmt.setLong(1, contato.getId());
stmt.execute();
stmt.close();
}
public Contato procura(Long id) throws SQLException {
PreparedStatement stmt = connection.prepareStatement("select * from contatos where id=?");
stmt.setLong(1, id);
ResultSet rs = stmt.executeQuery();
if(!rs.next()) return null;
Contato c = new Contato();
c.setId(rs.getLong("id"));
c.setNome(rs.getString("nome"));
c.setEmail(rs.getString("email"));
c.setEndereco(rs.getString("endereco"));
rs.close();
stmt.close();
return c;
}
}
**** fabrica de conexão*****
package br.com.ricardo.jdbc.jdbc;
import java.sql.*;
import javax.swing.*;
public class ConnectionFactory {
public static Connection getConnection() throws SQLException {
try {
Class.forName("com.mysql.jdbc.Driver");
JOptionPane.showMessageDialog(null, "Conectando ao banco", "Informação", 2);
return DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/teste", "root", "catia");
} catch (ClassNotFoundException e) {
throw new SQLException(e.getMessage());
}
}
}
**** action adiciona contato****
package br.com.ricardo.struts.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import br.com.ricardo.jdbc.dao.ContatoDAO;
import br.com.ricardo.jdbc.modelo.Contato;
import br.com.ricardo.struts.form.ContatoForm;
public class AdicionaContatoAction extends Action {
@Override
public ActionForward execute(ActionMapping map, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
//log
System.out.println("Tentando criar um novo contato");
//Formulário do cliente
ContatoForm formulario = (ContatoForm) form;
//Acessa o bean
Contato contato = formulario.getContato();
//Adiciona ao banco de dados
ContatoDAO dao = new ContatoDAO();
dao.adiciona(contato);
//ok... visualização
return map.findForward("ok");
}
}
**** struts-config******
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<form-bean name="ContatoForm" type="br.com.ricardo.struts.form.ContatoForm"/>
<form-bean name="RemoveContatoForm" type="br.com.ricardo.struts.form.RemoveContatoForm"/>
<form-bean name="MostraContatoForm" type="br.com.ricardo.struts.form.MostraContatoForm"/>
<form-bean name="AlteraContatoForm" type="br.com.ricardo.struts.form.AlteraContatoForm"/>
<form-bean name="LoginForm" type="br.com.ricardo.struts.form.LoginForm"/>
</form-beans>
<action-mappings>
<action path="/teste" type="br.com.ricardo.struts.action.TesteSimplesAction">
<forward name="ok" path="/exemplo.jsp"/>
</action>
<action path="/listaContatos" type="br.com.ricardo.struts.action.ListaContatosAction">
<forward name="lista" path="/lista.jsp"/>
<forward name="vazia" path="/listavazia.jsp"/>
</action>
<action path="/novoContato" name="ContatoForm" scope="request" input="/novo.jsp" type="br.com.ricardo.struts.action.AdicionaContatoAction">
<forward name="ok" path="/listaContatos.do"/>
</action>
<action path="/removeContato" name="RemoveContatoForm" type="br.com.ricardo.struts.action.RemoveContatoAction">
<forward name="ok" path="/listaContatos.do"/>
</action>
<action path="/mostraContato" name="MostraContatoForm" type="br.com.ricardo.struts.action.MostraContatoAction">
<forward name="ok" path="/mostraContato.jsp"/>
</action>
<action path="/alteraContato" name="AlteraContatoForm" type="br.com.ricardo.struts.action.AlteraContatoAction">
<forward name="ok" path="/listaContatos.do"/>
</action>
<action path="/mudaLingua" type="br.com.ricardo.struts.action.MudaLinguaAction">
<forward name="ok" path="/testa-mensagens.jsp"/>
</action>
<action path="/login" name="LoginForm" type="br.com.ricardo.struts.action.LoginAction">
<forward name="ok" path="/novo.jsp"/>
<forward name="erro" path="/erro.jsp"/>
</action>
</action-mappings>
<!-- Arquivo de Mensagens -->
<message-resources parameter="com/myapp/struts/ApplicationResource"/>
</struts-config>
**** login.jsp****
<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<html:html>
<head>
<title>
Sistema de teste do Struts
</title>
</head>
<html:form action="/login" focus="funcionario.usuario">
Digite o usuário:
<html:text property="funcionario.usuario"/><br/>
Digite sua senha:
<html:text property="funcionario.senha"/><br/>
<html:submit>Enviar dados</html:submit>
</html:form>
</html:html>
****** novo.jsp*****
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<html:html>
<head>
<title>
Sistema de teste do Struts
</title>
</head>
<html:form action="/novoContato" focus="contato.nome">
Nome:
<html:text property="contato.nome"/> <font color=red> <html:errors property="nome"/></font>
<br/>
e-Mail:
<html:text property="contato.email"/> <font color=red> <html:errors property="email"/></font>
<br/>
Endereço:
<html:text property="contato.endereco"/> <font color=red> <html:errors property="endereco"/></font>
<br/>
<html:submit>Enviar dados</html:submit>
<br/>
</html:form>
</html:html>
****minha pagina principal é o login…depois ele vai pra novo.jsp pra adicionar contato…quando vai inserir tá dando este erro …
obs: to usando o netbeans…
valeu