简体   繁体   English

如何从数组中为 Jtextfield 设置文本?

[英]How can set text for Jtextfield from the array?

I have a problem when doing my project.我在做我的项目时遇到了问题。 I connect my database and get database about the value of food.我连接我的数据库并获取有关食物价值的数据库。 I put all of the value to the array (public static array).我将所有值放入数组(公共 static 数组)。 But I wondering that anyway can I set the text of the textfield by the value from the array?但我想知道无论如何我可以通过数组中的值设置文本字段的文本吗? Here is my declaring array:这是我的声明数组:

public static String[] fp;

----------- main() - - - - - - 主要的()

fp = new String[9];
            try {
                ResultSet rs = st.executeQuery(get_food_price);
                int i =0;
                while (rs.next()) {
                    System.out.println(rs.getString(1));
                    fp[i] = rs.getString(1);
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

----------- private void initialize() { ----------- 私人无效初始化(){

food1_price.setText(fp[1]);

------------ It's not working with fp[1] (array) but It do work with normal string like "acb" or "asdsa" ------------ 它不适用于 fp[1] (数组),但它确实适用于普通字符串,如“acb”或“asdsa”

Can you try this?你能试试这个吗?

    MySQLConnection db = new MySQLConnection();
    Connection connect = db.getmysql_connection();
    String query="SELECT * FROM "; //Your query
    Statement stmt = (Statement) connect.createStatement();
    ResultSet rs = stmt.executeQuery(query);
    int i=0;
    while (rs.next()) {
        System.out.println(rs.getString(1));
        fp[i] = rs.getString(1);
        i++;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM