Solved

data not entering database

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maven project import to eclipse problems 13 102
constructor overloading 2 79
How to Post an If Statement in JSP 3 51
spring example non maven 4 82
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
With the rapid rise in mobile usage, mobile devices are here to stay and have become an integral part of doing business. Here are 9 great apps for your BYOD environment.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 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

21 Experts available now in Live!

Get 1:1 Help Now