java - Setting database records into jtextfield -
the problem of code can't set data database onto jtextfield. every time run code, says stackoverflowerror. how fix error?
here's code
public class dataconnect extends databasegui { private connection datacon; private statement datastmt; private resultset datars; private string name, address; int age; public dataconnect(){ try{ class.forname("com.mysql.jdbc.driver"); datacon = drivermanager.getconnection("jdbc:mysql://localhost:3306/studentrec","root",""); datastmt = datacon.createstatement(); datars = datastmt.executequery("select * studentrecords"); }catch(exception exc){ system.out.println(exc.getmessage()); } } public void setdata(){ try{ datars.next(); name = datars.getstring("name"); age = datars.getint("age"); address = datars.getstring("address"); databasegui dbgui = new databasegui (); dbgui.jtfdata[0].settext(name); dbgui.jtfdata[1].settext(integer.tostring(age)); dbgui.jtfdata[2].settext(address); //system.out.println(name+" "+age+" "+address); }catch(exception exc){ } } }
a possible cause of problem here:
public class dataconnect extends databasegui { //... public dataconnect(){ //... } public void setdata(){ try{ databasegui dbgui = new databasegui (); //*****
you're creating new databasegui object within class extends databasegui , risk infinite recursion. solution: don't want dataconnect extend databasegui, or don't want create new databasegui inside of dataconnect, or both. if have displayed databasegui, don't want create non-displayed one, instead want set data of components of gui displayed, not new 1 create on spot.
also, have wonder -- create dataconnect object within databasegui class? question can improved doing bit of debugging find out code in particular causes exception occur.
also empty catch block
catch(exception exc){ }
is code should never happen. @ least print stacktrace.
Comments
Post a Comment