473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Error in retrieving data from SQL table :

Hi,
Iam a begginer and i tried to establish the connection with my SQL DB through JAVA. The connection got established but i could not retrieve the data through the query. Could you check and tell whethr the query is correct. thank you.

here is the program::

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newI nstance();
String url = "jdbc:odbc:sakthi";
con = (Connection)DriverManager.getConnection(url,"sakth i","");
Statement stmt=con.createStatement();
System.out.println("Connection :"+con);
System.out.println("Connection is created!"+login_name);

String run=("select * from logg where user ="+"'"+login_name+"'");-----> Is this query correct. i chked wth ony "select * from logg", i got the output. when i tried this one....it's not working. it goes to the 'catch' function.


ResultSet rs=stmt.executeQuery(run);
while(rs.next())
{

uid=rs.getString("user");
psswd=rs.getString("pass") ;
}

con.close();
Sep 28 '07 #1
7 1846
r035198x
13,262 8TB
Hi,
Iam a begginer and i tried to establish the connection with my SQL DB through JAVA. The connection got established but i could not retrieve the data through the query. Could you check and tell whethr the query is correct. thank you.

here is the program::

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newI nstance();
String url = "jdbc:odbc:sakthi";
con = (Connection)DriverManager.getConnection(url,"sakth i","");
Statement stmt=con.createStatement();
System.out.println("Connection :"+con);
System.out.println("Connection is created!"+login_name);

String run=("select * from logg where user ="+"'"+login_name+"'");-----> Is this query correct. i chked wth ony "select * from logg", i got the output. when i tried this one....it's not working. it goes to the 'catch' function.


ResultSet rs=stmt.executeQuery(run);
while(rs.next())
{

uid=rs.getString("user");
psswd=rs.getString("pass") ;
}

con.close();
1.) Please use code tags when posting code.
2.) Did you try to print the sql to see what it says before passing it to the executeQuery.
3.) You'll save yourslef lots of debugging if you just use a PreparedStatement for this.
Sep 28 '07 #2
Expand|Select|Wrap|Line Numbers
  1. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
  2. String url = "jdbc:odbc:sakthi"; 
  3. con = (Connection)DriverManager.getConnection(url,"sakthi","");
  4. Statement stmt=con.createStatement();
  5. System.out.println("Connection :"+con);
  6. System.out.println("Connection is created!"+login_name);
  7.  
  8. String run=("select * from logg where user ="+"'"+login_name+"'");
  9.  
  10. /* Is this query correct. i chked wth ony "select * from logg", i got the output. when i tried this one....it's not working. it goes to the 'catch' function. */
  11.  
  12.  
  13. ResultSet rs=stmt.executeQuery(run);
  14. while(rs.next()) 
  15.  
  16. uid=rs.getString("user");
  17. psswd=rs.getString("pass") ;
  18. }
  19.  
  20. con.close();

i have also tried with this code...
Expand|Select|Wrap|Line Numbers
  1.  
  2. while(rs.next())                        
  3.     {   
  4.      System.out.println(login_name);
  5.      System.out.println(rs.getString(1));
  6.      if(rs.getString("user").equals(login_name))
  7.  
  8.     {
  9.     System.out.println("hello");     
  10.      uid=(rs.getString(1));
  11.      psswd=(rs.getString(2)) ;
  12.      passt.setText(uid);
  13.       }
  14.  
  15.      }
  16.    con.close();
  17.  
  18.     }
  19.         catch(Exception s)
  20.         {
  21.         System.out.println("Doesn't establish the connection!");
  22.             System.exit(0);
  23.         }

in this code, rs.getString(1), the value given in the table is "sample"
login_name-> the value given is also "sample" . but the evaluation in the "if" statement is not working. both the names given are the same but its not cheking it and entering into the if statement even both are same. plz help me out from this.

if(rs.getString("user").equals(login_name))
Sep 28 '07 #3
r035198x
13,262 8TB
Expand|Select|Wrap|Line Numbers
  1. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
  2. String url = "jdbc:odbc:sakthi"; 
  3. con = (Connection)DriverManager.getConnection(url,"sakthi","");
  4. Statement stmt=con.createStatement();
  5. System.out.println("Connection :"+con);
  6. System.out.println("Connection is created!"+login_name);
  7.  
  8. String run=("select * from logg where user ="+"'"+login_name+"'");
  9.  
  10. /* Is this query correct. i chked wth ony "select * from logg", i got the output. when i tried this one....it's not working. it goes to the 'catch' function. */
  11.  
  12.  
  13. ResultSet rs=stmt.executeQuery(run);
  14. while(rs.next()) 
  15.  
  16. uid=rs.getString("user");
  17. psswd=rs.getString("pass") ;
  18. }
  19.  
  20. con.close();

i have also tried with this code...
Expand|Select|Wrap|Line Numbers
  1.  
  2. while(rs.next())                        
  3.     {   
  4.      System.out.println(login_name);
  5.      System.out.println(rs.getString(1));
  6.      if(rs.getString("user").equals(login_name))
  7.  
  8.     {
  9.     System.out.println("hello");     
  10.      uid=(rs.getString(1));
  11.      psswd=(rs.getString(2)) ;
  12.      passt.setText(uid);
  13.       }
  14.  
  15.      }
  16.    con.close();
  17.  
  18.     }
  19.         catch(Exception s)
  20.         {
  21.         System.out.println("Doesn't establish the connection!");
  22.             System.exit(0);
  23.         }

in this code, rs.getString(1), the value given in the table is "sample"
login_name-> the value given is also "sample" . but the evaluation in the "if" statement is not working. both the names given are the same but its not cheking it and entering into the if statement even both are same. plz help me out from this.

if(rs.getString("user").equals(login_name))
Perhaps the value in the database contains some leading or trailing spaces (use TRIM). Perhaps the case is different (use equalsIgnoreCase)

You are also violating some rules of getting data from a result set (read this thread.)
Sep 28 '07 #4
Perhaps the value in the database contains some leading or trailing spaces (use TRIM). Perhaps the case is different (use equalsIgnoreCase)

You are also violating some rules of getting data from a result set (read this thread.)
I hav tried both TRIM and IGNORECASE...but still i cant get it...but if i give [HTML]System.out.println(rs.getString(1));[/HTML]

iam able to get the data from database....but can perform the if statement. i have given the entire program and the output. can u help me out.

Expand|Select|Wrap|Line Numbers
  1. public void actionPerformed(ActionEvent e) 
  2.     {  
  3.         Connection con=null;
  4.         String login_name=null;
  5.  
  6.         String uid = null;
  7.         //String psswd = null;
  8.         //String str=new String(passt.getPassword());
  9.  
  10.         if (e.getSource()== sub)
  11.         {
  12.             try
  13.             {
  14.  
  15.                   login_name=usert.getText();
  16.                   login_name=login_name.trim();
  17.                   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
  18.                   String url = "jdbc:odbc:sakthi"; 
  19.                   con = (Connection)DriverManager.getConnection(url,"sakthi","");
  20.                   Statement stmt=con.createStatement();
  21.                   System.out.println("Connection :"+con);
  22.                   System.out.println("Connection is created!"+login_name);
  23.                   String run=("select * from logg");
  24.                   ResultSet rs=stmt.executeQuery(run);
  25.                   while(rs.next())                        
  26.                 {   
  27.                      System.out.println(login_name);
  28.                      System.out.println(rs.getString(1));
  29.                      if(rs.getString("user").trim().equalsIgnoreCase(login_name.trim()))
  30.  
  31.                      {
  32.                      System.out.println("hello");     
  33.                      uid=(rs.getString(1));
  34.                      psswd=(rs.getString(2)) ;
  35.  
  36.                      passt.setText(uid);
  37.                      }
  38.                      else
  39.                      {
  40.                          System.out.println(" tata");
  41.                      }
  42.  
  43.                   }
  44.                  con.close();
  45.  
  46.             }
  47.                 catch(Exception s)
  48.                 {
  49.                   System.out.println("Doesn't establish the connection!");
  50.                   System.exit(0);
  51.                 }
  52.  
  53.         }
  54.         else if (e.getSource()==res)
  55.         {
  56.             usert.setText("");
  57.             passt.setText("");
  58.  
  59.         }
  60.     }
output is:
Expand|Select|Wrap|Line Numbers
  1. Connection :sun.jdbc.odbc.JdbcOdbcConnection@198dfaf
  2. Connection is created!sakthi
  3. sakthi
  4. sakthi
Sep 28 '07 #5
r035198x
13,262 8TB
You did not do anything about the third comment I made in my post above.
Why don't you print the values using println statements and see if they match.
Sep 28 '07 #6
i have done that also. i have tried with many System.out.println() in displaying the values. Till the If Statement in the While loop(rs.next())..its working. The database is retrieved. can u chk what mistake i have made in that coding. just the coding inside the while loop(); thank you.
Sep 28 '07 #7
r035198x
13,262 8TB
i have done that also. i have tried with many System.out.println() in displaying the values. Till the If Statement in the While loop(rs.next())..its working. The database is retrieved. can u chk what mistake i have made in that coding. just the coding inside the while loop(); thank you.
Retrieve the value once from the resultset using getString and store it in a String variable. Every time you want to access it again just use that variable and don't call rs.getString again (you should really have a look at my third comment above).
Sep 28 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: Steve Jorgensen | last post by:
If you've ever employed custom error numbers and messages in you programs, you've probably ended up with code similar to what I've ended up with in the past something like... <code> public...
0
by: Alistair | last post by:
Hi all, I am creating a database based site that keeps track of books, who has read them and the comments they have. After a little help in M.P.I.asp.DB I managed to create a database (access...
7
by: Mathew Butler | last post by:
I'm investigating an issue I have when pulling data back from MS access I'm retrieving all rows from a column in a table of 5000 rows - accessing a column of type "memo" ( can be 65353 character...
2
by: Nemisis | last post by:
hi all, If i have a table in my database, and that table contains foreign keys to other tables, should i extract the data from the other tables at the same time, or should my business layer call...
0
by: funky4you | last post by:
I am using a simple select query in C# app that fetches a row which has a datatime field. The query is simple "SELECT * FROM THE USERS WHERE USER_NAME = 'ABC' ", the users table contains a...
1
by: John | last post by:
Hi I have a perfectly working vs 2003 winform data app. All the data access code has been generated using the data adapter wizard and then pasted into the app. I had to add a new field in the...
0
by: mike1402 | last post by:
Hi ! I get the error below sometimes when retrieving a big amount of data using Datadapter.Fill(dataset,"table"). But when I send the command Fill again, there is no error. Is it a fault of...
8
by: JJ | last post by:
This is slightly OT but .... When testing why my aspx page isn't returning the data as expected I decided to type directly into one of my SQL tables. (The column definitions are below). When...
5
by: Sanjay Pais | last post by:
I have a table with over 1.3 million rows. I am retrieving only 20 at a time using the with - over clauses In query analyser, the data is retrieved in under a second. When retrieving using the...
0
bmallett
by: bmallett | last post by:
First off, i would like to thank everyone for any and all help with this. That being said, I am having a problem retrieving/posting my dynamic form data. I have a form that has multiple options...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.