[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

data not entering database

Posted on 2003-11-13
4
Medium Priority
?
269 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Working from home is a dream for many people who aren’t happy about getting up early, going to the office, and spending long hours at work. There are lots of benefits of remote work for employees.
Are you looking for the options available for exporting EDB files to PST? You may be confused as they are different in different Exchange versions. Here, I will discuss some options available.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

649 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