aspose file tools*
The moose likes JDBC and the fly likes complex JDBC display problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "complex JDBC display problem" Watch "complex JDBC display problem" New topic
Author

complex JDBC display problem

Kareem Saheed
Greenhorn

Joined: Mar 09, 2007
Posts: 1
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

Joined: Apr 14, 2004
Posts: 10336

Is there any exception message?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

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?


Saifuddin..
[Blog][Linkedin] How To Ask Questions On JavaRanch My OpenSource
Herman Schelti
Ranch Hand

Joined: Jul 17, 2006
Posts: 387
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: complex JDBC display problem