10 Replies - 357 Views - Last Post: 16 July 2019 - 01:56 AM Rate Topic: -----

#1 dako   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 01-July 19

Having an issue getting the ResultSet next() method to work!

Posted 10 July 2019 - 08:31 PM

When I run the code I am attaching in this post, the login button does nothing. I know that it is something wrong with the while(rs.next()) loop so if someone could tell me why my login button is unresponsive I would really appreciate it!

PS I know that it is the rs.next() inside the loop because when I change the condition inside of the loop the login button works. Thank you all!

import java.awt.EventQueue;

import javax.swing.JFrame;
import java.sql.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;



public class DBGui {

	
	private JFrame frame;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					DBGui window = new DBGui();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	Connection connection = null;
	private JTextField userField;
	private JPasswordField passwordField;
	public DBGui() {
		initialize();
		connection = dbManager.dbConnector();
		
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		userField = new JTextField();
		userField.setBounds(11, 42, 101, 19);
		frame.getContentPane().add(userField);
		userField.setColumns(10);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(11, 149, 101, 19);
		frame.getContentPane().add(passwordField);
		
		JButton btnLogin = new JButton("Login");
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			try {
				String query = "select * from sample where name=? and password=?";

				PreparedStatement pst = connection.prepareStatement(query);

					pst.setString(1, userField.getText());
					pst.setString(2, passwordField.getText());
					
					ResultSet rs = pst.executeQuery();
					int count = 0;
					while(rs.next()) {
						count = count + 1;
						
						if(count == 1) {
							JOptionPane.showMessageDialog(null, "Connection made!");
						System.out.println("1");
						}	else {
							JOptionPane.showMessageDialog(null, "Fail");
						System.out.println("2");
						}	}
					rs.close();
					pst.close();
			} catch (Exception e2) {
				// TODO: handle exception
				JOptionPane.showMessageDialog(null, e2);
				System.out.println(e2);
			}
				
			}
		});
		btnLogin.setBounds(48, 232, 85, 21);
		frame.getContentPane().add(btnLogin);
	}

}




Is This A Good Question/Topic? 0
  • +

Replies To: Having an issue getting the ResultSet next() method to work!

#2 wseng92   User is offline

  • D.I.C Addict
  • member icon

Reputation: 49
  • View blog
  • Posts: 677
  • Joined: 23-September 15

Re: Having an issue getting the ResultSet next() method to work!

Posted 10 July 2019 - 10:55 PM

Any exceptions?
Was This Post Helpful? 0
  • +
  • -

#3 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3635
  • View blog
  • Posts: 16,702
  • Joined: 20-September 08

Re: Having an issue getting the ResultSet next() method to work!

Posted 11 July 2019 - 01:42 AM

Quote

pst.setString(2, passwordField.getText());

should actually be
pst.setString(2, new String(passwordField.getPassword()));

though that might not fix it. Print the strings and make sure you can execute the query by other means with success
Was This Post Helpful? 0
  • +
  • -

#4 dako   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 01-July 19

Re: Having an issue getting the ResultSet next() method to work!

Posted 11 July 2019 - 08:35 AM

So yeah, there is no exceptions.

And btw I do appreciate you showing me a way to get the password that isn't deprecated haha!

No but, so to provide more clarity to the issue: There is not an exception it is just that rs.next() is sending false rather than true. And I have no idea why it is doing this... I am new to Java and databases. If someone could skim through and I can provide the Connection .java file too if necessary.
Was This Post Helpful? 0
  • +
  • -

#5 wseng92   User is offline

  • D.I.C Addict
  • member icon

Reputation: 49
  • View blog
  • Posts: 677
  • Joined: 23-September 15

Re: Having an issue getting the ResultSet next() method to work!

Posted 11 July 2019 - 10:43 AM

Have you tested your database connection? Did it successfully connected?

This post has been edited by wseng92: 11 July 2019 - 10:46 AM

Was This Post Helpful? 0
  • +
  • -

#6 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3635
  • View blog
  • Posts: 16,702
  • Joined: 20-September 08

Re: Having an issue getting the ResultSet next() method to work!

Posted 11 July 2019 - 11:38 AM

You didn't test the query outside Java?

Quote

JOptionPane.showMessageDialog(null, "Connection made!");

btw is a misleading message. A connection is a prerequisite for any query, irrespective of its returning results

This post has been edited by g00se: 11 July 2019 - 11:42 AM

Was This Post Helpful? 0
  • +
  • -

#7 dako   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 01-July 19

Re: Having an issue getting the ResultSet next() method to work!

Posted 11 July 2019 - 10:33 PM

View Postg00se, on 11 July 2019 - 11:38 AM, said:

You didn't test the query outside Java?

Quote

JOptionPane.showMessageDialog(null, "Connection made!");

btw is a misleading message. A connection is a prerequisite for any query, irrespective of its returning results


As in, whether or not it was actually connected with the database I was using, it would still show a connection? How do I make a test that actually works.. maybe I should try to retrieve a specific name off of a table from the database that says connection made?
Was This Post Helpful? 0
  • +
  • -

#8 wseng92   User is offline

  • D.I.C Addict
  • member icon

Reputation: 49
  • View blog
  • Posts: 677
  • Joined: 23-September 15

Re: Having an issue getting the ResultSet next() method to work!

Posted 11 July 2019 - 11:29 PM

Quote

How do I make a test that actually works..

You can use this code to test your database connection.
https://gitlab.com/w...Connection.java

Make sure dbName and username is same with your database set up.

This post has been edited by wseng92: 11 July 2019 - 11:32 PM

Was This Post Helpful? 0
  • +
  • -

#9 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3635
  • View blog
  • Posts: 16,702
  • Joined: 20-September 08

Re: Having an issue getting the ResultSet next() method to work!

Posted 12 July 2019 - 01:46 AM

Quote

As in, whether or not it was actually connected with the database I was using, it would still show a connection?

What i'm saying is that your message doesn't accord with the code. What would accord with it is something like "At least one record found". And that might lead you to ask yourself why you're looking for more than one too.
But you need to separate your db logic from your code implementation or it can lead to errors where you think an error in the former is an error in the latter, and vice versa

This post has been edited by g00se: 12 July 2019 - 01:48 AM

Was This Post Helpful? 0
  • +
  • -

#10 dako   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 01-July 19

Re: Having an issue getting the ResultSet next() method to work!

Posted 15 July 2019 - 08:00 PM

I'm such a rookie g00se that I don't even understand what that means.

But in other news, I got the database stuff to work by executing the query with Statement instead of PreparedStatement. I don't really know why that was a factor, but it was. Whenever I called the while(rs.next()) when I set rs = PreparedStatement pst = connection.prepareStatement(query);

Actually, if someone could explain why that works it would be awesome... and PLEASE keep in mind I have zero background in logic, zero background in computer science... I am learning this stuff just like most of you guys did when you were probably 12. Break things down if it isn't too much of a hassle.
Was This Post Helpful? 0
  • +
  • -

#11 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3635
  • View blog
  • Posts: 16,702
  • Joined: 20-September 08

Re: Having an issue getting the ResultSet next() method to work!

Posted 16 July 2019 - 01:56 AM

Well in order to have a chance of commenting we'd need at least to see all the new code
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1