per ora ho fatto questo. Ditemi un po se almeno di logica ci siamo.
pagina .jsp
codice:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<title><bean:message key="inserimento.title" /></title>
<body>
<logic:notPresent name="org.apache.struts.action.MESSAGE"
scope="application">
<font color="red"> ERROR: Application resources not loaded --
check servlet container logs for error messages. </font>
</logic:notPresent>
<h3>
<bean:message key="inserimento.heading" />
</h3>
<bean:message key="inserimento.message" />
</p>
<form name="Persona" action="/InsertAction" method="get">
<fieldset style="width: 300px;">
<legend>Inserimento Dati:</legend>
<table>
<tr>
<td>ID:</td>
<td><input name="id" type="text" />
</td>
</tr>
<tr>
<td>Nome:</td>
<td><input name="nome" type="text" />
</td>
</tr>
<tr>
<td>Cognome:</td>
<td><input name="cognome" type="text" /></td>
</tr>
<tr>
<td>Indirizzo:</td>
<td><input name="indirizzo" type="text" />
</td>
</tr>
<tr>
<td>Ruolo:</td>
<td><input name="ruolo" type="text" />
</td>
</tr>
<!--
<tr>
<td>Select*:</td>
<td><input type="radio" name="CRUD" value="select" />
</td>
</tr>
<tr>
<td>Insert**:</td>
<td><input type="radio" name="CRUD" value="insert" /></td>
</tr>
<tr>
<td>Update**:</td>
<td><input type="radio" name="CRUD" value="update" />
</td>
</tr>
<tr>
<td>Delete**:</td>
<td><input type="radio" name="CRUD" value="delete" /></td>
</tr>
-->
<tr>
<td><input id="Submit" type="submit" value="Invia Richiesta" />
</td>
</tr>
</table>
</fieldset>
*ID Opzionale **ID Obbligatorio
</form>
</body>
</html>
classe action
codice:
import java.io.IOException;
import javax.servlet.ServletException;
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;
public class InsertAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
Persona mioForm = (Persona) form; //Cast del form
String id = mioForm.getId();
String nome = mioForm.getNome();
String cognome = mioForm.getCognome(); //prendo i dati dal form presente nella pagina jsp
String indirizzo = mioForm.getIndirizzo();
String ruolo = mioForm.getRuolo();
mioForm.setId(id);
mioForm.setNome(nome);
mioForm.setCognome(cognome);
mioForm.setIndirizzo(indirizzo);
mioForm.setRuolo(ruolo);
DAO dao = new DAO();
Persona p = new Persona(id, nome, cognome, indirizzo, ruolo);
dao.inserisciPersona(p);
return mapping.findForward("success");
}
}
classe Persona
codice:
import org.apache.struts.action.ActionForm;
public class Persona extends ActionForm {
private static final long serialVersionUID = 1L;
String id = null;
String nome = null;
String cognome = null;
String indirizzo = null;
String ruolo = null;
public Persona(String id, String nome, String cognome,
String indirizzo, String ruolo) {
this.id = id;
this.nome = nome;
this.cognome = cognome;
this.indirizzo = indirizzo;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getCognome() {
return cognome;
}
public void setCognome(String cognome) {
this.cognome = cognome;
}
public String getIndirizzo() {
return indirizzo;
}
public void setIndirizzo(String indirizzo) {
this.indirizzo = indirizzo;
}
public String getRuolo() {
return ruolo;
}
public void setRuolo(String ruolo) {
this.ruolo = ruolo;
}
public String toString() {
return "ID: " + id + "Nome: " + nome + "\nCognome: " + cognome
+ "\nIndirizzo: " + indirizzo + "\nRuolo: " + ruolo;
}
}
classe DAO
codice:
import java.sql.*;
import java.util.ArrayList;
public class DAO {
public void inserisciPersona(Persona p) {
try {
String driver = "com.mysql.jdbc.Driver";
Class.forName(driver);
String url = "jdbc:mysql://localhost:3306/progetto";
Connection con = DriverManager.getConnection(url, "root", "");
PreparedStatement cmd = con
.prepareStatement("INSERT INTO persona (idPersona, nome, cognome, indirizzo, ruolo) VALUES (?, ?, ?, ?, ?)");
String qry = "SELECT * FROM persona";
ArrayList<Persona> lista = new ArrayList<Persona>();
ResultSet res = cmd.executeQuery(qry);
while (res.next()) {
lista.add(new Persona(res.getString("idPersona"), res
.getString("nome"), res.getString("cognome"), res
.getString("indirizzo"), res.getString("ruolo")));
if (p.id == res.getString("idPersona")
|| (p.nome == res.getString("nome") && p.cognome == res
.getString("cognome"))) {
System.err
.println("impossibile inserire un record già esistente");
} else {
cmd.setInt(1, Integer.parseInt(p.id));
cmd.setString(2, p.nome);
cmd.setString(3, p.cognome);
cmd.setString(4, p.indirizzo);
cmd.setString(5, p.ruolo);
cmd.execute();
}
}
} catch (SQLException SQLexc) {
SQLexc.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}