sono arrivato a questo punto
index .jsp
codice:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form name="Inserimento DB" action="/Progetto/Acquisisci" method="get">
<div style="text-align: center">
ID: <input name="id" type="text" />
Nome: <input name="nome"
type="text" />
Cognome:<input name="cognome" type="text" />
Indirizzo:<input name="indirizzo" type="text" />
Ruolo: <input
name="ruolo" type="text" />
<input id="Submit" type="submit"
value="Inserisci" />
</div>
</form>
</body>
</html>
Acquisisci.java (SERVLET)
codice:
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import classi.DAO;
import classi.Persona;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Acquisisci extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id = request.getParameter("id");
String nome = request.getParameter("nome");
String cognome = request.getParameter("cognome");
String indirizzo = request.getParameter("indirizzo");
String ruolo = request.getParameter("ruolo");
if (id != null && nome != null && cognome != null && indirizzo != null
& ruolo != null) {
if (id.length() > 0 && nome.length() > 0 && cognome.length() > 0
&& indirizzo.length() > 0 && ruolo.length() > 0) {
Persona p = new Persona(id, nome, cognome, indirizzo, ruolo);
DAO dao = new DAO();
dao.inserisciPersona(p);
} else {
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1> Errore nell'inserimento dei dati, controlla i campi </h1>");
out.println("</body>");
out.println("</html>");
}
}
}
}
Persona.java
codice:
package classi;
public class Persona {
String id;
String nome;
String cognome;
String indirizzo;
String ruolo;
public Persona(String id, String nome, String cognome, String indirizzo,
String ruolo) {
this.id = id;
this.nome = nome;
this.cognome = cognome;
this.indirizzo = indirizzo;
this.ruolo = ruolo;
}
}
DAO.java
codice:
package classi;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
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 (?, ?, ?, ?, ?)");
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();
// if (cmd.execute())
// System.out.println("inserimento corretto");
con.close();
} catch (SQLException SQLexc) {
SQLexc.printStackTrace();
System.err.println(SQLexc);
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.err.println(e);
}
}
}
Secondo voi perchè non fa sta cavolo di Insert?