简体   繁体   中英

JSP and tables - Won't start the code

I don't get any error in Netbeans, but it doesn't execute nothing except the "Hola?" that i put for test, I think the main error is in the Printwriter command, it doesn't generate anything in my display,

<%@page import="java.io.*"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Página Nacho</title>
</head>
    <%@page import="ejemplo.BaseDatos" %>
    <%  out.println("Hola?");
        out.println("<body>");
            BaseDatos b = new BaseDatos();
            PrintWriter salida = response.getWriter();
            if (request.getParameter("Agregar")!= null){
            boolean inserto = b.inserta (
            request.getParameter("nombre"),
            request.getParameter("apellido_p"),
            request.getParameter("apellido_m"),
            request.getParameter("direccion"),
            request.getParameter("telefono"),
            request.getParameter("email")
            );
            if(inserto){
                salida.println("<br>Usuario agregado"); 
            }else{
                salida.println("<br>No fue posible agregar el usuario"); 
            }
            }else{
            //Agrega la funcionalidad de consultar todos los usuarios
            }
        out.println("</body>");
    %>
</html>

This is the java file for this code, it's named on spanish, but I think nothing is too difficult:

package ejemplo;

import java.sql.*;
public class BaseDatos {
 Connection conexion = null;
 public BaseDatos(){
 conexion = null;
 }
 private boolean conecta(){
try {
 String urlBD = "jdbc:mysql://localhost/practica2?user=root&password="; 
 Class.forName("com.mysql.jdbc.Driver").newInstance();
 conexion = DriverManager.getConnection(urlBD);
 } catch (Exception e) { 
 System.out.println(e);
 return false;
 }
 return true;
 }
 public boolean inserta(String nom, String ap_p, String ap_m, String dir, String tel,                      String email){
 try {
 if (conecta()){
 String q = "insert into     personas (nombre,apellido_p,apellido_m,direccion,telefono,email) values(?,?,?,?,?,?)";
 PreparedStatement i = conexion.prepareStatement(q);
 i.setString(1, nom);
 i.setString(2, ap_p);
 i.setString(3, ap_m);
 i.setString(4, dir);
 i.setString(5, tel);
 i.setString(6, email);
 i.executeUpdate();
  i.close(); 
 conexion.close();
 return true;
 }else{
 return false;
 }
 } catch (Exception e){
 System.out.println(e);
 return false;
 }
 }

 public String tablaUsuarios(){
 try{
 if(conecta()){
 String q="select * from personas";
 PreparedStatement p=conexion.prepareStatement(q);
 ResultSet r=p.executeQuery();
 String tabla="<table border=\"3\" align=\"center\">";
 tabla+="<tr bgcolor=blue><th align=center><font color=white>Nombre</font></th>";
 tabla+="<th align=center><font color=white>Apellido</font></th></tr>";
 while(r.next()){
    tabla+="<tr><td>"+r.getString("nombre")+"</td><td>"+r.getString("apellido_p")+"        </td></tr>";
 }
 r.close();
 p.close();
 conexion.close();
 return tabla;
 }else{
 return "";
 }
 }catch(Exception e) { 
 System.out.println(e);
 return "";
 }
 }
}

Thanks in advanced for all the help.

There is a whole bunch of URL parameters that the JSP is expecting, the least is the parameter called "Agregar". You need to pass these parameters to the page for it to do further processing.

if (request.getParameter("Agregar")!= null){
            boolean inserto = b.inserta (
            request.getParameter("nombre"),
            request.getParameter("apellido_p"),
            request.getParameter("apellido_m"),
            request.getParameter("direccion"),
            request.getParameter("telefono"),
            request.getParameter("email")
            );
            if(inserto){
                salida.println("<br>Usuario agregado"); 
            }else{
                salida.println("<br>No fue posible agregar el usuario"); 
            }
            }else{
            //Agrega la funcionalidad de consultar todos los usuarios
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM