我正在尝试将一个listview附加到另一个listview中的每个元素,并且该listview连接到sql数据库。因此,在listview中选择一个值,然后弹出另一个listview。但我不知道如何把它们和听众联系起来。这是我到目前为止所做的,希望你能理解我的问题。应该是这样的
public static void main(String[] args) {
launch(args);
}
private ArrayList<String> getArtister () {
ArrayList<String> artister = new ArrayList<>();
try (Connection conn = DriverManager.getConnection(
"jdbc:mysql://vhost6.lnu.se:23306/1ik013v20-12?useSSL=false",
"1ik013v20-12", "aubergine")) {
Statement statement = conn.createStatement();
ResultSet result = statement.executeQuery("SELECT Namn FROM artist");
while (result.next()) {
artister.add(result.getString(1));
}
} catch (SQLException ex) {
System.out.println("Something went wrong...");
}
return artister;
}
private ArrayList<String> getLåtar () {
ArrayList<String> låtar = new ArrayList<>();
try (Connection conn = DriverManager.getConnection(
"jdbc:mysql://vhost6.lnu.se:23306/1ik013v20-12?useSSL=false",
"1ik013v20-12", "aubergine")) {
Statement statement = conn.createStatement();
ResultSet result = statement.executeQuery("SELECT Namn FROM album WHERE Artist = 'Bob Dylan'");
while (result.next()) {
låtar.add(result.getString(1));
}
} catch (SQLException ex) {
System.out.println("Something went wrong...");
}
return låtar;
}
private ArrayList<String> getLåtar1 () {
ArrayList<String> låtar = new ArrayList<>();
try (Connection conn = DriverManager.getConnection(
"jdbc:mysql://vhost6.lnu.se:23306/1ik013v20-12?useSSL=false",
"1ik013v20-12", "aubergine")) {
Statement statement = conn.createStatement();
ResultSet result = statement.executeQuery("SELECT Namn FROM album WHERE Artist = 'Bob Marley'");
while (result.next()) {
låtar.add(result.getString(1));
}
} catch (SQLException ex) {
System.out.println("Something went wrong...");
}
return låtar;
}
private ArrayList<String> getLåtar2 () {
ArrayList<String> låtar = new ArrayList<>();
try (Connection conn = DriverManager.getConnection(
"jdbc:mysql://vhost6.lnu.se:23306/1ik013v20-12?useSSL=false",
"1ik013v20-12", "aubergine")) {
Statement statement = conn.createStatement();
ResultSet result = statement.executeQuery("SELECT Namn FROM album WHERE Artist = 'Bob Marley'");
while (result.next()) {
låtar.add(result.getString(1));
}
} catch (SQLException ex) {
System.out.println("Something went wrong...");
}
return låtar;
}
private ArrayList<String> getLåtar3 () {
ArrayList<String> låtar = new ArrayList<>();
try (Connection conn = DriverManager.getConnection(
"jdbc:mysql://vhost6.lnu.se:23306/1ik013v20-12?useSSL=false",
"1ik013v20-12", "aubergine")) {
Statement statement = conn.createStatement();
ResultSet result = statement.executeQuery("SELECT Namn FROM album WHERE Artist = 'Bob Marley'");
while (result.next()) {
låtar.add(result.getString(1));
}
} catch (SQLException ex) {
System.out.println("Something went wrong...");
}
return låtar;
}
@Override
public void start(Stage primaryStage) {
BorderPane pane = new BorderPane();
ListView art = new ListView();
art.getItems().addAll(getArtister());
ListView alb = new ListView();
ListView alb1 = new ListView();
ListView alb2 = new ListView();
ListView alb3 = new ListView();
art.getSelectionModel().selectedIndexProperty().addListener(ov -> {
});
Text text = new Text("MUSIK");
text.setFont(new Font(20));
text.setFill(Color.DARKBLUE);
pane.setLeft(art);
pane.setTop(text);
BorderPane.setAlignment(art, Pos.CENTER_LEFT);
BorderPane.setAlignment(text, Pos.TOP_LEFT);
Scene scene = new Scene(pane, 500, 500);
primaryStage.setTitle("Musiken");
primaryStage.setScene(scene);
primaryStage.show();
}
}
1条答案
按热度按时间zzlelutf1#
好的,试试这个。创建一个新类,如下所示
我刚把它命名为乙烯基。你想叫什么名字都行。
现在回到你的主类,定义一个列表。只调用这些方法来填充列表。
请小心,有两件事要做。这些是你的字段索引。我不知道你数据库的结构,所以我只写了1和2。这些必须是您的艺术家姓名和专辑名称的字段。
最后,您将只有一个集合,涵盖您需要的所有内容。