meu acesso a banco ta ok, fiz uma classe inserindo no banco e deu certo, o problema está quando pego via request da pagina JSP e vem como null, resultando no erro (Column ‘cpf’ cannot be null)
form no jsp:
<form action="solicita_cadastro" method="POST" id="esq-form">
<h1>Solicitação de cadastro</h1>
<br/>
<p><label for="nome">Nome Completo </label>
<input type="text" name="nome" class="round full-width-input" required/></p>
<p><label for="numero">E-Mail </label>
<input type="email" name="email" class="round full-width-input" required/></p>
<p><label for="cpf">CPF</label>
<input type="text" id="cpf" class="round full-width-input" required/></p>
<p><label for="rg">RG</label>
<input type="text" id="rg" class="round full-width-input" required/></p>
<input type="submit" class="button2" value="Enviar">
</form>
Servlet controller:
public class solicita_cadastro extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String email,cpf,rg,nome;
email= request.getParameter("email");
cpf= request.getParameter("cpf");
rg= request.getParameter("rg");
nome = request.getParameter("nome");
Aluno model = new Aluno();
model.setCpf(cpf);
model.setRg(rg);
model.setEmail(email);
model.setNome(nome);
AlunoDao aluno = new AlunoDao();
aluno.cadastrar(model);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}
}
AlunoDAO:
public class AlunoDao {
private Connection connection;
String nome;
String cpf;
String email;
String rg;
public AlunoDao() {
try {
this.connection = new ConnectionFactory().getConnection();
} catch (SQLException e) {
System.out.println("não conectou AlunoDAO");
e.printStackTrace();
}
}
public void cadastrar(Aluno aluno) {
String sql = "INSERT INTO aluno(nome,cpf,email,rg) VALUES(?,?,?,?)";
try {
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setString(1, aluno.getNome());
stmt.setString(2, aluno.getCpf());
stmt.setString(3, aluno.getEmail());
stmt.setString(4, aluno.getRg());
stmt.execute();
stmt.close();
} catch (SQLException u) {
throw new RuntimeException(u);
}
}
public void alterar(Aluno aluno) {
String sql = "UPDATE aluno SET nome = '?', cpf = '?', rg = '?', email = '?' WHERE rg='"
+ aluno.getRg() + "'";
try {
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setString(1, aluno.getNome());
stmt.setString(2, aluno.getCpf());
stmt.setString(3, aluno.getRg());
stmt.setString(4, aluno.getEmail());
stmt.execute();
stmt.close();
} catch (SQLException u) {
throw new RuntimeException(u);
}
}
public Aluno consultarPorRg(String rg) {
Aluno aluno = new Aluno();
String sql = "select * from aluno where rg='" + rg + "'";
try {
// procedimentos para obter os dados de uma tabela
String query = "SELECT * FROM ALUNO";
PreparedStatement stmt = connection.prepareStatement(sql);
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
aluno.setNome(rs.getString("nome"));
aluno.setRg(rs.getString("rg"));
aluno.setCpf(rs.getString("cpf"));
aluno.setEmail(rs.getString("email"));
}
return aluno;
} catch (SQLException u) {
throw new RuntimeException(u);
}
}
}
Classe Aluno
public class Aluno {
String nome,rg,cpf,email;
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getRg() {
return rg;
}
public void setRg(String rg) {
this.rg = rg;
}
public String getCpf() {
return cpf;
}
public void setCpf(String cpf) {
this.cpf = cpf;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Classe de conexão:
public class ConnectionFactory {
public Connection getConnection() throws SQLException {
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Conectando ao Bancoo !!");
return DriverManager.getConnection("jdbc:mysql://localhost/aluno","root","admin");
}
catch (ClassNotFoundException e) { //Driver n�o encontrado
System.out.println("O driver expecificado nao foi encontrado.");
return null;
} catch (SQLException e) { //N�o conseguindo se conectar ao banco
System.out.println("Nao foi possivel conectar ao Banco de Dados.");
return null; }
}
}