Solved

data not entering database

Posted on 2003-11-13
4
247 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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
tomcat users xml 7 120
reflection example 3 101
instanceof  operator in java 26 91
how to hide hide popup page refresh second time? 10 125
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
Data breaches are on the rise, and companies are preparing by boosting their cybersecurity budgets. According to the Cybersecurity Market Report (http://www.cybersecurityventures.com/cybersecurity-market-report), worldwide spending on cybersecurity …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

773 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