Een resultatenset opslaan in een array in java
Ik wil het resultaat van een hele Mysql-tabel in een array opslaan
String sDriver = "com.mysql.jdbc.Driver"; String sURL = "jdbc:mysql://www.odorxd.xyz:3306/u218933149_odor_base"; try { Class.forName(sDriver).newInstance(); Connection con = DriverManager.getConnection(sURL, "u218933149_estenoesodor", "Tsunayoshi27?"); PreparedStatement stmt = null; ResultSet rs = null; stmt = con.prepareStatement("SELECT * FROM justname"); rs = stmt.executeQuery(); while (rs.next()) { for (int x = 1; x <= rs.getMetaData().getColumnCount(); x++) { System.out.print(rs.getString(x) + "t"); } System.out.println(""); } } catch (SQLException sqle) { sqle.printStackTrace(); } catch (Exception e) { e.printStackTrace(); }
het geeft mij dit terug vanuit de database
run:brandonBrandonJulio
Daniël
SUCCESVOL BOUWEN (totale tijd:1 seconde)
Ik wil wat zich in de database bevindt in een array opslaan om het te kunnen implementeren met een sorteer- en zoekmethode
String str[] = { "Ajeet", "Steve", "Rick", "Becky", "Mohan", "Brandon", "Brandon Jesus", "Brandon Flores", "Brandon Flores Flores"}; String temp; System.out.println("Strings in sorted order:"); for (int j = 0; j < str.length; j++) { for (int i = j + 1; i < str.length; i++) { // comparing adjacent strings if (str[i].compareTo(str[j]) < 0) { temp = str[j]; str[j] = str[i]; str[i] = temp; } } System.out.println(str[j]); }
daarom moet ik het in een array opslaan
Ik zou elke kritiek of hulp op prijs stellen omdat je geen idee hebt, dank je
Antwoord
U kunt een List
. gebruiken .
List<String> list = new ArrayList<>(); while (rs.next()) { for (int x = 1; x <= rs.getMetaData().getColumnCount(); x++) { String str = rs.getString(x) list.add(str); System.out.print(str + "t"); } System.out.println(); } String[] arr = list.toArray(new String[0]); String temp; // ... sort