简体   繁体   English

Java - 在JTextArea中显示来自数据库的信息

[英]Java - show information from database in JTextArea

I am working with MySQLWorkbench and Eclipse. 我正在使用MySQLWorkbench和Eclipse。 In MySQLWorkbench I have a database, where I store my informations. 在MySQLWorkbench中我有一个数据库,我存储我的信息。 Now I want to click on a button in my UI and want to get the informations in my JTextArea in Java. 现在我想点击我的UI中的一个按钮,并希望在Java中获取我的JTextArea中的信息。 I tryed a lot but noting works. 我尝试了很多,但注意到了作品。 And the problem is that I don't know how I can access to the actionListener in the main class.. 问题是我不知道如何在主类中访问actionListener。

When I execute my code I can see it in the console but I want to show it in the TextArea.. 当我执行我的代码时,我可以在控制台中看到它,但我想在TextArea中显示它。

That's the code I still have : 那是我仍然拥有的代码:

public static void main (String[] args) 

{

    //String x = txtfldVorname.getText();
     try {
           Class.forName("com.mysql.jdbc.Driver");
           Connection con = DriverManager.getConnection ("jdbc:mysql://localhost:3306/dblabor","root",""); 
           System.out.println("Connection successful");

                String sql = "SELECT name,nachname FROM kunden";
                ps= (PreparedStatement) con.prepareStatement(sql);

                     rs = ps.executeQuery();

                     if(rs.next()){
                           String name = rs.getString("name");
                           String nachname = rs.getString("nachname");

                         System.out.print(" Nachname: " + name);
                         System.out.print(" Nachname: " + nachname);
                        }


                      rs.close();     

           /*System.out.println("Select kunden");
           st = con.createStatement();

           String sql = "SELECT * FROM kunden";
           rs= st.executeQuery(sql);

           String sql1 = "INSERT INTO kunden (name,nachname)"+ "VALUES(?,?)";
           PreparedStatement ps = (PreparedStatement) con.prepareStatement(sql1);


           ps.execute();
           /*while(rs.next()) {

              String vorname= rs.getString("name");
              String nachname = rs.getString("nachname");


          System.out.format("%s,%s",vorname,nachname);
          }

          */
     } catch (Exception e) {
           System.err.println(e);
     }
}
 ActionListener al = new ActionListener() {

        public void actionPerformed(ActionEvent e) {
             buttonProf.addActionListener(al);


                try {
                    String sql = "SELECT name,nachname FROM kunden";
                    ps= (PreparedStatement) con.prepareStatement(sql);
                     rs = ps.executeQuery();

                     if(rs.next()){
                           String name = rs.getString("name");
                           String nachname = rs.getString("nachname");
                           textfeld.getText();

                         System.out.print("Name: " + name);
                         System.out.print(" Nachname: " + nachname);
                } 
                }//try
                catch (SQLException e1) {

                    e1.printStackTrace();
                }//catch

    } 

.. Thanks all in advance. ..提前谢谢。

Use the setText() method of the JTextArea to set the contents of the Text Area. 使用JTextArea的setText()方法设置文本区域的内容。 You can checkout more about it from the docs 您可以从文档中查看更多相关信息

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

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