[英]How to return multiple rows from mysql database with java
I want to select multiple rows and return them in an Arraylist.
My Database structure looks like this:我的数据库结构如下所示:
1 Bestellnummer int(20)
2 BestellerID int(20)
3 ArtikelNummer int(20)
4 Anzahl int(10)
5 Preis double
It doesnt have a unique key, since it will not be changed.它没有唯一的密钥,因为它不会被更改。 I wrote this Method but i get the error:我写了这个方法,但我得到了错误:
" You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'BestellerID,ArtikelNummer,Preis FROM bestellungen WHERE Bestellnummer = 1' at line 1Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 1 out-of-bounds for length 0" " You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'BestellerID,ArtikelNummer,Preis FROM bestellungen WHERE Bestellnummer = 1' at line 1Exception in thread "main" java. lang.IndexOutOfBoundsException: 长度为 0 的索引 1 越界"
public ArrayList<Bestellung> getBestellung (int i) throws SQLException {
ArrayList<Bestellung> Auftrag = new ArrayList<>();
final String SQL ="SELECT* BestellerID,ArtikelNummer,Preis FROM bestellungen WHERE Bestellnummer = ?" ;
ResultSet rs = null ;
try {
PreparedStatement stmt = con.prepareStatement(SQL,ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
stmt.setInt(1, i);
rs = stmt.executeQuery();
System.out.println("test");
while (rs.next()) {
Bestellung test = new Bestellung( i, rs.getInt("BestellerID"), rs.getInt("ArtikelNummer"), rs.getDouble("Preis"));
Auftrag.add(test);
}
}
catch (SQLException e) {
System.err.print(e);
}
finally {
if (rs!=null) {rs.close();
}
}
return Auftrag;
}
}
public class Bestellung {
private int Bestellnummer, BestellerID,ArtikelNummer,Anzahl;
private double Preis;
Here is the Class that Objects will be in the Arraylist:这是 Class 对象将在 Arraylist 中:
Bestellung (int Bestellnummerin,int BestellerIDin,int ArtikelNummerin,double Preisin)
{
this.Bestellnummer = Bestellnummerin ;
this.BestellerID = BestellerIDin ;
this.ArtikelNummer = ArtikelNummerin;
this.Anzahl = 1;
this.Preis = Preisin;
}}
You can try this:你可以试试这个:
final String SQL ="SELECT BestellerID,ArtikelNummer,Preis
FROM bestellungen WHERE Bestellnummer = ?";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.