Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Control;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- /**
- *
- * @author Zapata
- */
- public class Conexion2 {
- private String USERNAME = "root";
- private String PASSWORD = "root";
- private String HOST = "localhost";
- private String PORT = "3306";
- private String DATABASE = "registro";
- private String CLASSNAME = "com.mysql.jdbc.Driver";
- private String URL = "jdbc:mysql://"+HOST+":"+PORT+"/"+DATABASE;
- private Connection con;
- public Conexion2(){
- try{
- Class.forName(CLASSNAME);
- con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
- } catch (ClassNotFoundException e){
- System.err.println("ERROR" + e);
- } catch (SQLException e){
- System.err.println("ERROR"+ e);
- }
- }
- public Connection getConnection(){
- return con;
- }
- }
- public class Consultas2 extends Conexion2 {
- public boolean registrar (String nombre, String contraseña){
- PreparedStatement pst = null;
- try {
- String registrar = "insert into usuarios (nombre, contraseña) values(?, ?)";
- pst = getConnection().prepareStatement(registrar);
- pst.setString(1, nombre);
- pst.setString(2, contraseña);
- /* if(Nombre == null && Apellido== null && Direccion== null && Imail==null && Contraseña == null && nivel == null){
- System.out.println("llena todos los campos");
- }*/
- if (pst.executeUpdate()== 1){
- return true;
- }
- } catch (Exception ex) {
- System.err.println("Error" + ex);
- }finally{
- try {
- if (getConnection() != null)getConnection().close();
- if (pst != null) pst.close();
- } catch (Exception e) {
- System.err.println("Error" + e);
- }
- }
- return false;
- }
- }
- package com.servlet;
- import Control.Consultas2;
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- /**
- *
- * @author Zapata
- */
- public class MiServlet extends HttpServlet {
- /**Connection
- * 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 {
- response.setContentType("text/html;charset=UTF-8");
- PrintWriter out = response.getWriter();
- /* TODO output your page here. You may use following sample code. */
- }
- // <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 {
- response.setContentType("text/html;charset=UTF-8");
- PrintWriter out = response.getWriter();
- String nombre= request.getParameter("nombre");
- String contraseña= request.getParameter("contraseña");
- Consultas2 co = new Consultas2();
- if(co.registrar(nombre, contraseña)){
- out.println("registro exitoso");
- }else{
- out.println("no se pudo registrar");
- }
- }
Add Comment
Please, Sign In to add comment