Julianosts, vlw pela dica, já tinha dado uma olhada na apostila da Caelum.
Estou tendo problemas na minha tentativa de gravar algo no banco, alguém pode me ajudar?
Pra começar estou tentando fazer algo bem simples: existe um campo de texto que o usuário preenche com algum nome e o form quando é submetido é enviado para um servlet que tenta gravar no banco usando as classes Contato e ContatoDAO (pelo que entendi a classe DAO é a que lida diretamente com o banco). Meus códigos estão assim:
Index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form name="FormPessoa" action="ServletCadastrar" method="get">
Nome: <input type="text" name="nome" /><br />
<input type="submit" value="Enviar" />
</form>
</body>
</html>
ContatoDAO
package Exemplos;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ContatoDAO {
public String Nome;
public Connection con;
public ContatoDAO()
{
}
public void setConexao() throws SQLException{
con = new Conexao().getConexao();
}
public void inserir(Contato contato) {
String insercao = "insert teste (Nome) Value ('" + contato.getNome() + "')";
try {
PreparedStatement inserir = con.prepareStatement(insercao);
inserir.execute();
inserir.close();
} catch (SQLException ex) {
Logger.getLogger(ContatoDAO.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Contato
package Exemplos;
public class Contato {
private String Nome;
public String getNome() {
return Nome;
}
public void setNome(String Nome) {
this.Nome = Nome;
}
}
Conexao
package Classes;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Conexao {
private static final String URL = "jdbc:mysql://localhost:3306/TutorialJSP";
private static final String Driver = "com.mysql.jdbc.Driver";
private static final String Usuario = "root";
private static final String Senha = "senha";
public Connection getConexao() throws SQLException
{
try{
Class.forName(Driver);
return DriverManager.getConnection(URL, Usuario, Senha);
} catch(ClassNotFoundException ex) {
throw new RuntimeException(ex);
}
}
}
ServletCadastrar
package Exemplos;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author Charles Rockenbach
*/
public class ServletCadastrar extends HttpServlet {
/**
* 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
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Contato contato = new Contato();
contato.setNome(request.getParameter("nome"));
ContatoDAO contatoDao = new ContatoDAO();
contatoDao.inserir(contato);
}
// <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>
}
O erro que ocorre ao executar é esse:
java.lang.NullPointerException
Exemplos.ContatoDAO.inserir(ContatoDAO.java:34)
Exemplos.ServletCadastrar.processRequest(ServletCadastrar.java:41)
Exemplos.ServletCadastrar.doGet(ServletCadastrar.java:56)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
Não sei mais o que fazer... alguem me ajuda, please :D
Além de falar o que está errado poderiam me dizer se a estrutura de comunicação pra fazer o que quero é essa mesma que montei???
vlw