Ciao a tutti! Sto creando una servlet in Java su Netbeans, lo scopo č prelevare dei dati da una form per inserirli in un database, solo che l' inserimento non viene fatto e non capisco cosa sbaglio.
Qui uno screen delle cartelle e di come sono strutturate:
http://i40.tinypic.com/fabs44.jpg
E poi i vari codici:
JSP:
Classe con metodo per inserire nel database:codice:<%-- Document : registrazione Created on : 21-nov-2013, 19.29.48 Author : Dario --%> <!DOCTYPE html> <!-- To change this license header, choose License Headers in Project Properties. To change this template file, choose Tools | Templates and open the template in the editor. --> <html> <head> <title>Registrazione utente</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width"> </head> <body> <h1>Inserisci i dati per la registrazione!!!</h1> <form action="<%=request.getContextPath()%>/Inserimento" METHOD="post"> <p>Nome: <input type="text" name="nome" value=""></p> <p>Cognome: <input type="text" name="cognome" value=""></p> <p><input type="submit" name="submit" value="OK"></p> </form> </body> </html>
E Servlet che visualizza il risultato e utilizza il metodo della classe precedente per caricare in tabella:codice:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package javaCode; import java.io.PrintWriter; import java.io.StringWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author liliana */ public class DBClasses { public String caricaTabellaUtenti(String ur, String us, String p, String nome, String cognome) throws SQLException { String out=""; try{ Connection conn = DriverManager.getConnection(ur, us, p); PreparedStatement ps = conn.prepareStatement("INSERT into TABELLA_UTENTI(NOME,COGNOME) VALUES (?,?)"); //INSERISCO I DATI PRELEVANDO //PARAMETRI DAL FORM ps.setString(1,nome); ps.setString(2,cognome); ps.execute(); ps.close(); conn.close(); out="Complimenti!!! Sei stato inserito nel database."; } catch(SQLException e){ out=e.getMessage(); } return out; } }
L' eccezione č:codice:/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package javaCode; /** * * @author Dario */ /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ import java.io.IOException; import java.io.PrintWriter; import java.sql.DriverManager; import java.sql.SQLException; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.annotation.WebInitParam; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * * @author Dario */ @WebServlet( name = "Inserimento", urlPatterns = {"/Inserimento"}, initParams = { @WebInitParam(name = "DBurl", value = "jdbc:derby://localhost:1527/sample", description = "URL del DB"), @WebInitParam(name = "DBuser", value = "app", description = "Account per accedere al DB"), @WebInitParam(name = "DBpwd", value = "app", description = "Password per accedere al DB") }) public class Inserimento extends HttpServlet { private static String url = ""; private static String user = ""; private static String pwd = ""; /** * @see Servlet#init(ServletConfig) */ public void init(ServletConfig config) throws ServletException { user = config.getInitParameter("DBuser"); url = config.getInitParameter("DBurl"); pwd = config.getInitParameter("DBpwd"); //System.out.println(url + " " + user + " " + pwd); try { // registro DB driver (una sola volta, prima di gestire le richieste HTTP che arriveranno) DriverManager.registerDriver(new org.apache.derby.jdbc.ClientDriver()); } catch (SQLException e) { ServletException e1 = new ServletException(e.getMessage()); throw e1; } } /** * Processes requests for both HTTP * <code>GET</code> and * <code>POST</code> methods. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ private void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); String cognome = request.getParameter("cognome"); String nome = request.getParameter("nome"); DBClasses db = new DBClasses(); try { out.println("<html>"); out.println("<head>"); out.println("<title>Registrazione utente</title>"); out.println("</head>"); out.println("<body>"); out.println("<h2>Ciao " + nome + " "+cognome+"!!!</h2>"); out.println(db.caricaTabellaUtenti(url, url, pwd, nome, cognome)); out.println("</body>"); out.println("</html>"); } catch(SQLException e){ //dovrei lanciare un' eccezione di tipo ServletException... e.printStackTrace(); } finally { out.close(); } } // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> /** * Handles the HTTP * <code>GET</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Handles the HTTP * <code>POST</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Returns a short description of the servlet. * * @return a String containing servlet description */ @Override public String getServletInfo() { return "Short description"; }// </editor-fold> }
codice:Eccezione del protocollo di rete: DSS concatenato con lo stesso id al termine della stessa analisi di catena id. La connessione č stata terminata.
Sapreste dirmi cosa potrebbe non andare?

Rispondi quotando