Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why i get the incorrect data from mySQL????

 
leo tien
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want get data from mySQL.
mySQL's table as following:
+---------+--------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------------------+------+-----+---------+-------+
| article | int(4) unsigned zerofill | | PRI | 0000 | |
| dealer | char(20) | | PRI | | |
| price | double(16,2) | | | 0.00 | |
+---------+--------------------------+------+-----+---------+-------+
my table is :
+---------+--------+-------+
| article | dealer | price |
+---------+--------+-------+
| 0001 | A | 3.45 |
| 0001 | B | 3.99 |
| 0002 | A | 10.99 |
| 0003 | B | 1.45 |
| 0003 | C | 1.69 |
| 0003 | D | 1.25 |
| 0004 | D | 19.95 |
+---------+--------+-------+
then I get the data from it ,
......
ResultSet result;
ResultSetMetaData rs;
......
while(result.next()) {
System.out.println(result.getString(
rs.getColumnName(1)));
/*this sentence can not run.
System.out.println(result.getInt(
rs.getColumnName(1)));
*/
System.out.println(result.getString(
rs.getColumnName(2)));
System.out.println(result.getDouble(
rs.getColumnName(3)));
}
......
it display:
......
Article : pppq
Dealer :
price : 3.45
......
as you see, the problem take place: the article's data isn't "pppq", why occur this error? How can I resolve it??please help ,,,thanks....
 
John Dunn
slicker
Ranch Hand
Posts: 1108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
article field is an int(4) and you access with getString(rs.getColumnName(1).............
shouldn't it be getInt(rs.getColumnName(1))
or something like that?
 
Lu Battist
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
For debugging make it as simple as possible.
Try the following (forget the metadata for now):
......
ResultSet rs;
......
while(rs.next()) {
System.out.println(rs.getString(1));
System.out.println(rs.getString(2));
System.out.println(rs.getString(3));
}
......
Does it print okay now? If yes, the problem probably lies with the metadata usage. If still a problem, try the query directly from mysql.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic