Devo fare un inserimento in una base di dati....ma non ci si riesce...perchè?
Faccio presente che i termini da inserire vengono letti...
La struttura della tabella è
- codiceutente
nome
cognome
login
password
Qui di seguito il programma (la parte che interessa....)
codice:
import java.io.*;
import java.net.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
*
* @author xxxxxx
* @version
*/
public class Inserimento extends HttpServlet {
static final String JDBC_DRIVER="sun.jdbc.odbc.JdbcOdbcDriver";//driver
static final String DATABASE_URL="jdbc:odbc:GestioneUtenti.mdb";//db
Connection connection=null;//connessione
PreparedStatement ps=null;
Statement statement=null;
/** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet Inserimento</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet Inserimento at " + request.getContextPath () + "</h1>");
PreparedStatement ps=null;
try{
Class.forName(JDBC_DRIVER);
connection=DriverManager.getConnection(DATABASE_URL);
String nome=request.getParameter("nome");
out.println(nome);
String cognome=request.getParameter("cognome");
out.println(cognome);
String login=request.getParameter("login");
out.println(login);
String password=request.getParameter("password");
out.println(password);
String sql="INSERT INTO Utenti VALUES(?,?,?,?)";
ps=connection.prepareStatement(sql);
ps.setString(1,nome);
ps.setString(2,cognome);
ps.setString(3,login);
ps.setString(4,password);
ps.executeUpdate();
} catch(SQLException sqle)
{
sqle.printStackTrace();
}
catch(ClassNotFoundException cnfe)
{
cnfe.printStackTrace();
}
catch(NullPointerException npe)
{
npe.printStackTrace();
}
finally
{
try
{
statement.close();
connection.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
out.println("</body>");
out.println("</html>");
out.close();
}
Qualcuno ha un'idea?
P.S.....Scusate la lunghezza del messaggio....