Solved

data not entering database

Posted on 2003-11-13
4
242 Views
Last Modified: 2010-04-01
...Hi, this is a part of the database code, surprisingly data is not being entered into database.if i would take out the if else statement, then the data gets inserted,


String connectionURL = "jdbc:mysql://localhost:3306/mydatabase?user=;password=";
Connection connection = null;
Statement statement = null;
PreparedStatement prep1,prep2,prep3;
prep1=null;
prep2=null;
Statement stmt=null;
ResultSet rs = null;
String username="";
   
   String passwd="";
   String verifypwd="";

    String name="";
    String edu="";
    String city="";
    String state="";
    String postcode="";
    String qualify1="";
    String house1="";
    String office1="";
    String mobile1="";
    String email="";
    String notify="";
    String add1="";
    String add2="";
    String timestamp="";
    String skill="";
    String choose="";






try
{

   
   Class.forName("com.mysql.jdbc.Driver").newInstance();
   connection = DriverManager.getConnection(connectionURL, "", "");

 
   
    username=vol.getUsername();
   
    passwd=vol.getPasswd();
    verifypwd=vol.getVerifypwd();

     name=vol.getName();
     edu=vol.getEdu();
     city=vol.getCity();
     state=vol.getState();
     postcode=vol.getPostcode();
     qualify1=vol.getQualify();
     house1=vol.getHouse1();
     office1=vol.getOffice1();
     mobile1=vol.getMobile1();
     email=vol.getEmail();
     notify=vol.getNotify();
     add1=vol.getAdd1();
     add2=vol.getAdd2();
     timestamp=vol.getTimestamp();
     skill=vol.getSkill();
     choose=vol.getChoose();



      statement=connection.createStatement();
 stmt=connection.createStatement();
       String myquery="select * from account where username='"+vol.getUsername()+"'";
       rs=statement.executeQuery(myquery);

       boolean anyRecords=rs.next();

 

   if(rs.getString("username").equals(vol.getUsername()))
           {
              System.out.println("KK");
%>
              <jsp:forward page="register.jsp" />

<%
           }



 
       else  {  

System.out.println("LL");



String myaccount="insert into account(username,password,verifypassword) values(?,?,?)";
   prep1=connection.prepareStatement(myaccount);
   
   prep1.setString(1,username);
   prep1.setString(2,passwd);
   prep1.setString(3,verifypwd);

   prep1.executeUpdate();
   prep1.close();


 
 
     String myed="insert into volunteer(username,name,education,city,state,postcode,qualify,house1,office1,mobile1,email,notify,add1,add2,skill,day)values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

      prep2=connection.prepareStatement(myed);
      prep2.setString(1,username);
      prep2.setString(2,name);
      prep2.setString(3,edu);
      prep2.setString(4,city);
      prep2.setString(5,state);
      prep2.setString(6,postcode);
      prep2.setString(7,qualify1);
      prep2.setString(8,house1);
      prep2.setString(9,office1);
      prep2.setString(10,mobile1);
      prep2.setString(11,email);
      prep2.setString(12,notify);
      prep2.setString(13,add1);
      prep2.setString(14,add2);
      prep2.setString(15,skill);
      prep2.setString(16,timestamp);

     prep2.executeUpdate();
      prep2.close();



 
   

 
 

//  } //else

       

   }//try
   catch( SQLException ex )
   {
       ex.printStackTrace() ;
    }
    catch( ClassNotFoundException ex )
   {
     ex.printStackTrace() ;
   }

0
Comment
Question by:Jasbir21
  • 2
4 Comments
 

Author Comment

by:Jasbir21
ID: 9738811
it seems like the first if works fine but not the else statement, i guess i nneed to put
the not equal sign in java,

i mean if it is equal , it is like

if(rs.getString("username").equals(vol.getUsername()))

can it be because i need to put not equal, if yes, how is it,
0
 
LVL 4

Accepted Solution

by:
vk33 earned 50 total points
ID: 9738858
Hi!

Your else block will never run! You select records with username=<value> and then compare the username with <value> in if statement. Of course they will be equal!

If you want to find out if the requested record is present in the database do something like this:

      String myquery="select * from account where username='"+vol.getUsername()+"'";
       rs=statement.executeQuery(myquery);
       if (rs.next()) {

       } else {
     
       }

In this case your else block will work if the user is not found in the database.

Regards!
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9738922
For information:

The opposite of:

    if (myString.equals("Some text"))

is:

    if (!myString.equals("Some text"))

Notice the ! before myString ;-)
0
 

Author Comment

by:Jasbir21
ID: 9739006
thanks, it works,
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

In this article, you will read about the trends across the human resources departments for the upcoming year. Some of them include improving employee experience, adopting new technologies, using HR software to its full extent, and integrating artifi…
What is Backup? Backup software creates one or more copies of the data on your digital devices in case your original data is lost or damaged. Different backup solutions protect different kinds of data and different combinations of devices. For e…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now