java - How can I pass string to a JOptionPane? -


i have tried write code searches database of students , shows searched result on option pane. , ended writing following. expected result is:

name: "something" roll: "something" registration: "something"

but output this:

name: null roll: null registration: null


public class search 

{

jframe sw = new jframe("search students' info");   //search window jtable stable; jlabel stfl = new jlabel("roll");                  //search text field label jtextfield stf = new jtextfield(8);               //search text field   jbutton sb = new jbutton("search");             //search button  public void exsearch()                              //execute search {     sw.setlayout(new gridlayout(2,2));     sw.add(stfl);     sw.add(stf);     sw.add(sb);       sw.setsize(200, 100);     sw.setlocation(100, 100);     sw.setvisible(true);      sb.addactionlistener(new actionlistener()     {         public void actionperformed(actionevent e)         {             string driver = "com.mysql.jdbc.driver";             string url = "jdbc:mysql://localhost/srsdb";             try             {                 class.forname(driver).newinstance();                  java.sql.connection con = drivermanager.getconnection(url, "root", "");                 joptionpane.showmessagedialog(null, "connected", "connection confirmation", joptionpane.plain_message);                  string str ="select* students roll="+stf.gettext();                 java.sql.statement st = con.createstatement();                 java.sql.resultset rs = st.executequery(str);                  rs.first();                  int n = rs.getmetadata().getcolumncount();             //  string[] columnnnames;                 string[] attributes= new string[10];                  int j;                  while(rs.next())                 {                     for(j=0; j<3; j++)                     {                         attributes[j] = rs.getstring(j);                     }                 }                     joptionpane.showmessagedialog(null, "name :"+attributes[0]+"\nroll :"+attributes[1]+"\nregistration :"+attributes[2], "search result", joptionpane.plain_message);             }         catch(exception f)         {             f.printstacktrace();             joptionpane.showmessagedialog(null, "not found", "search result", joptionpane.plain_message);         }         }});   }  public static void main (string[] args) {     new search(); } 

}

i use debugger check part of code omitted. 1 wierd thing see is, jump first record in result set:

rs.first(); 

and read data subsequent records in while loop

 while(rs.next())  {     for(j=0; j<3; j++)     {         attributes[j] = rs.getstring(j);     }  } 

this ensures, data last matching record if there more one. better check if there 0 or more 1 record. if case, issue warning because there wrong code (use debugger find out what). if there 1 record, read data record , print (more or less try rs.getstring())


Comments