Kashub's Code Barn - "JSP i MySQL"

podświetlone jako java (dodał(a) Kashub @ 2007-07-12 23:36:29)

Twoja wyszukiwarka
Podświetl ten kod w:
Ostatnio dodane:
Losowe wpisy:
/*
 * serwletJDBC.java
 *
 * Created on 29 kwiecien 2007, 11:40
 */
 
package serwlety;
 
import java.io.*;
import java.net.*;
import java.sql.*;
 
import javax.servlet.*;
import javax.servlet.http.*;
 
/**
 *
 * @author student1
 * @version
 */
public class serwletJDBC extends HttpServlet {
 
    /** Initializes the servlet.
     */
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
 
    }
 
    /** Destroys the servlet.
     */
    public void destroy() {
 
    }
 
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
 
        try {
            DriverManager.registerDriver(new org.gjt.mm.mysql.Driver());
            Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3307/sklep", "root", "root");
            Statement stmt = conn.createStatement();
            ResultSet rset = stmt.executeQuery("select * from Produkty");
            /*
            out.println("<table border=\"1\">");
            out.println("<tr><td>id</td><td>nazwa</td><td>cena</td></tr>");
            while(rset.next()) {
                out.println("<tr>");
                out.println("<td>"+rset.getString("id")+"</td>");
                out.println("<td>"+rset.getString("name")+"</td>");
                out.println("<td>"+rset.getString("price")+"</td>");
                out.println("</tr>");
            }
            out.println("</table>");
            */
 
            out.println("<form method=\"get\">");
            out.println("<select name=\"id\">");
            while(rset.next()) {
                out.println("<option ");
                out.println("value=\""+rset.getString("id")+"\">");
                out.println(rset.getString("name")+"</option>");
            }
            out.println("</select><input type=\"submit\" value=\"Pytaj\"</form>");
 
 
 
        }
        catch (SQLException e) {
            out.println("Blad: " +e);
        }
 
        out.close();
    }
 
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        if(request.getParameter("id") != null) {
            PrintWriter out = response.getWriter();
            int id_prod = Integer.parseInt(request.getParameter("id"));
 
 
           // out.print(id_prod);
 
 
 
 
                    try {
            DriverManager.registerDriver(new org.gjt.mm.mysql.Driver());
            Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3307/sklep", "root", "root");
            Statement stmt = conn.createStatement();
            ResultSet rset = stmt.executeQuery("select * from Produkty where id=" + id_prod);
            out.println("<table border=\"1\">");
            out.println("<tr><td>id</td><td>nazwa</td><td>cena</td></tr>");
            while(rset.next()) {
                out.println("<tr>");
                out.println("<td>"+rset.getString("id")+"</td>");
                out.println("<td>"+rset.getString("name")+"</td>");
                out.println("<td>"+rset.getString("price")+"</td>");
                out.println("</tr>");
            }
            out.println("</table><a href=\"javascript:history.go(-1)\">&lt;-- powrót</a>");
 
 
 
        }
        catch (SQLException e) {
            out.println("Blad: " +e);
        }
 
        out.close();
 
 
 
 
 
 
 
 
 
 
        }
        else {
            processRequest(request, response);
        }
    }
 
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
 
    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
 
}
| Sklep z artykułami dla dzieci | | Opony całoroczne | | Opony letnie | | karma dla psa - sklep | | Skróć link | | Blogi za darmo | | Przenieś bloga z onetu | | Pionowe opisy |