1

I am using Java netbeans and mysql. I want to check whether the value entered by the user in a textfield tf is already present in the mysql table or not.

 String query1="SELECT * FROM trytable WHERE name='8'";
 ResultSet rs=stmt.executeQuery(query1);
if(rs.isBeforeFirst()==true){JOptionPane.showMessageDialog(null,"already");}

In the above code in place of 8 I want to give the value that the user input in the form and then check whether that value already exist in form or not.

Please help me in the first line . Thanks

1
  • 1
    So you have two issues: How to declare a variable in Java netbeans, and how to query a database? For the second point, what if someone inserts '8' while you're in the middle of checking? Commented Jul 29, 2018 at 5:02

3 Answers 3

1

You should use a PreparedStatement instead of a regular statement. This is more secure than a normal Statement and allows you to avoid SQL injection issues.

You would change your query like so:

String query = "SELECT * FROM trytable WHERE name='?';";

Note the ? at the end of the query. This can be replaced later in your code when setting up the PreparedStatement:

PreparedStatement preparedStatement = connection.prepareStatement(query);
preparedStatement.setString(1, userInput);

ResultSet rs = preparedStatement.executeQuery();
if (rs.next()) System.out.println("Record exists!");

Here, you are telling the prepared statement to replace the first ? in the query, with the value of userInput. So, if the user inputs a 3, the query that gets executed would be SELECT * FROM trytable WHERE name=3;.

Also note that rs.next() returns true if the query returns any results, so that would be the proper way to determine if the record exists.

Sign up to request clarification or add additional context in comments.

6 Comments

Note that will also return usernames like '3alpha'
@Strawberry - How would it return that?
I see. I made the assumption that name would be an integer type. Probably a poor assumption, so I updated my answer to include the '. Thank you :)
Hm, you think ;-)
But I don't think that's the right way to prepare strings
|
0

ResultSet is like a table, it has a cursor. At the beginning the cursor is above the first row so isBeforeFirst() will always return true even there are no results in the ResultSet.

In order to retrieve results you need to move the cursor to the next row, to do that you can use,

rs.next()

If the cursor moved to the next row successfully (which means there are more results) it will return true otherwise false. As you only need the first result you can also use,

rs.first()

to confirm there are data available in the returned ResultSet.


Try,

if (rs.first()) {
  JOptionPane.showMessageDialog(null, "already");
}

6 Comments

I tried but its not fixed yet. Please check: Code: imgur.com/a/McRnHb3 Error: imgur.com/a/FkjYanN
@DhirendraSinghBisht Which SQL statement gives the error?
@Zephyr Thanks to you your codes and explanation helped me a lot.
Thanks to you your codes and explanation helped me a lot.
@DhirendraSinghBisht My pleasure :)
|
0

This is the final code will is working absolutely fine.

try{
                    Class.forName("com.mysql.jdbc.Driver");
                    Connection conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root","");
                    String query = "SELECT * FROM table_name WHERE name=?;";
                    PreparedStatement preparedStatement = conn.prepareStatement(query);
                    preparedStatement.setString(1,jtf.getText());
                    ResultSet rs = preparedStatement.executeQuery();
                    if(rs.next()==true){
                        JOptionPane.showMessageDialog(null,"Value already exist");
                    }
                    else{
                        JOptionPane.showMessageDialog(null,"Value not present");
                        String query1="INSERT INTO table_name(col_name) VALUES (?)";
                        preparedStatement = conn.prepareStatement(query1);
                        preparedStatement.setString(1,jtf.getText());
                        preparedStatement.execute();
                        JOptionPane.showMessageDialog(null,"DONE");

                    }
                    rs.close();
                    preparedStatement.close();
                }
                catch(Exception e){
                    System.out.println("Exception:"+e.getMessage());
                }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.