首页 > 解决方案 > 为什么我的 .setTexts 出现空白?

问题描述

我必须将访问数据库连接到 netbeans 项目并创建一个程序,该程序允许用户搜索足球运动员的姓名,并将其结果显示在屏幕上。我的问题是,当我最后执行 setTexts 时,标签只是变成空白。我没有收到任何错误消息。

我不知道问题出在链接到数据库还是在解析参数上,还是在其他地方?

    Connection conn = null;
    PreparedStatement pst = null;
    ResultSet rst = null;
    String temp = new String();
    String playerID = null;
    String name = null;
    String  surname = null;
    String shirtNo = null;
    String height = null;
    String prefFoot = null;
    String nation = null; 
 try
 {
    Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
    conn = DriverManager.getConnection("jdbc:ucanaccess://Database4.accdb");
    String sql = "SELECT * FROM Players WHERE Name = (?) and Surname = (?)";
    pst = conn.prepareStatement(sql);
    pst.setString(1, txtfieldFirst.getText());
    pst.setString(2, txtfieldSecond.getText());
    rst = pst.executeQuery();
    if(rst.next())
    {
        int count = 0;
        while(rst.next())
        {
            playerID = rst.getString("PlayerID");

            name = rst.getString("Name");
            surname = rst.getString("Surname");
            shirtNo = rst.getString("ShirtNo");
            height = rst.getString("Height (Metres)");
            prefFoot = rst.getString ("PrefFoot");
            nation = rst.getString ("Nation"); 
        }
    }
    conn.close();
 }
 catch (Exception e)
 {
    System.out.println(e);
 }

    Connection con = null;
    PreparedStatement pstt = null;
    ResultSet rs = null;
    String temp2 = new String();
    String league = null;
    String DOB = null;
    String club = null;
    boolean tec = false;
    String goals15 = null;
    String goals16 = null;
    String goals17 = null;

    String assists15 = null;
    String assists16 = null;
    String assists17 = null;

 try
 {
    Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
    con = DriverManager.getConnection("jdbc:ucanaccess://Database4.accdb");
    String sqll = "SELECT * FROM Stats WHERE PlayerID = (?)";
    pstt = con.prepareStatement(sqll);
    pstt.setString(1, playerID);
    rs = pstt.executeQuery();
    if(rs.next())
    {
        int count = 0;
        while(rs.next())
        {
            league = rs.getString("League");
            DOB = rs.getString("DOB");
            club = rs.getString("Club");
            goals15 = rs.getString("Goals15/16");
            goals16 = rs.getString("Goals16/17");
            goals17 = rs.getString("Goals17/18");
            assists15 = rs.getString("Assists15/16");
            assists16 = rs.getString("Assists16/17");
            assists17 = rs.getString("Assists17/18"); 
        }
    }
    con.close();
 }

 catch (Exception e)
 {
    System.out.println(e);
 }
    babyStats pps = new babyStats(league, DOB, club, goals15, goals16, goals17, assists15, assists16, assists17);
    babyPlayers ps = new babyPlayers(name, surname, shirtNo, height, prefFoot, nation);

    PlayerScreen p = new PlayerScreen(ps, pps); //connection to other screen
    p.setVisible(true);
    this.setVisible(false);


   public PlayerScreen(babyPlayers obj, babyStats objj) //parsed as paramters
{
    initComponents();

    lblName.setText(obj.getName());
    lblSurname.setText(obj.getName());
    lblShirtNo.setText(obj.getShirtNo());
    lblDOB.setText(objj.getDOB());
    lblHeight.setText(obj.getHeight());
    lblPFoot.setText(obj.getPreFoot());
    lblClub.setText(objj.getClub());
    lblNation.setText(obj.getNation()); //these setTexts just make the labels blank
    lblGoals16.setText(objj.getGoals1516());
    lblGoals17.setText(objj.getGoals1617());
    lblGoals18.setText(objj.getGoals1718());
    lblAssists16.setText(objj.getAssists1516());
    lblAssists17.setText (objj.getAssists1617());
    lblAssists18.setText (objj.getAssists1718());   
}

我希望使用来自数据库的详细信息来设置标签,但它们会变成空白。我真的很感激任何帮助。*更新,在调试时,我使用 system.out.println 打印其中一个名称,结果为空。*更新,我修复了它,while循环不应该在那里。

标签: javanetbeanssettext

解决方案


推荐阅读