+ 1

How do i avoid it?

I am coding in java related to jdbc. However I get "null" when I want to display data after picking it up from my database. I took care to check that my query was correct and that the data did indeed exist in my MySQL database. Here a part of my code to do it: Statement state=con.createStatement(); String recu="Select name from table where id=1"; ResultSet rs=state.executeQuery(recu); While(rs.next()){ String h=rs.getString(0); } System.out.println(h);

18th Mar 2022, 3:23 PM
Romaric
7 Answers
+ 3
By getting "name" is not a good idea because somehow if you drop column name then you have to change in code as well which is not a good idea. We should avoid to right direct query in code. Stored Procedure is a better option.
18th Mar 2022, 3:46 PM
AÍąJ
AÍąJ - avatar
+ 2
Romaric Your code is right but I think there should be rs.getString(1);
18th Mar 2022, 3:36 PM
AÍąJ
AÍąJ - avatar
+ 1
Jayakrishna🇼🇳 Thanks , i take note
19th Mar 2022, 11:30 PM
Romaric
0
Statement state=con.createStatement(); String recu="Select name from table where id=1"; ResultSet rs=state.executeQuery(recu); while(rs.next()){ String h=rs.getString(1); System.out.println(h); } //Romaric column index starts from 1
18th Mar 2022, 3:41 PM
Jayakrishna 🇼🇳
0
FF9900 okay thanks foy ur contribution
19th Mar 2022, 11:31 PM
Romaric