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

发布于 2024-12-19 01:15:33 字数 1279 浏览 1 评论 0原文

我编写了一个 Java 程序,该程序能够将客户添加到数据库中,然后通过用户名或 customer_id 查找它们。

我可以毫无问题地将用户添加到数据库中。此外,我还可以通过用户名或 customer_id 选择用户,并将用户信息打印到 JTextArea 中以供查看。然而,我想做的是将数据库行中每个特定列的用户信息打印到表单上相应的 JTextField 中。我认为我需要使用数组来做到这一点,但到目前为止我还没有成功。

此时我选择数据的代码如下:

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

现在我需要更改该代码以将列结果放入数组中,然后我应该能够简单地从数组中提取数据,对吗?我可能会完全发疯。我不知道。 D:

如有任何建议,我们将不胜感激。

谢谢!

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.

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. 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. 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:

Any advice would be appreciated.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我很OK 2024-12-26 01:15:33

也许类似的东西可以帮助您,您可以创建语句和连接作为实例变量,这样您就可以定期检查它是否打开。如果你想以数组形式返回,你应该使用多维数组

 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;

        }

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;

        }
浮生未歇 2024-12-26 01:15:33

当然,您可以使用 getString(和其他 getXXX 方法)从 ResultSet 获取值,然后使用 setText 将值放入文本字​​段中。

本教程可能会有所帮助: http://docs.oracle.com/javase/教程/jdbc/basics/retreering.html

PS我忘了说,如果您愿意,您可以通过列名称而不是列索引获取值 - 这稍微慢一些,但使代码更易于编写和维护。

华泰

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.

This tutorial may help: 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.

HTH

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文