简体   繁体   中英

mysql jdbc connection no error but doesnt work on the web page

I have to do a an web page that is choosing datas from database with jdbc and mysql. There is not look any problem. But page doesnt take the database values from database, it is just shows column name.What can i do to show database values on the table on the web page?

That is my staff class:

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.ResultSet;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.List;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 *

 */
public class Staff {
    List <Kisiler> sorguSonucu;

    public List<Kisiler> getSorguSonucu(List<Kisiler> sorguSonucu){
        return sorguSonucu;
    }
    public List<Kisiler> getTablodakiKayitlar()
    {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        sorguSonucu = new ArrayList<Kisiler>();

        try{
            Class.forName("com.mysql.jdbc.Driver");

            String url = "jdbc:mysql://localhost:3309/staff";
            String user = "admin";
            String psw = "admin";
            Class.forName("com.mysql.jdbc.Driver");
            connection = (Connection) DriverManager.getConnection(url, user, psw);

            preparedStatement = (PreparedStatement) connection.prepareStatement("SELECT * FROM staff");
            resultSet = (ResultSet) preparedStatement.executeQuery();
            while(resultSet.next()){
                Kisiler kisiler = new Kisiler();
                kisiler.setId(resultSet.getInt("id"));
                kisiler.setFirstName(resultSet.getString("firstName"));
                kisiler.setLastName(resultSet.getString("lastName"));
                kisiler.setTelephone(resultSet.getInt("telephone"));
                kisiler.setEmail(resultSet.getString("email"));
                sorguSonucu.add(kisiler);
            }
        }
        catch(Exception e){
            System.err.println("Hata meydana geldi.Hata: " + e);
        }

        finally{
            try{
                connection.close();
                preparedStatement.close();
            }
            catch(Exception e){
                System.err.println("Hata meydana geldi.Hata: " + e);
            }
        }

    return sorguSonucu;
            }

}

and i have kisiler class

class Kisiler {

    private int id;
private String firstName;
private String lastName;
private int telephone;
private String email;

/**
 * @return the id
 */
public int getId() {
    return id;
}

/**
 * @param id the id to set
 */
public void setId(int id) {
    this.id = id;
}

/**
 * @return the firstName
 */
public String getFirstName() {
    return firstName;
}

/**
 * @param firstName the firstName to set
 */
public void setFirstName(String firstName) {
    this.firstName = firstName;
}

/**
 * @return the lastName
 */
public String getLastName() {
    return lastName;
}

/**
 * @param lastName the lastName to set
 */
public void setLastName(String lastName) {
    this.lastName = lastName;
}

/**
 * @return the telephone
 */
public int getTelephone() {
    return telephone;
}

/**
 * @param telephone the telephone to set
 */
public void setTelephone(int telephone) {
    this.telephone = telephone;
}

/**
 * @return the email
 */
public String getEmail() {
    return email;
}

/**
 * @param email the email to set
 */
public void setEmail(String email) {
    this.email = email;
}

}

and my html codes:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
    <h:head>
        <title>View Staff</title>
    </h:head>
    <h:body>
        <h:form>

            <h:dataTable value="#{staff.tablodakikayitlar}" var="kayitCekObjesi"
                         >
                <h:column>
                    <f:facet name="header">
                        ID
                    </f:facet>
                    <h:outputText value="#{kayitCekObjesi.id}"/>
                </h:column>
                <h:column>
                    <f:facet name="header">
                        First Name
                    </f:facet>
                    <h:outputText value="#{kayitCekObjesi.firstName}"/>
                </h:column>
                <h:column>
                    <f:facet name="header">
                        Last Name
                    </f:facet>
                    <h:outputText value="#{kayitCekObjesi.lastName}"/>
                </h:column>
                <h:column>
                    <f:facet name="header">
                        Telephone
                    </f:facet>
                    <h:outputText value="#{kayitCekObjesi.telephone}"/>
                </h:column>>
                <h:column>
                    <f:facet name="header">
                        E-mail
                    </f:facet>
                    <h:outputText value="#{kayitCekObjesi.email}"/>
                </h:column>>
           </h:dataTable>
        </h:form>
    </h:body>
</html>

首先请通过调试检查数据库中的值是否实际返回;如果看到值实际返回;则检查kisiler类是否具有针对该属性'id; firstName; .lastName的getter方法。请检查是否存在针对该方法的getter方法kisiler类中的那些属性。

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