简体   繁体   English

如何将Java中的SQL查询结果打印到每列的各个JTextField?

[英]How can I print results from a SQL query in Java to individual JTextFields for each column?

I have writted a Java program that will be able to add customers to a database, and then look them up via a username or customer_id. 我编写了一个Java程序,可以将客户添加到数据库中,然后通过用户名或customer_id查找它们。

I am able to add users to the database without issue. 我能够毫无问题地将用户添加到数据库中。 Also, I am able to select a user by username or customer_id and print the user information into a JTextArea for viewing. 此外,我可以通过用户名或customer_id选择用户,并将用户信息打印到JTextArea中以供查看。 What I would like to do, however, is print the user information from each specific column in their row of the database to the corresponding JTextField on my form. 但是,我想要做的是将用户信息从数据库行中的每个特定列打印到表单上相应的JTextField。 I think I need to use an array to do so, but I have had no success thus far. 我想我需要使用数组才能这样做,但到目前为止我还没有成功。

The code I have at this point to select data is as follows: 此时我用来选择数据的代码如下:

public String selectCustomer() throws ClassNotFoundException, SQLException{
    String result = "";
    String strSQL = "SELECT * FROM customer WHERE username = '" + this.getUsername() + "'"; 
    DataAccess DA = new DataAccess();
    ResultSet rs;
    try{
        rs = DA.getResultSet(strSQL);
        ResultSetMetaData metaData = rs.getMetaData();
        int columns=metaData.getColumnCount();
        while(rs.next()){//reading one record
            for(int i=1;i<=columns;++i) {//this reads column by column
                result+="<"+metaData.getColumnName(i)+">";
                result+=rs.getString(i);
                result+="</"+metaData.getColumnName(i)+">\n";
            }//closes for loop
        }//closes while loop
    }//closes try       
    catch(SQLException sqle){sqle.printStackTrace();}   
    catch(Exception e){e.printStackTrace();}
    return result;
}

Now I need to alter that code to place my column results into an array, and then I should be able to simply pull the data from the array, correct? 现在我需要更改代码以将列结果放入数组中,然后我应该能够简单地从数组中提取数据,对吗? I could be completely off my rocker here. 我可以完全脱离摇杆。 I don't know. 我不知道。 D: d:

Any advice would be appreciated. 任何意见,将不胜感激。

Thanks! 谢谢!

Sure, you can get the values from the ResultSet using getString (and other getXXX methods) and then use setText to put the values into your text field. 当然,您可以使用getString(和其他getXXX方法)从ResultSet中获取值,然后使用setText将值放入文本字​​段中。

This tutorial may help: http://docs.oracle.com/javase/tutorial/jdbc/basics/retrieving.html 本教程可能有所帮助: http//docs.oracle.com/javase/tutorial/jdbc/basics/retrieving.html

PS I forgot to say, you can get the values by column name rather than column index if you like - this is slightly slower but makes the code easier to write and maintain. PS我忘了说,你可以通过列名而不是列索引来获取值 - 如果你愿意的话 - 这稍微慢一些但是使代码更容易编写和维护。

HTH HTH

Maybe something like that can help, you can create statement and connection as instance variable so regular you can check if it is open or not. 也许这样的东西可以帮助你,你可以创建语句和连接作为实例变量,所以你可以检查它是否是开放的。 You should use a multidimensional array if you want to return as an array 如果要作为数组返回,则应使用多维数组

 public ArrayList<ArrayList<String>> getQueryResult(String query){

            ArrayList<ArrayList<String>> feedback = new ArrayList<ArrayList<String>>();
            ArrayList<String> feed = null;

            try {
                ResultSet rs = stm.executeQuery(query);

                ResultSetMetaData rsm = rs.getMetaData();
                    feed = new ArrayList<String>();

                    for(int y = 1;y<rsm.getColumnCount();y++){

                        feed.add(rsm.getColumnName(y));
                    }
                    feedback.add(feed);

                while(rs.next()){
                    feed = new ArrayList<String>();
                for(int i=1;i<=rsm.getColumnCount();i++){

                        feed.add(rs.getString(i));
                }
                feedback.add(feed);
            }



            } catch (SQLException e) {
                //handler
            }
            return feedback;

        }

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

相关问题 如何从SQL到Java到CSV打印列部分? - How can I print column sections from SQL to Java to CSV? 一个SQL查询,用于填充Java中的多个JTextfields - One SQL query for filling several JTextfields in Java 我怎么知道从Java到SQL Server的SQL选择查询没有返回任何结果? - How can I know if a SQL select query from Java to SQL Server didn't return any results? 如何在Java Swing中控制JTextField的宽度? - How can I control the width of JTextFields in Java Swing? 如何将JTextFields保存在Java Swing BoxLayout中? - How do I keep JTextFields in a Java Swing BoxLayout from expanding? 如何从文本文件的每一行中拆分单个列值? - How can I split out individual column values from each line in a text file? 使用Spark Java RDD,如何在每个任务完成时立即处理各个任务结果,而无需等待collect()? - With Spark Java RDDs, how can I process individual task results immediately as each one completes without waiting for collect()? 如何从 Java 中的 sql 查询中获取结果,将其转换为字符串列表,然后打印结果? - How to take results from a sql query in Java, convert it to a list of strings, then print the result? Java:为什么我无法从另一个类的 jTextFields 中获取值? - Java: Why I can't get value from jTextFields from another class? 如何从该对象打印? 爪哇 - How can I print from this object? Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM