【问题标题】:mysql jdbc connection no error but doesnt work on the web pagemysql jdbc连接没有错误但在网页上不起作用
【发布时间】:2016-09-10 16:30:05
【问题描述】:

我必须做一个网页,使用 jdbc 和 mysql 从数据库中选择数据。看起来没有任何问题。但是页面并没有从数据库中获取数据库值,它只是显示列名。我该怎么做才能在网页的表格上显示数据库值?

那是我的员工班:

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;
            }

}

我有kisiler课

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;
}

}

和我的 html 代码:

<?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>

【问题讨论】:

  • 我认为您将数据表的值命名错误。由于您的方法名称是:getTablodakiKayitlar,您应该将其命名为value="#{staff.tablodakiKayitlar}",如果它有效,我会添加作为答案。 CamelCase 真的很重要
  • 感谢您的评论,但它仍然无法正常工作,表是这样的i.hizliresim.com/6n7XNW.png
  • 那么你检查过你的数据库表中是否真的有数据吗?在填充它的同时添加System.out.println(),然后查看控制台上显示的内容。如果列表是空的,我也只是注意到其他问题:您的列&lt;/h:column&gt;&gt; 上的错误很少见双 &gt; ?这可能会导致错误。
  • 我现在都做了所有这些我得到一个错误“/index.xhtml @18,65 value="#{kayitCekObjesi.id}": Property 'id' not readable on type Kisiler" 我在我的主要信息中也添加了 kisiler 类
  • 您的班级没有问题Kisiler 我会尝试从页面中删除该列以查看错误是否仅与ID有关,如果不是,可能是因为您的错误更新容器(tomcat,码头或任何你正在使用的)尝试清理代码(如果是eclipse,项目清理)也清理你的容器右键,清理

标签: java html mysql jdbc


【解决方案1】:

首先请通过调试检查数据库中的值是否实际返回。如果您看到实际返回的值;然后检查 kisiler 类是否有这些属性 'id;firstName;.lastName 在 kisiler 类中的 getter 方法。请检查是否有kisiler 类中这些属性的 getter 方法。

【讨论】:

  • 现在我得到一个错误 "/index.xhtml @18,65 value="#{kayitCekObjesi.id}": Property 'id' not readable on type Kisiler" 我也添加了 kisiler 类在我的主要信息中
  • stackoverflow.com/questions/21887122/… .please see this link.your kisiler class需要公开。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-24
  • 2012-03-07
  • 2016-01-17
  • 1970-01-01
  • 2014-06-20
  • 1970-01-01
相关资源
最近更新 更多