?
Solved

data not entering database

Posted on 2003-11-13
4
Medium Priority
?
262 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The Summer 2017 Scholarship Winners have been announced!
If you're a modern-day technology professional, you may be wondering if certifications are really necessary. They are. Here's why.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

800 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