1

i create a MariaDB based shopping list. I get a full running java console in/output program. Now i am trying to build a GUI. This class "WindowList" is my constructor and is implemented in main.java with "WindowList showlist = new WindowList();" If i query data from MariaDB to print it in JFrame via JLabel or JTextArea i only get the last point of my list.

I am new i java and programming at all. Thanks in advance.

package shoppinglist;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.*;

public class WindowList {


//Driver für MariaDB
static final String JDBC_DRIVER = "org.mariadb.jdbc.Driver";
//url samt port von der MariaDB
static final String DB_URL = "xxx";
//Datenbankname
static final String DB_DB = "xxx";

//  Database credentials
static final String USER = "xxx";
static final String PASS = "xxx";

public WindowList() {

Connection conn = null;
Statement stmt = null;

JFrame listFrame = new JFrame();
listFrame.setSize(200,200);
listFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//JLabel derText = new JLabel();

JTextArea textfeld = new JTextArea(40, 40);

listFrame.getContentPane().add(textfeld);
listFrame.setVisible(true);


try {
    //Verbindung zu MariaDB funktioniert            
    conn = DriverManager.getConnection(DB_URL+DB_DB, USER, PASS);
    stmt = conn.createStatement();
    //Query muss hier genau so hinterlegt sein wie in der DB selbst
    String sql = "Select * from shoplist";
    ResultSet rs = stmt.executeQuery(sql);

    while (rs.next()) {

    textfeld.setText("- " + rs.getString("Item"));

    }
} catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
}
}
JayKay
  • 11
  • 3
  • Have you seen the _Stack Overflow_ question: [Add a new line to the end of a JtextArea](https://stackoverflow.com/questions/2088016/add-a-new-line-to-the-end-of-a-jtextarea)? – Abra Jun 12 '19 at 19:16
  • Thank you. "textfeld.append("rs.getString("Item"));" print all the data from my mariaDB out. Now i have to bring it in "shape" as a bullitpoint list. – JayKay Jun 12 '19 at 20:11
  • I don't think you can display a bulleted list in `JTextArea`. I suggest you look into using `JEditorPane` or `JTextPane` instead. – Abra Jun 13 '19 at 04:32
  • @Abra Thank you. You inspired me to search a way to get this done. I used JList. – JayKay Jun 13 '19 at 19:08

0 Answers0