Professional Documents
Culture Documents
retreivingdatafrommysqlselectstatementinjavaStackOverflow
signup
login
tour
help
xDismiss
JointheStackOverflowCommunity
StackOverflowisacommunityof6.3million
programmers,justlikeyou,helpingeachother.
Jointhemitonlytakesaminute:
Signup
retreivingdatafrommysqlselectstatementinjava
Havingtroublewiththis..I'msureI'mmissingsomethingsimplebutIsimplywanttheresultofaselectstatementinajavamysqlselectstatement
butIkeepgetting:
com .mysql .jdbc .JDBC4ResultSet@1c190c99
Here'smycode:
PreparedStatement Findstatement ;
Findstatement =con .prepareStatement ("SELECTCodefromDataMaster
whereDataName=(?)" );
Findstatement .setString (1,Name );//Ihaveavariableinmyfile
named'Name'
ResultSet CodeAll =Findstatement .executeQuery ();
System .out .println (CodeAll );
I'vetriedintfundCode=fundCodeAll.getInt(1)attheendofthestatementbutstillnoluck.
HowdoIgettheintvalueofthetheresultfromthe
selectstatement?
java mysql jdbc
editedApr26'11at19:44
BalusC
688k
2711
asked Apr26'11at19:37
Lostsoul
212
2496
7,259
18
67
122
Itsprobablyabetterideatousejava.sql.ResultSetunlessthemysqlresultsetgivesyouamazing
performanceimprovements.KalApr26'11at19:48
3Answers
Iftherearezeroormanyresults,thenyoucancollecttheminaList .
List <Integer >codes =new ArrayList <Integer >();
//...
resultSet =statement .executeQuery ();
while (resultSet .next ()) {
codes .add (resultSet .getInt ("Code" ));
}
//...
Orifthereiszerooroneresult,thenreplacewhile by if .
http://stackoverflow
.com/questions/5795507/retreivingdatafrommysqlselectstatementinjava
1/2
3/11/2016
retreivingdatafrommysqlselectstatementinjavaStackOverflow
Seealso:
JDBCtutorial
Unrelated totheconcreteproblem,pleasepayattentiontothe
Javanamingconventions.
answered Apr26'11at19:40
BalusC
688k
2711
212
2496
ohhhIsawthatbutdidn'tthinkIhadtodoitiftherewasonlyoneresult. Isawthisinexamplesbutitwasall
BalusCApr26'11at19:42
Iunderstoodthisformultipleresultsbutyesinthisexampleitsalwaysgoingtobeoneresult. IfIhave
multipleidcodesforthesameproductsthenthisresultsetisgoingtobetheleastofmyproblems:)
Lostsoul Apr26'11at19:45
SoundslikeyourkeyproblemwasthatyoumissedthatyouneedtosayresultSet.next()togetthenext
record.Ifthere'sonlyonerecord,youstillneedtocallnext()oncetogetthefirstrecord. JayApr26'11at
19:51
Yes,theinitialcursorpositionis beforetherow. BalusC Apr26'11at19:53
Try:
while (CodeAll .next ())
{
int i =CodeAll .getInt (0);
System .out .println ("Code:" +i);
}
answered Apr26'11at19:41
jolo
331
Aftergettingtheresultset,youshoulditerateoverit:
while (CodeAll .next ()){
String value =CodeAll .getString ("columnname" );
//getallvaluesdependingontype,likegetStringforstring,
//getDoublefordouble,etc
}
Justgetvaluesfromresultsetanddowhateveryouwantfromit.
editedMar15at18:45
answered Apr26'11at19:47
FastSnail
7,864
kinshuk4
18
47
1,478
15
29
http://stackoverflow
.com/questions/5795507/retreivingdatafrommysqlselectstatementinjava
2/2