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();
7 1846
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.
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
-
String url = "jdbc:odbc:sakthi";
-
con = (Connection)DriverManager.getConnection(url,"sakthi","");
-
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();
i have also tried with this code... -
-
while(rs.next())
-
{
-
System.out.println(login_name);
-
System.out.println(rs.getString(1));
-
if(rs.getString("user").equals(login_name))
-
-
{
-
System.out.println("hello");
-
uid=(rs.getString(1));
-
psswd=(rs.getString(2)) ;
-
passt.setText(uid);
-
}
-
-
}
-
con.close();
-
-
}
-
catch(Exception s)
-
{
-
System.out.println("Doesn't establish the connection!");
-
System.exit(0);
-
}
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))
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
-
String url = "jdbc:odbc:sakthi";
-
con = (Connection)DriverManager.getConnection(url,"sakthi","");
-
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();
i have also tried with this code... -
-
while(rs.next())
-
{
-
System.out.println(login_name);
-
System.out.println(rs.getString(1));
-
if(rs.getString("user").equals(login_name))
-
-
{
-
System.out.println("hello");
-
uid=(rs.getString(1));
-
psswd=(rs.getString(2)) ;
-
passt.setText(uid);
-
}
-
-
}
-
con.close();
-
-
}
-
catch(Exception s)
-
{
-
System.out.println("Doesn't establish the connection!");
-
System.exit(0);
-
}
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.)
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. - public void actionPerformed(ActionEvent e)
-
{
-
Connection con=null;
-
String login_name=null;
-
-
String uid = null;
-
//String psswd = null;
-
//String str=new String(passt.getPassword());
-
-
if (e.getSource()== sub)
-
{
-
try
-
{
-
-
login_name=usert.getText();
-
login_name=login_name.trim();
-
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
-
String url = "jdbc:odbc:sakthi";
-
con = (Connection)DriverManager.getConnection(url,"sakthi","");
-
Statement stmt=con.createStatement();
-
System.out.println("Connection :"+con);
-
System.out.println("Connection is created!"+login_name);
-
String run=("select * from logg");
-
ResultSet rs=stmt.executeQuery(run);
-
while(rs.next())
-
{
-
System.out.println(login_name);
-
System.out.println(rs.getString(1));
-
if(rs.getString("user").trim().equalsIgnoreCase(login_name.trim()))
-
-
{
-
System.out.println("hello");
-
uid=(rs.getString(1));
-
psswd=(rs.getString(2)) ;
-
-
passt.setText(uid);
-
}
-
else
-
{
-
System.out.println(" tata");
-
}
-
-
}
-
con.close();
-
-
}
-
catch(Exception s)
-
{
-
System.out.println("Doesn't establish the connection!");
-
System.exit(0);
-
}
-
-
}
-
else if (e.getSource()==res)
-
{
-
usert.setText("");
-
passt.setText("");
-
-
}
-
}
output is: - Connection :sun.jdbc.odbc.JdbcOdbcConnection@198dfaf
-
Connection is created!sakthi
-
sakthi
-
sakthi
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.
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.
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).
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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,...
|
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...
| |