数据库中没有显示任何内容

时间:2011-10-18 21:35:04

标签: mysql jsp

我正在尝试从表中检索信息,并将它们显示在表单中。代码没有警告,也没有错误,但数据库中没有显示任何内容。出了什么问题?谢谢。

<%@page import="com.Prog.GestionLivres"%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%-- <%@ page errorPage="erreur.jsp" %> --%>

<%@ page import="java.sql.*" %> 
<%@ page import="java.io.*" %>
<%@ page import="java.util.Enumeration" %>
<%@ page import="java.util.List,java.util.ArrayList,java.util.Iterator"%>

<HTML>
    <HEAD>
        <title>Supprimer</title>
        <link rel="stylesheet" href="style.css" type="text/css">
    </HEAD>
    <BODY>
        <%      
            Enumeration <String> names = request.getParameterNames();
            while (names.hasMoreElements())
            {
                String name = (String) names.nextElement();
                StringBuffer sb = new StringBuffer(name);
                sb.deleteCharAt(0);
                com.Prog.GestionLivres.Supprimer(sb.toString());
            }           
        %>
        <BR>
        <DIV class="navigator">
        <A href="ajouter.jsp">Ajouter</a>
        <A id="currenttab" href="supprimer.jsp">Supprimer</A>
        </DIV>
        <BR> <BR> <BR>
        <FORM action="supprimer.jsp" method="POST">
            <TABLE>
                <TR>
                    <TH>Auteur</TH>
                    <TH>Titre</TH>
                    <TH>Année</TH>
                    <TH>Remarques</TH>
                </TR>
                <%                                      
                    List <String> list = (List <String>) com.Prog.GestionLivres.LivresDisponibles();

                    int id = 0;
                    String box = null;
                    Iterator<String> it = list.iterator();

                    while (it.hasNext())
                    {
                        id = Integer.parseInt(it.next());
                        out.print("<TR>");
                        for (int i = 0; i < 4; i++)
                        {
                            out.print("<TD>");
                            out.print(it.next());
                            out.print("</TD>");
                        }
                        out.print("<TD>");
                        box = "<INPUT name=r" + id + " type=’checkbox’>";
                        out.print(box);
                        out.print("</TD>");
                        out.print("</TR>");
                    }
                %>
            </TABLE>
            <BR>
            <INPUT type="submit" value="Supprimer">
        </FORM>
    </BODY>
</HTML>


package com.Prog;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class GestionLivres
{
    static final String url = "jdbc:mysql://localhost:3306/bibliotheques";

    public static void Insert(String auteur, String titre, int annee, String remarques)
    {
        try
        {
            String insert = "INSERT INTO livres(auteur, titre, annee, remarques)" + "VALUES (?, ?, ?, ?)";
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection(url, "root", "");
            PreparedStatement ps = conn.prepareStatement(insert);
            ps.setString(1, auteur);
            ps.setString(2, titre);
            ps.setInt(3, annee);
            ps.setString(4, remarques);
            ps.executeUpdate();
            conn.close();
        }

        catch (Exception ex)
        {
            Logger.getLogger(GestionLivres.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static List <String> LivresDisponibles()
    {        
        List <String> list = new ArrayList <String>();
            try
            {
                Class.forName("com.mysql.jdbc.Driver");
                Connection conn = DriverManager.getConnection(url, "root", "");
                Statement stmt = conn.createStatement();
                ResultSet result = stmt.executeQuery("SELECT * FROM livres");
                    while(result.next())
                    {
                        list.add(result.getString("id"));
                        list.add(result.getString("auteur"));
                        list.add(result.getString("titre"));
                        list.add(result.getString("annee"));
                        list.add(result.getString("remarques"));
                    }
                conn.close();
            }

        catch (Exception ex)
        {
            Logger.getLogger(GestionLivres.class.getName()).log(Level.SEVERE, null, ex);
        }

        return list;
    }

    public static void Supprimer(String id)
    {
        try
        {
            String supprimerEnregistrement = "DELETE from livres WHERE id = ?";
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection(url, "root", "");
            PreparedStatement ps = conn.prepareStatement(supprimerEnregistrement);
            ps.setString(1, id);
            ps.executeUpdate();
            conn.close();
        }

        catch (Exception ex)
        {
            Logger.getLogger(GestionLivres.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

0 个答案:

没有答案