Wrong number of database records is output by a Java program
I have a table in mySQL and has 3 line which has name ,address,Telephone,Age,Charge. in two of them the names are "Adam" and the last row is "Abas" ,i don't know that why it prints in the console like this,please help me thanks!
Statement s;
int s4,s5;String s1,s2,s3;
List<InfoClass> clientList = null;
try {
s = conn.createStatement();
ResultSet rs = s.executeQuery("SELECT * FROM customer_info WHERE Name LIKE 'A%'");
InfoClass list = null;
while (rs.next()) {
s1 = rs.getString(2);
if (rs.wasNull()) {
s1 = null;
}
s2 = rs.getString(3);
if (rs.wasNull()) {
s2 = null;
}
s3 = rs.getString(4);
if (rs.wasNull()) {
s3 = null;
}
s4 = rs.getInt(5);
if (rs.wasNull()) {
s4 = 0;
}
s5 = rs.getInt(6);
if (rs.wasNull()) {
s5 = 0;
开发者_StackOverflow中文版 }
list = new InfoClass(s1, s2);
if (clientList == null) {
clientList = new ArrayList<InfoClass>();
}
clientList.add(list);
}
for(int i=0;i<clientList.size();i++){
InfoClass c =clientList.get(i);
System.out.println(c.getName());
}
in the console:
run:
Adam Smit
Adam Smit
Abas
Adam Smit
Adam Smit
Adam Smit
Adam Smit
I want the output be like this:
run:
Adam Smit
Adam Smit
Abas
You seem to have a customer named 'Abas' in your database, which is a valid match for your query.
It is better to use the rs.getString( String columnLabel ) method.
Could you please change the println
row into this:
System.out.println("" + i + ": " + c.getName());
and run it?
精彩评论