This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes JDBC and Relational Databases and the fly likes Problem with comparing results in jdbc? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Problem with comparing results in jdbc?" Watch "Problem with comparing results in jdbc?" New topic
Author

Problem with comparing results in jdbc?

Jan Michael Soan
Ranch Hand

Joined: Feb 08, 2003
Posts: 36
Hello;
I have created a password management program for my payroll system which can add new users but the problem is i can't compare the results if a new record is added to the database.
Each time i compare the new username and the new password the password dailog doesnt continue.
Here's the codes :
/*Program Made By: Jan - Michael Soan
4-D Project Study T-F 3:00 - 4:30 */
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import soan.jan.michael.Splash;
public class Password extends JFrame {
private int tryme1, tryme2;
private Connection connect;
private String url;
public Password()
{
super("Login . . .");
try {
url = "jdbc dbc:Firewall";

Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
connect = DriverManager.getConnection( url );
}
catch ( ClassNotFoundException cnfex ) {
cnfex.printStackTrace();
}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
}
catch ( Exception ex ) {
ex.printStackTrace();
}
Container c = getContentPane();
JLabel x1 = new JLabel("Username :");
JLabel x2 = new JLabel("Password :");
JLabel x3 = new JLabel("");

final JTextField i1 = new JTextField(20);
final JPasswordField i2 = new JPasswordField(20);
i2.setEchoChar('#');
final JTextField i3 = new JTextField(20);
final JTextField i4 = new JTextField(20);
JButton b1 = new JButton("Accept");
JButton b2 = new JButton("Cancel");
final JCheckBox b3 = new JCheckBox("User !");
final JCheckBox b4 = new JCheckBox("Admin !");
final JPanel p1 = new JPanel();
final JPanel p2 = new JPanel();
final JPanel p3 = new JPanel();
p1.setLayout(new GridLayout(4, 2));
p1.setBorder(new javax.swing.border.
TitledBorder(new javax.swing.border.EtchedBorder()));
p1.add(x1); p1.add(i1);
p1.add(x2); p1.add(i2);
p1.add(x3); p1.add(p3);
p3.setLayout(new GridLayout(1, 2));
p3.setBorder(new javax.swing.border.
TitledBorder(new javax.swing.border.EtchedBorder()));
p3.add(b3); p3.add(b4);
p2.setLayout(new FlowLayout());
p2.add(b1); p2.add(b2);
c.add(p1, BorderLayout.CENTER);
c.add(p2, BorderLayout.SOUTH);
Dimension sd = Toolkit.getDefaultToolkit().getScreenSize();
setLocation(sd.width / 2 - 280 / 2,
sd.height / 2 - 170 / 2);
setResizable(false);
setSize(280, 170);
show();
b1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e)
{
try {

// select statement 1 & 2
Statement statement1 = connect.createStatement();
Statement statement2 = connect.createStatement();
String query1 = "SELECT Username FROM UserId";
String query2 = "SELECT Password FROM UserId";

ResultSet rs1 = statement1.executeQuery(query1);
ResultSet rs2 = statement2.executeQuery(query2);
rs1.next();
rs2.next();
String pass1 = "" + rs1.getString(1);
String pass2 = "" + rs2.getString(1);
String pass3 = new String(i2.getPassword());


// select statement 3 & 4
Statement statement3 = connect.createStatement();
Statement statement4 = connect.createStatement();
String query3 = "SELECT Username FROM Admin";
String query4 = "SELECT Password FROM ADmin";

ResultSet rs3 = statement3.executeQuery(query3);
ResultSet rs4 = statement4.executeQuery(query4);
// here is the problem, is rs.next() the problem. Can anyone pls help ! //
rs3.next();
rs4.next();
String pass4 = "" + rs3.getString(1);
String pass5 = "" + rs4.getString(1);
String pass6 = new String(i2.getPassword());

// if - else statement for user
if ( pass1.equals( i1.getText() ) ) {
if ( pass2.equals( pass3 ) ) {
try {
Statement statement = connect.createStatement();
String query = "UPDATE UserSelect SET User='User'";
int result = statement.executeUpdate( query );
System.out.println("User - Initialized !");

statement.close();
}
catch ( SQLException sqley ) {
sqley.printStackTrace();
System.out.println( sqley.toString() );
}
Splash time = new Splash();
dispose();
}
else {
JOptionPane.showMessageDialog(null,"Incorrect Password. . .",
"Warning . . .",JOptionPane.WARNING_MESSAGE);
tryme1++;
if( tryme1 >= 3 )
{
JOptionPane.showMessageDialog(null,"Your not a valid user . . .",
"Error . . .",JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}
}

// if - else statement for admin
else if ( pass4.equals( i1.getText() ) ) {
if ( pass5.equals( pass6 ) ) {
try {
Statement statement = connect.createStatement();
String query = "UPDATE UserSelect SET User='Admin'";
int result = statement.executeUpdate( query );
System.out.println("Admin - Initialized !");

statement.close();
}
catch ( SQLException sqley ) {
sqley.printStackTrace();
System.out.println( sqley.toString() );
}
Splash time = new Splash();
dispose();
}
else {
JOptionPane.showMessageDialog(null,"Incorrect Password. . .",
"Warning . . .",JOptionPane.WARNING_MESSAGE);
tryme1++;
if( tryme1 >= 3 )
{
JOptionPane.showMessageDialog(null,"Your not a valid user . . .",
"Error . . .",JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}
}

// catch wrong username
else {
JOptionPane.showMessageDialog(null,"Incorrect Username . . .",
"Warning . . .",JOptionPane.WARNING_MESSAGE);
tryme2++;
if( tryme2 >= 3 )
{
JOptionPane.showMessageDialog(null,"Your not a valid user . . .",
"Error . . .",JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}
statement1.close();
statement2.close();
}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
}
}
});
b2.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e)
{
i1.setText("");
i2.setText("");
i3.setText("");
i4.setText("");
}
});
}

// Main method
public static void main( String args[] )
{
Password app = new Password();
app.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
}
}

Thanks and God Bless all of You !
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with comparing results in jdbc?
 
jQuery in Action, 3rd edition