cadastrousuario.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="configuracoes/geral.js"></script>
<script src="configuracoes/valida_form.js"></script>
<link rel="stylesheet" href="configuracoes/geral.css" type="text/css">
<link rel="stylesheet" href="configuracoes/cadastro.css" type="text/css">
<title>Cadastro de Usuario</title>
<SCRIPT>
function setAcao(opcao){
ducument.FormDados.opcao.action = opcao;
ducument.FormDados.submit();
}
</SCRIPT>
</head>
<body>
<tbody>
<tr>
<td width="1032" valign="top" class="content"><h3>Cadastro de usuários:</h3>
<form name="frm_aluno" action="UsuarioServlet" method="post" TARGET= "FrameServlet">
<span class="required">*</span>Campos Obrigatorios
<table width="75%" height="257" class="table_cadastro">
<tbody><tr>
<td height="31" colspan="2" id="form_field_group">Dados do Usuário:
</td>
</tr>
<tr>
<th height="24"><span class="required">*</span>Nome:</th>
<td><input name="username" value="" size="60" maxlength="60" title="obrigatório: Curso" type="text" >
</td>
<tr>
<th height="24"><span class="required">*</span>Senha:</th>
<td>
<input name="password" size="20" maxlength="8" type="password">
</td>
</tr>
<tr>
<th height="24">Email:</th>
<td>
<input name="email" size="40" maxlength="40" type="text">
</td>
</tr>
<tr>
<th height="24"><span class="required">*</span>Telefone:</th>
<td>
<input value="" name="telefoneres" size="20" maxlength="13"type="text">
</td>
</tr>
<tr>
<th height="26"><span class="required">*</span>Celular:</th>
<td>
<input name="telefonecel" size="20" maxlength="13" type="text">
</td>
</tr>
<tr>
<td height="19" colspan="4" id="button_group">
<span id="frm_button1" class="form_button" style="width: 126px;">
<input type="submit" value="Enviar Dados" OnClick= "setAcao(CADASTRAR)">
</span>
<span id="frm_button2" class="form_button" style="width: 126px;">
<input type="reset" value="Limpar">
</span>
<span id="frm_button3" class="form_button" style="width: 126px;">
<input type="button" onClick="location.href='index.jsp'" value="Voltar">
</span>
</td>
</tr>
</tbody>
</table>
</form></td>
</tr>
<tr>
<td class="footer" colspan="2"> </td>
</tr>
</tbody>
</body>
</html>
usuarioservlet.java
package Servlet;
import Bean.UsuarioBean;
import Dao.UsuarioDao;
import javax.servlet.*;
import javax.servlet.http.*;
public class UsuarioServlet extends HttpServlet {
protected void Post(HttpServletRequest rqt, HttpServletResponse resp) throws ServletException{
try{
UsuarioBean cli = new UsuarioBean();
UsuarioDao dao = new UsuarioDao();
cli.setIdusuario(Integer.parseInt(rqt.getParameter("idusuario")));
cli.setUsername(rqt.getParameter("username"));
cli.setPassword(rqt.getParameter("password"));
cli.setEmail(rqt.getParameter("email"));
cli.setTelefoneres(rqt.getParameter("telefoneres"));
cli.setTelefonecel(rqt.getParameter("telefonecel"));
String opcao = rqt.getParameter("opcao");
if(opcao.equals("CADASTRAR")){
dao.Cadastrar(cli);
RequestDispatcher saida = rqt.getRequestDispatcher("status.jsp");
saida.forward(rqt, resp);
}
else if(opcao.equals("CONSULTAR")){
dao.Consultar(cli.getIdusuario());
RequestDispatcher saida = rqt.getRequestDispatcher("status.jsp");
saida.forward(rqt, resp);
}
else if(opcao.equals("ALTERAR")){
dao.Alterar(cli);
RequestDispatcher saida = rqt.getRequestDispatcher("status.jsp");
saida.forward(rqt, resp);
}
else if(opcao.equals("EXCLUIR")){
dao.Excluir(cli);
RequestDispatcher saida = rqt.getRequestDispatcher("status.jsp");
saida.forward(rqt, resp);
}
}catch(Exception e){
}
}
}
usuariodao.java
package Dao;
import Bean.UsuarioBean;
import java.sql.*;
public class UsuarioDao {
private Connection conexao = null;
public UsuarioDao() throws ClassNotFoundException {
this.conexao = Fabrica.getConexao();
}
public boolean Cadastrar(UsuarioBean cli){
try{
String comando = "INSERT INTO USUARIO (username,password,email,telefoneres,telefonecel) VALUES (?,?,?,?,?,?)";
PreparedStatement SMT = conexao.prepareStatement(comando);
SMT.setString(1, cli.getUsername());
SMT.setString(2, cli.getPassword());
SMT.setString(3, cli.getEmail());
SMT.setString(4, cli.getTelefoneres());
SMT.setString(5, cli.getTelefonecel());
boolean sucesso = !SMT.execute();
return sucesso;
} catch (SQLException e){
return false;
}
}
public boolean Alterar(UsuarioBean cli) {
try {
String comando = "" +
" UPDATE USUARIO set USERNAME = ?, " +
" PASSWORD = ?, " +
" EMAIL = ?, " +
" TELEFONERES = ?, " +
" TELEFONECEL = ? " +
" where IDUSUARIO = ? ";
PreparedStatement SMT = conexao.prepareStatement(comando);
SMT.setString(1, cli.getUsername());
SMT.setString(2, cli.getPassword());
SMT.setString(3, cli.getEmail());
SMT.setString(4, cli.getTelefoneres());
SMT.setString(5, cli.getTelefonecel());
boolean sucesso = !SMT.execute();
return sucesso;
} catch (SQLException e) {
return false;
}
}
public boolean Excluir(UsuarioBean cli){
try {
String comando = "DELETE FROM USUARIO WHERE IDUSUARIO = ?";
PreparedStatement SMT = conexao.prepareStatement(comando);
SMT.setInt(1, cli.getIdusuario());
boolean sucesso = !SMT.execute();
return sucesso;
} catch (SQLException e) {
return false;
}
}
public UsuarioBean Consultar(int IDUSUARIO){
try {
String comando = "SELECT * FROM USUARIO WHERE idusuario = ?";
PreparedStatement SMT = conexao.prepareStatement(comando);
SMT.setInt(1, IDUSUARIO);
ResultSet resultado = SMT.executeQuery();
if (resultado.next()) {
UsuarioBean cli = new UsuarioBean();
cli.setIdusuario(resultado.getInt("Idusuario"));
cli.setUsername(resultado.getString("Username"));
cli.setPassword(resultado.getString("Password"));
cli.setEmail(resultado.getString("Email"));
cli.setTelefoneres(resultado.getString("Telefoneres"));
cli.setTelefonecel(resultado.getString("Telefonecel"));
return cli;
}else{
return null;
}
}catch (SQLException e) {
return null;
}
}
}
fabricaconexao.java
package Dao;
import java.sql.*;
public class Fabrica{
public static Connection getConexao() {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = "jdbc:mysql://127.0.0.1:3306/trabalho";
String usuario = "root";
String senha = "";
return DriverManager.getConnection(url, usuario, senha);
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
return null;
} catch (InstantiationException ex) {
ex.printStackTrace();
return null;
} catch (IllegalAccessException ex) {
ex.printStackTrace();
return null;
} catch (SQLException e) {
return null;
}
}
}
usuarioBean.java
package Bean;
public class UsuarioBean {
private int idusuario;
private String username;
private String password;
private String email;
private String telefoneres;
private String telefonecel;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getIdusuario() {
return idusuario;
}
public void setIdusuario(int idusuario) {
this.idusuario = idusuario;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTelefonecel() {
return telefonecel;
}
public void setTelefonecel(String telefonecel) {
this.telefonecel = telefonecel;
}
public String getTelefoneres() {
return telefoneres;
}
public void setTelefoneres(String telefoneres) {
this.telefoneres = telefoneres;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
Obrigado a todos