
Plasmar en la página web desde JSP los registros ya grabados de una BD de SQL Server
Publicado por Percy (3 intervenciones) el 20/02/2016 07:41:39
AYÚDENME POR FAVOR LO MAS RÁPIDO POSIBLE EN LA CODIFICACIÓN DE mostrar_usuarios.jsp QUE ESTÁ MÁS ABAJO:
(Adjunto la Base de Datos "MVC_SQLQuery1.rar")
CODIFICACIÓN DE LA CLASE ServletUsuario.java (Paquete = org.me.servlet):
CODIFICACIÓN DE mostrar_usuarios.jsp (arreglar este código por favor, ayúdenmeee):
CODIFICACIÓN DE LA CLASE Usuario.java (paquete = org.me.bean):
CODIFICACIÓN DE LA CLASE UsuarioDAO.java (paquete = org.me.dao) :
CODIFICACION de la clase ConectaDB.java del PAQUETE=com.me.sql
del archivo NLib que es un Java Class Library:
(Adjunto la Base de Datos "MVC_SQLQuery1.rar")
CODIFICACIÓN DE LA CLASE ServletUsuario.java (Paquete = org.me.servlet):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package org.me.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.me.bean.Usuario;
import org.me.dao.UsuarioDAO;
/**
*
* @author FAMARESAM-PC
*/
public class ServletUsuario 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, Exception {
String accion = request.getParameter("accion");
PrintWriter out = response.getWriter();
try {
if (accion == null) {
request.getSession().setAttribute("mensaje", "llegó nulo!");
} else if (accion.compareTo("ACCESO") == 0) {
//solicitud de parámetros
String nick = request.getParameter("nick");
String password = request.getParameter("password");
//encapsulando datos en el bean
Usuario usuario = new Usuario();
usuario.setNick(nick);
usuario.setPassword(password);
//operación DAO
String nombre = UsuarioDAO.getNombre(usuario);
//mensaje de exito o fracaso
if (nombre == null) {
request.getSession().setAttribute("msg", "Usuario no existe!");
} else {
request.getSession().setAttribute("msg", "Bienvenido " + nombre);
}
} else if (accion.compareTo("INS") == 0) {
//sentencias para insertar usuario
//solicitud de parámetros
String idUsuario = request.getParameter("idUsuario");
String nombre = request.getParameter("nombre");
String nick = request.getParameter("nick");
String password = request.getParameter("password");
//encapsulando datos en el bean
Usuario usuario = new Usuario();
UsuarioDAO usuDAO = new UsuarioDAO();
usuario.setIdUsuario(idUsuario);
usuario.setNombre(nombre);
usuario.setNick(nick);
usuario.setPassword(password);
if (usuDAO.getInsertar(usuario)) {
response.sendRedirect("mensaje.jsp");
}
//operación DAO
//mensaje de exito o fracaso
} else if (accion.compareTo("DEL") == 0) {
//sentencias para retirar usuario
} else if (accion.compareTo("UPD") == 0) {
//sentencias para actualizar datos de usuario
}
response.sendRedirect("mostrar_usuarios.jsp");
} catch (Exception e) {
out.println(e);
}
}
// <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 {
try {
processRequest(request, response);
} catch (Exception ex) {
Logger.getLogger(ServletUsuario.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* 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 {
try {
processRequest(request, response);
} catch (Exception ex) {
Logger.getLogger(ServletUsuario.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
CODIFICACIÓN DE mostrar_usuarios.jsp (arreglar este código por favor, ayúdenmeee):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page import="org.me.dao.UsuarioDAO"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Mostrar Usuarios</title>
</head>
<body>
<%
UsuarioDAO dao= new UsuarioDAO();
%>
<h1>¡Registrar usuarios!</h1>
<table border="1">
<thead>
<tr>
<th>IdUsuario</th>
<th>Nombre</th>
<th>Nick</th>
<th>Password</th>
</tr>
<tr>
<%--<%=dao.getMostrar("st") %>--%>
<%=request.getAttribute("getMostrar(cod)")%>
</tr>
</thead>
</table>
</body>
</html>
CODIFICACIÓN DE LA CLASE Usuario.java (paquete = org.me.bean):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package org.me.bean;
/**
*
* @author alumno
*/
public class Usuario {
private String idUsuario;
private String nombre;
private String nick;
private String password;
public String getIdUsuario() {
return this.idUsuario;
}
public void setIdUsuario(String idUsuario) {
this.idUsuario = idUsuario;
}
public String getNombre() {
return this.nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getNick() {
return this.nick;
}
public void setNick(String nick) {
this.nick = nick;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
}
CODIFICACIÓN DE LA CLASE UsuarioDAO.java (paquete = org.me.dao) :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package org.me.dao;
import com.me.sql.Sql;
import org.me.bean.Usuario;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.me.sql.ConectaDB;
/**
*
* @author alumno
*/
public class UsuarioDAO {
private Connection cn=ConectaDB.getConnection() ;
public static String getNombre(Usuario usuario)throws SQLException,Exception{
String nombre=Sql.getCampo("SELECT nombre FROM usuarios " +
"WHERE nick like '" + usuario.getNick()
+"' AND password like '" + usuario.getPassword() + "'");
return nombre;
}
// public static String getInsertar(Usuario usuario)throws SQLException,Exception{
// String nombre=Sql.ejecuta("INSERT INTO usuarios VALUES("+usuario.getIdUsuario()+",'"+usuario.getNombre()+"','"+usuario.getNick()+"','"+usuario.getPassword()+"')");
// return nombre;
//}
public boolean getInsertar(Usuario us) throws SQLException, Exception {
PreparedStatement pst = cn.prepareCall("{CALL PA_INSERTAR_USUARIO(?,?,?,?)}");
pst.setString(1, us.getIdUsuario());
pst.setString(2, us.getNombre());
pst.setString(3, us.getNick());
pst.setString(4, us.getPassword());
if (pst.executeUpdate() == 1) {
return true;
} else {
return false;
}
}
public String getMostrar(String cod) throws SQLException, Exception {
String r = "";
Statement st = cn.createStatement();
ResultSet rs = st.executeQuery("select * from usuarios");
while (rs.next()) {
r = r + "<tr>";
r = r + "<td>" + rs.getString(1) + "</td>";
r = r + "<td>" + rs.getString(2) + "</td>";
r = r + "<td>" + rs.getString(3) + "</td>";
r = r + "<td>" + rs.getString(4) + "</td>";
r = r + "</tr>";
}
return r;
}
}
CODIFICACION de la clase ConectaDB.java del PAQUETE=com.me.sql
del archivo NLib que es un Java Class Library:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.me.sql;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConectaDB {
//Con SQLServer
private static String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static String url = "jdbc:sqlserver://localhost:1433;databaseName=DATA";
private static String usuario = "sa";
private static String password = "123";
/*
//Con ODBC
private String driver = "sun.jdbc.odbc.JdbcOdbcDriver"; // Para ODBC
private String url = "jdbc:odbc:dsnVentas"; // URL
private String usuario = "sa"; // usuario
private String password = ""; // password
*/
/*
//Con MySQL
private String driver= "com.mysql.jdbc.Driver"; // Para MySQL
private String url= "jdbc:mysql://localhost/productos"; // URL
private String usuario= "root"; // usuario
private String password= "admin"; // password
*/
// Retorna un objeto connection. Caso contrario, null si no a sido posible.
public static Connection getConnection() {
Connection cn = null;
try {
Class.forName(driver).newInstance();
cn = DriverManager.getConnection(url, usuario, password);
//ODBC
} catch (SQLException e) {
System.out.println(e.toString());
cn = null;
//JDBC
} catch (Exception e) {
System.out.println(e.toString());
cn = null;
}
return cn; //retorna null si fracasó
}
}
- MVC_SQLQuery1.rar(558,0 B)
- appLogueo1.rar(1,6 MB)
Valora esta pregunta


0