Username and password exist but not found and authenticated - java

The password and username are retrieved from the database. If one row is returned, a user exists. However, the below code says that the user does not exist even if the username and password are correct.
if(con != null) {
try {
String query = "select * from users where username = '"+user.getText()+"' and password = '"+pasword.getText()+"'";
PreparedStatement ps = con.prepareStatement(query);
ResultSet rs = ps.executeQuery();
int count = 0;
while(rs.next()) {
count = count + 1;
}
if(count == 1) {
JOptionPane.showMessageDialog(null, "user exist");
} else {
JOptionPane.showMessageDialog(null, "user doesnot exist");
}
rs.close();
ps.close();
} catch (Exception e1) {
}
}

It´s a sensible code, but you should to write the
e.printStackTrace()
in the catch block, maybe an exception it's occurs. Check the user.getText() and password.getText() content, also you can apply an user.getText().trim() for example, to delete de blank spaces. Then try this:
while(rs.next()) {
count++;
}
if(count > 0) {
JOptionPane.showMessageDialog(null, "user exist");
} else {
JOptionPane.showMessageDialog(null, "user doesnot exist");
}
Good luck!

Related

SQLException: query does not return results

I am doing a project in Java Swing and using SQLite as my database.
This is the function I wrote for deleting a record from the Room table in my database.
public void Delete() {
String room_code = jTextField5.getText();
String sql = "DELETE FROM Room WHERE room_code = '" + room_code + "'";
try {
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
if (rs.next()) {
JOptionPane.showMessageDialog(null, "Room Deleted Successfully");
}
else {
JOptionPane.showMessageDialog(null, "Invalid Room Code");
}
}
catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex);
}
}
However, I am met with the following Exception: SQLException: query does not return results.
I have tried to use pst.executeUpdate() as suggested in other answers but it says "int cannot be converted into resultset".
A DELETE statement does not return a result set. You should call method executeUpdate rather than method executeQuery.
Also, you can use place holders with a PreparedStatement.
Also you should use try-with-resources
Consider the following code.
public void Delete() {
String room_code = jTextField5.getText();
String sql = "DELETE FROM Room WHERE room_code = ?";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(room_code);
int count = ps.executeUpdate();
if (count > 0) {
JOptionPane.showMessageDialog(null, "Room Deleted Successfully");
}
else {
JOptionPane.showMessageDialog(null, "Invalid Room Code");
}
}
catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex);
}
}

if-else loop condition is not working using java swing?

I'm trying to fetch database records of user name and password and validate using if-else statement. Final else statement is not displaying message dialog. I'm stuck with this, suggest me a solution and say where i done mistake.
My complete code
String s1 = id.getText();
String s2 = new String(pass.getPassword());
try
{
Class.forName("org.apache.hive.jdbc.HiveDriver");
Connection con = DriverManager.getConnection("jdbc:hive2://localhost:10000/twitter_db","arunachalam","");
String sql = "select userid, password from user_reg where userid='"+s1+"'";
PreparedStatement ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while(rs.next())
{
if((rs.getString("userid").equals(s1)) && (rs.getString("password").equals(s2)))
{
dispose();
showMessageDialog(null,"Login Successfully");
new UserPage().setVisible(true);
}
else if((rs.getString("userid").equals(s1)) && (!rs.getString("password").equals(s2)))
{
showMessageDialog(null,"Incorrect Password");
}
else
{
showMessageDialog(null,"Invalid User");
}
}
}
catch(Exception e)
{
showMessageDialog(null, e);
}
You checked same condition twice for user id and password so every time it checks first else if statement and it does't satisfy final else if.
Obeserve in your below piece of source code.
else if((rs.getString("userid").equals(s1)) && (!rs.getString("password").equals(s2)))
{
showMessageDialog(null,"Incorrect Password");
}
else if((rs.getString("userid").equals(s1)) && (!rs.getString("password").equals(s2)))
{
showMessageDialog(null,"Incorrect Password");
}
May be you are trying for below code that i have updated
// check for user id not equal
else if((!rs.getString("userid").equals(s1)) && (rs.getString("password").equals(s2)))
{
showMessageDialog(null,"Incorrect User Id");
}
else if((rs.getString("userid").equals(s1)) && (!rs.getString("password").equals(s2)))
{
showMessageDialog(null,"Incorrect Password");
}

APOSTROPHE issue with java and SQL

I have code, where I have single quote or APOSTROPHE in my search
I have database which is having test table and in name column of value is "my'test"
When running
SELECT * from test WHERE name = 'my''test';
this works fine
If I use the same in a Java program I am not getting any error or any result
But If I give the name with only single quote then it works
SELECT * from test WHERE name = 'my'test';
Could you please help me out to understand.
Java code is
Connection con = null;
PreparedStatement prSt = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.
getConnection("jdbc:oracle:thin:#localhost:1521:orcl"
,"user","pwd");
String query = "SELECT * from "
+ "WHERE name = ? ";
prSt = con.prepareStatement(query);
String value = "my'mobile";
char content[] = new char[value.length()];
value.getChars(0, value.length(), content, 0);
StringBuffer result = new StringBuffer(content.length + 50);
for (int i = 0; i < content.length; i++) {
if (content[i] == '\'')
{
result.append("\'");
result.append("\'");
}
else
{
result.append(content[i]);
}
}
prSt.setObject(1, result.toString());
int count = prSt.executeUpdate();
System.out.println("===============> "+count);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally{
try{
if(prSt != null) prSt.close();
if(con != null) con.close();
} catch(Exception ex){}
}
You don't have to escape anything for the parameter of a PreparedStatement
Just use:
prSt = con.prepareStatement(query);
prSt.setString("my'mobile");
Additionally: if you are using a SELECT statement to retrieve data, you need to use executeQuery() not executeUpdate()
ResultSet rs = prst.executeQuery();
while (rs.next())
{
// process the result here
}
You might want to go through the JDBC tutorial before you continue with your project: http://docs.oracle.com/javase/tutorial/jdbc/index.html

Unable to Match user and pass Login form netbeans

how can I match user and password in the conditions below. I am always getting UserName/Password does not match.
try
{
Conn();
st = conn.createStatement();
rs = st.executeQuery("Select UserName,PassWord from login_tbl where UserName = '"+txtUser.getText()+"' and PassWord = '"+txtPass.getPassword()+"'");
if(rs != null)
{
new CarRent().setVisible(true);
this.dispose();
}
else
{
JOptionPane.showMessageDialog(null, "Your Username or Password is incorrect","",JOptionPane.ERROR_MESSAGE);
}
}
You should use this rs.next() instead of this rs != null
try
{
JOptionPane.showMessageDialog(null, "Username is : " + txtUser.getText().trim().toString() + " Password is : " + txtPass.getPassword().trim().toString(),"",JOptionPane.ERROR_MESSAGE);
Conn();
st = conn.createStatement();
rs = st.executeQuery("Select UserName,PassWord from login_tbl where UserName = '"+txtUser.getText()+"' and PassWord = '"+txtPass.getPassword()+"'");
if(rs.next())
{
JOptionPane.showMessageDialog(null, "Password is valid","",JOptionPane.ERROR_MESSAGE);
new CarRent().setVisible(true);
this.dispose();
}
else
{
JOptionPane.showMessageDialog(null, "Your Username or Password is incorrect","",JOptionPane.ERROR_MESSAGE);
}
i already solved my problem thanks to all :)
String sql = "select * from login_tbl where usern=? and pass=?";
try{
Conn();
ps=conn.prepareStatement(sql);
ps.setString(1,txtUser.getText());
ps.setString(2,new String(txtPass.getText()));
rs=ps.executeQuery();
if (rs.next()){
//about s= new about();
//
//s.setVisible(true);
new CarRent().setVisible(true);
dispose();
}
else
{
JOptionPane.showMessageDialog(null, "Password or Username is incorrect");
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, e);
}

Reached end of file while parsing what do imiss

Compiling...
src\server\model\players\packets\Commands.java:1389: reached end of file while parsing
}→
^
1 error
Press any key to continue . . .
What am I missing?
if (playerCommand.startsWith("auth") && playerCommand.length() > 5) {
if (!Config.MYSQL_ACTIVE) {
c.sendMessage("Sorry this is currently disabled.");
return;
} else {
try {
PreparedStatement ps = Database.getConnection().prepareStatement("SELECT * FROM votes WHERE username = ? AND used = '1' LIMIT 1");
ps.setString(1, c.playerName);
ResultSet results = ps.executeQuery();
if (results.next()) {
c.sendMessage("You have already voted once today.");
} else {
ps.close();
ps = Database.getConnection().prepareStatement("SELECT * FROM votes WHERE authcode = ? AND used = '0' LIMIT 1");
ps.setString(1, playerCommand.substring(5));
results = ps.executeQuery();
if (results.next()) {
ps.close();
ps = Database.getConnection().prepareStatement("UPDATE votes SET used = '1' WHERE authcode = ?");
ps.setString(1, playerCommand.substring(5));
ps.executeUpdate();
c.getItems().addItem(995, 10000000);
c.sendMessage("Thank you for voting.");
} else {
c.sendMessage("The auth code is not valid!");
}
}
ps.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return;
Make sure that your class have proper opening and closing braces { ... }
A missing brace is typically what causes such an error...

Categories