p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

java_databases thread: Query problem


Message #1 by "saeed Akhter" <saeedakhter15@h...> on Sun, 24 Nov 2002 08:05:28 +0000
Sir,
i am having problem in query in resultset data. Actually i have three
tables name

Patient Table

pid - firstname - lastname - gender - phone - etc...

in patient table the pid filed is autonumber and rest are text


Phistory Table

hid - complain1 - complain2 - complain3 - pid

in phistory table the hid filed are the auto increment number while pid 
field is number type  are the forign key whihc relates to the pid field in 
patient table .


relation ship b/t table are as follow

patient table field pid---- (1 to many )--- phistory table field pid


i have the userinterface where user fill the information in 
firstname,lastname,gender,phone,complain1,complain2,complain3
fields. and then press the add button which will add the record to
teh related tables fields.

i am getting problem in query expressiion that mismatch data type.
sir please correct my following code

public void actionPerformed(ActionEvent e)

try {

  st = con.createStatement();

if(!this.fnametxt.getText().equals( "" ) && !this.lnametxt.getText().equals( 
"" ))
{
String quries = "INSERT INTO patient (" +
"firstname,lastname,age,gender,phone,email,address"  +
                  ") VALUES ('" +
                fnametxt.getText() + "', '" +
                  lnametxt.getText() + "', '" +
                  agetxt.getText() + "', '" +

               a.getSelectedItem() + "', '" +
                  phonetxt.getText() +  "', '" +
                  addresstxt.getText() + "', '" +
                  emailtxt.getText() + "')";



                              int result = st.executeUpdate( quries );

                  if ( result == 1 ){
// read just inserted record to obtain id field
		      // needed to add the phistory table data

   try {

	   	     String query = "SELECT pid FROM patient" +
         "WHERE patient.pid=phistory.sid";

	  	         ResultSet rs = st.executeQuery( query );
	   	          rs.next();

	  	         pidtxt.setText(String.valueOf(rs.getInt("pid")));

}
  catch ( SQLException sqlex ) {

      }


               fnametxt.setText( "" );
                lnametxt.setText( "" );
               agetxt.setText( "" );
             phonetxt.setText( "" );
              addresstxt.setText( "" );
              emailtxt.setText( "" );


            }
         }
else


         st.close();
      }
      catch(SQLException sqlex)
      {
      sqlex.printStackTrace();
      }





// end of 1st query


// start of 2nd query
try {

  stt = con.createStatement();

if(!this.comp1txt.getText().equals( "" ) && !this.comp2txt.getText().equals( 
"" ))
{



String quriess = "INSERT INTO phistory (" +
"pid,complain1,complain2  "  +
                  ") VALUES ('" +
                 pidtxt.getText() + "', '" +

                  comp1txt.getText() + "', '" +


                  comp2txt.getText() + "')";


                  int resultt = stt.executeUpdate( quriess );

                  if ( resultt == 1 ){

              comp1txt.setText( "" );
               comp2txt.setText( "" );

            }
         }
else


         stt.close();
      }
      catch(SQLException sqlex)
      {
      sqlex.printStackTrace();
      }

sir please it urgent i will be very great full to you.

Thanks



_________________________________________________________________
Tired of spam? Get advanced junk mail protection with MSN 8. 
http://join.msn.com/?page=features/junkmail


  Return to Index