validating string between jdbc and textfield

Hi guys,

I am having a problem with JDBC. I am posting the app here. The problem is, I couldnt compare the testfield text and the database string. Am i doing something wrong or is there any other way?

******************************************

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.sql.*;

public class test extends JFrame implements ActionListener{

JTextField tid;

JTextField tpass;

public test(){

JLabel id=new JLabel("ID");

JLabel pass=new JLabel("Password");

tid=new JTextField(10);

tpass=new JTextField(10);

JButton validate=new JButton("VALIDATE");

GridBagLayout g1=new GridBagLayout();

GridBagConstraints gbc=new GridBagConstraints();

Container content=getContentPane();

content.setLayout(g1);

gbc.anchor=GridBagConstraints.CENTER;

gbc.gridx=0;

gbc.gridy=0;

g1.setConstraints(id,gbc);

content.add(id);

gbc.anchor=GridBagConstraints.CENTER;

gbc.gridx=2;

gbc.gridy=0;

g1.setConstraints(tid,gbc);

content.add(tid);

gbc.anchor=GridBagConstraints.CENTER;

gbc.gridx=0;

gbc.gridy=1;

g1.setConstraints(pass,gbc);

content.add(pass);

gbc.anchor=GridBagConstraints.CENTER;

gbc.gridx=2;

gbc.gridy=1;

g1.setConstraints(tpass,gbc);

content.add(tpass);

gbc.anchor=GridBagConstraints.CENTER;

gbc.gridx=1;

gbc.gridy=2;

g1.setConstraints(validate,gbc);

content.add(validate);

validate.addActionListener(this);

}

public void actionPerformed(ActionEvent evt){

try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con;

con=DriverManager.getConnection("jdbc:odbc:javatest","sa",null);

PreparedStatement stat=con.prepareStatement("select * from javatest where cid=?");

stat.setString(1,tid.getText());

ResultSet result=stat.executeQuery();

while(result.next())

{

System.out.println("REached");

if(result.getString(2)==tpass.getText())

System.out.println("Successfulll");

else

System.out.println("Failure");

}

}catch(Exception e)

{

System.out.println(e);

}

}

public static void main(String args[]){

test t1=new test();

t1.setSize(300,300);

t1.setVisible(true);

}

}

******************************************

here is the sql code:

create table javatest

(

cid varchar(10),

pass varchar(10

)

reply please, thanks.

[2653 byte] By [megamatrix] at [2007-9-26 3:02:50]
# 1

You need to be more precise you you are describing problems.

Explain what you expected to happen and what actually happened.

You could have more than one problem but to me the following code

result.getString(2)==tpass.getText()

looks highly suspicious. You are comparing the equality of two objects rather than the equality of the value of the two objects. You would do that like this

result.getString(2).equals(tpass.getText())

This does of course presume that getString(2) will never return a null.

jschell at 2007-6-29 11:02:11 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2
I tried that too, its not working. I also tried to reduce the string to characters and then compare them, that too is not working.
megamatrix at 2007-6-29 11:02:11 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

As already noted you must not use " = " to compare 2 strings.

Then, it sounds to me that you try to retrieve a string located at column 2 when you previously set a value into column 1.

:( setString(1, txt.getText() )

and then

( getString (2 ) )

Did you try to print out the result from your database query ?

fquinet at 2007-6-29 11:02:11 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4
And of course the case has to match exactly too.
jschell at 2007-6-29 11:02:11 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5
you might want to trim the textfield to rid any unwanted whitespaces accidentally entered by the user:stat.setString(1,tid.getText().trim());Jamie
jlrober at 2007-6-29 11:02:11 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 6
I printed out the result of the query, both the strings have the same oontent, but they are failing to compare to each other.
megamatrix at 2007-6-29 11:02:11 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 7

I had to add a primary key and therfore columns were offseted of one this is why i got getString(3)

your code works !!!

while(result.next())

{

System.out.println("REached");

if(result.getString(3).equals(tpass.getText()))

System.out.println("Successfulll");

else

System.out.println("Failure");

}

}catch(Exception e)

{

System.out.println(e);

}

fquinet at 2007-6-29 11:02:11 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 8

>both the strings have the same content, but they are failing to compare to each other.

That isn't possible.

Print the strings like this

System.out.println("s1=<" + s1 "> s2=<" + s2 + ">");

The greater/than signs will visibly delimit the strings.

jschell at 2007-6-29 11:02:11 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...