Ver Mensaje Individual
  #3 (permalink)  
Antiguo 13/05/2012, 12:02
Avatar de teto100
teto100
 
Fecha de Ingreso: mayo-2012
Mensajes: 7
Antigüedad: 12 años
Puntos: 0
Pregunta Respuesta: Programando con Jsp,Servlets y SqlServer2008

Gracias por tomarte el tiempo de contestar .

bueno ya me esta tomando forma el código;
Ya que logre conectarme a la BD , he creado una clase Sql que se utilizara para hacer consultas , insertar, updates, retornos etc.


Código Java:
Ver original
  1. package misclases;
  2. import java.sql.*;
  3. import java.util.Vector;
  4.  
  5. public class Sql {
  6.     public static String Linea="";
  7.     // Ejecuta Insert, Delete y Update. Retorna null si todo bien, caso contrario, el mensaje de error
  8.     static public String ejecuta(String sql) {
  9.         String mensaje= null;
  10.        
  11.         try {
  12.             Conexion    db = new Conexion();
  13.             Connection  cn = db.getConnection();
  14.            
  15.             if (cn == null) {
  16.                 mensaje = "No hay conexión a la base de datos...!";
  17.             } else {
  18.                
  19.                 Statement st = cn.createStatement();
  20.                 st.execute(sql);
  21.                 st.close();
  22.                 cn.close();
  23.             }
  24.         } catch(SQLException e) {
  25.             mensaje= e.getMessage();
  26.         } catch(Exception e) {
  27.             mensaje= e.getMessage();
  28.         }
  29.        
  30.         return mensaje;
  31.     }
  32.    
  33.     // Ejecuta Select simple
  34.     static public Vector consulta(String sql) {
  35.         Vector regs = new Vector();
  36.        
  37.         try {
  38.             Conexion        db = new Conexion();
  39.             Connection      cn = db.getConnection();
  40.            
  41.             if (cn == null) {
  42.                 regs = null;
  43.             } else {
  44.                 Statement       st = cn.createStatement();
  45.                 ResultSet       rs = st.executeQuery(sql);
  46.                 ResultSetMetaData   rm = rs.getMetaData();
  47.                 int         numCols = rm.getColumnCount();
  48.                
  49.                 // Toma los títulos de las columnas
  50.                 String[] titCols= new String[numCols];
  51.                 for(int i=0; i<numCols; ++i)
  52.                     titCols[i]= rm.getColumnName(i+1);
  53.                
  54.                 // la fila 0 del vector lleva los títulos de las columnas
  55.                 regs.add(titCols);
  56.                
  57.                 // toma las filas de la consulta
  58.                 while(rs.next()) {
  59.                     String[] reg= new String[numCols];
  60.                    
  61.                     for(int i=0; i<numCols; i++) {
  62.                         reg[i] = rs.getString(i + 1);
  63.                     }
  64.                    
  65.                     regs.add(reg);
  66.                        
  67.                        
  68.                 }
  69.                
  70.                 rs.close();
  71.                 st.close();
  72.                 cn.close();
  73.             }
  74.            
  75.         } catch(SQLException e) {
  76.             regs= null;
  77.         } catch(Exception e) {
  78.             regs= null;
  79.         }
  80.        
  81.         return regs;
  82.     }
  83.    
  84.     static public String ver (String sql){
  85.        String regs="";
  86.      
  87.        
  88.        try{
  89.             Conexion        db = new Conexion();
  90.             Connection      cn = db.getConnection();
  91.            
  92.             if (cn == null) {
  93.                 regs = null;
  94.             } else {
  95.                 Statement       st = cn.createStatement();
  96.                 ResultSet       rs = st.executeQuery(sql);
  97.                 ResultSetMetaData   rm = rs.getMetaData();
  98.              
  99.                
  100.              
  101.                
  102.                 // toma las filas de la consulta
  103.                 while(rs.next()) {
  104.                Linea += ("Codigo Producto" + rs.getString("Cod_Producto")) + "\t";
  105.                Linea += ("Nombre Producto" + rs.getString("Nombre")) + "\t";
  106.                Linea += ("Marca Producto" + rs.getString("Marca")) + "\n";
  107.            
  108.                
  109.          }
  110.                    
  111.            
  112.             }
  113.        }catch(Exception e){
  114.            
  115.        }
  116.        
  117.         return regs;
  118.     }
  119.    
  120.     // Retorna una sola fila
  121.     static public String[] getFila(String sql) {
  122.         Vector vector = consulta(sql);
  123.         String[] fila = null;
  124.        
  125.         if(vector!=null)                // todo OK!
  126.             if(vector.size()>1)             // hay filas
  127.                 fila = (String[]) vector.get(1);    // en 0 están los títulos
  128.        
  129.         return fila;
  130.     }
  131.    
  132.     // Retorna un solo campo
  133.     static public String getCampo(String sql) {
  134.         String[] fila = getFila(sql);
  135.         String campo = null;
  136.        
  137.         if(fila!=null)      // hay campo
  138.             campo = fila[0];
  139.        
  140.         return campo;
  141.     }
  142. }



Ademas tengo 2 jsp y un Servlet.

el index.jsp solo tiene un boton un action para que lo mande al servlet (Listar) y esta con metodo post.

Código Java:
Ver original
  1. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  2. <!DOCTYPE html>
  3. <html>
  4.     <head>
  5.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  6.         <title>JSP Page</title>
  7.     </head>
  8.     <body>
  9.         <form name="produ" action="<%=request.getContextPath()%>/Listar" method="post">
  10.                  
  11.             <input type="submit" name="btnEnviar" value="VER">
  12.         </form>
  13.     </body>
  14. </html>



Tengo un servlet (Listar) que se acciona con el boton del index.jsp y que utiliza la clase SQL

Código Java:
Ver original
  1. package misclases;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import javax.servlet.ServletException;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import java.sql.Connection;
  10. import java.sql.ResultSet;
  11. import java.sql.PreparedStatement;
  12. import java.sql.Statement;
  13. import java.util.Vector;
  14. import javax.servlet.RequestDispatcher;
  15.  
  16.  
  17. public class Listar extends HttpServlet {
  18.  
  19.      
  20.     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  21.             throws ServletException, IOException {
  22.         response.setContentType("text/html;charset=UTF-8");
  23.         PrintWriter out = response.getWriter();
  24.     }
  25.  
  26.     @Override
  27.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  28.             throws ServletException, IOException {
  29.         processRequest(request, response);
  30.     }
  31.  
  32.     @Override
  33.    
  34.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  35.             throws ServletException, IOException {
  36.         processRequest(request, response);
  37.         response.setContentType("text/html;charset=UTF-8");
  38.         PrintWriter out = response.getWriter();
  39.      
  40.         try {
  41.      
  42.          String l = Sql.ver("Select * from Productos");
  43.          response.sendRedirect("Productos.jsp");  
  44.         } catch (Exception ex) {
  45.         }
  46.        
  47.     }
  48.    
  49.     @Override
  50.     public String getServletInfo() {
  51.         return "Short description";
  52.     }// </editor-fold>
  53. }


Aqui tengo el problema porque no se como mandar esos datos que ha recogido al siguiente JSP (Productos,jsp) dentro de un textarea

aqui mi Producto.jsp


Código java:
Ver original
  1. <%@page import="misclases.Sql"%>
  2. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  3. <!DOCTYPE html>
  4. <html>
  5.     <head>
  6.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7.         <title>JSP Page</title>
  8.     </head>
  9.     <body bgcolor="e0e0f8">
  10.         <form value="Miprodu">
  11.             <textarea id="teto">
  12.                 <%
  13.                 String Linea = Sql.Linea;
  14.                 System.out.println(Linea);
  15.                 %>
  16.             </textarea>
  17.             <a href = "index.jsp">  << REGRESAR </a>
  18.          
  19.     </form>
  20.             </body>
  21.             </html>
ALguien que pueda ayudar? Saludos y Muchas gracias.

Última edición por teto100; 13/05/2012 a las 12:40