Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

complex JDBC display problem

 
Kareem Saheed
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pls I'm retrieving data from two tables in Mysql
and displaying them on a swing interface
here is the class that handles the search based on parameters you enter into textfields
when a button is clicked this is the event handler the problem is I don't get to see anything

class SearchHandler implements ActionListener
{
private Connection connection;
private Statement statement;
private ResultSet resultSet;
JTextField tsurname,tothernames, tDofB,thfid,tadd,tPNumber;
JComboBox sex,tlang,ttribe,EDstatus,tSofO,service,Rank,Ostatus,mstatus,csr;
JLabel lservice,lsex,lMstat,lEstat,lrank,lOstat,lsr,tribe,lang,SofO,add,PNumber,hfid;
JButton submit;
String sname,oname,yb,sx,sico,langu,tri,educ,stae,srev,srnk,sogns,smrls,souefer,sads,stelep;

public SearchHandler(JTextField tsurname,JTextField tothernames,JTextField tDofB,JComboBox sex,JTextField thfid,JComboBox tlang,JComboBox ttribe,JComboBox EDstatus,JComboBox tSofO,JComboBox service,JComboBox Rank,JComboBox Ostatus,JComboBox mstatus,JTextField tadd,JComboBox csr,JTextField tPNumber,JLabel lservice,JLabel lsex,JLabel lMstat,JLabel lEstat,JLabel lrank,JLabel lOstat,JLabel lsr,JLabel tribe,JLabel lang,JLabel SofO,JLabel add,JLabel PNumber,JLabel hfid,JButton submit)
{
this.tsurname=tsurname;
this.tothernames=tothernames;
this.tDofB=tDofB;
this.sex=sex;
this.thfid=thfid;
this.tlang=tlang;
this.ttribe=ttribe;
this.EDstatus=EDstatus;
this.tSofO=tSofO;
this.service=service;
this.Rank=Rank;
this.Ostatus=Ostatus;
this.mstatus=mstatus;
this.csr=csr;
this.tadd=tadd;
this.tPNumber=tPNumber;
this.lservice=lservice;
this.lsex=lsex;
this.lMstat=lMstat;
this.lEstat=lEstat;
this.lrank=lrank;
this.lOstat=lOstat;
this.lsr=lsr;
this.tribe=tribe;
this.lang=lang;
this.SofO=SofO;
this.add=add;
this.tadd=tadd;
this.PNumber=PNumber;
this.hfid=hfid;
this.submit=submit;


}


public void actionPerformed(ActionEvent ae)
{
if(tsurname.getText().length()==0||tothernames.getText().length()==0||tDofB.getText().length()==0)
{
JOptionPane.showMessageDialog(null,"Please Enter Missing Details","Do Check",JOptionPane.ERROR_MESSAGE);

}
else
{


int response = JOptionPane.showConfirmDialog(null,"Are you sure","Confirm",JOptionPane.YES_NO_OPTION);



if(response==0)
{

try
{

String sname=tsurname.getText();
String oname=tothernames.getText();
//String sx=(String)sex.getSelectedItem();
String yb=tDofB.getText();

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection = DriverManager.getConnection("Jdbc:Odbc:MyDataSource");
statement= connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
String Mysql = "select personID,sex,SiteCode,PreferredLanguage,Tribe,EducationLevel,StateOfOrigin,Service,rank,OccupationStatus,MaritalStatus,ResidenceAddress,SourceOfReferral,PhoneNumber from demographic,person where surname='sname' and othernames='oname' and yearbirth='yb'and person.PersonID=demographic.demographicID ";

//Mysql = Mysql + " (from person,demographic where surname='sname' and othernames='oname' and yearbirth='yb'and person.PersonID=demographic.demographicID)";



//conn.setAutoCommit(false);
resultSet=statement.executeQuery(Mysql);
//resultSet.next();
if(resultSet.next())
{

sex.setSelectedItem((Object)resultSet.getString("Sex"));

thfid.setText(resultSet.getString("SiteCode"));

tlang.setSelectedItem((Object)resultSet.getString("PreferredLanguage"));

ttribe.setSelectedItem((Object)resultSet.getString("Tribe"));

EDstatus.setSelectedItem((Object)resultSet.getString("EducationLevel"));

tSofO.setSelectedItem((Object)resultSet.getString("StateOfOrigin"));

service.setSelectedItem((Object)resultSet.getString("Service"));

Rank.setSelectedItem((Object)resultSet.getString("rank"));

Ostatus.setSelectedItem((Object)resultSet.getString("OccupationStatus"));

mstatus.setSelectedItem((Object)resultSet.getString("MaritalStatus"));

tadd.setText(resultSet.getString("ResidenceAddress"));

csr.setSelectedItem((Object)resultSet.getString("SourceOfReferral"));

tPNumber.setText(resultSet.getString("PhoneNumber"));
}

connection.close();
//Ps.close();

lang.setVisible(true);
tlang.setVisible(true);
lsex.setVisible(true);
sex.setVisible(true);

tribe.setVisible(true);
ttribe.setVisible(true);
lEstat.setVisible(true);
EDstatus.setVisible(true);
SofO.setVisible(true);
tSofO.setVisible(true);
lservice.setVisible(true);
service.setVisible(true);
lrank.setVisible(true);
Rank.setVisible(true);
lOstat.setVisible(true);
Ostatus.setVisible(true);
lMstat.setVisible(true);
mstatus.setVisible(true);
add.setVisible(true);
tadd.setVisible(true);
lsr.setVisible(true);
csr.setVisible(true);
hfid.setVisible(true);
thfid.setVisible(true);
PNumber.setVisible(true);
tPNumber.setVisible(true);
submit.setVisible(true);
}

catch(SQLException sq)
{
JOptionPane.showMessageDialog(null,sq.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
}

catch(ClassNotFoundException E)
{
JOptionPane.showMessageDialog(null,E.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
}
}
}
}
}
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any exception message?
 
Muhammad Saifuddin
Ranch Hand
Posts: 1324
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
might be problem inside your actionPerformed method. I like to suggest you to check and run your database connection and ResultSet code in simple java class to findout where the problem is?
 
Herman Schelti
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Kareem,

I think the problem is in your SQL, you are looking for persons who's surname is 'surname', and you are probably looking for people who's surname is equal to the value of the String-variable surname?

Try to change

String Mysql = "select personID,sex,SiteCode,PreferredLanguage from demographic,person where surname='sname' and othernames='oname' and yearbirth='yb'and person.PersonID=demographic.demographicID ";

into:

String Mysql = "select personID,sex,SiteCode,PreferredLanguage from demographic,person where surname='" + sname + "' and othernames='" + oname + "' and yearbirth='" yb + "'and person.PersonID=demographic.demographicID ";

If you think this code looks ugly (and I hope you do): use a Preparedstatement instead of Statement.

And close your connection, (prepared)statement and resultset in a finally block: so they will always get closed, even when an Exception occurs.

And like Saif said: it's best to test your sql-code in a simple program, and maybe you can call this program from your Swing app.

Goodluck!
Herman
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic