-1

I'm getting an error while executing following select query:

ResultSet rs = St.executeQuery("select * from login where username="+username+"and password ="+password);

And the exception is

java.sql.SQLSyntaxErrorException: ORA-00933: SQL command not properly ended. 

Please let me know if the query is wrong syntactically.

bpgergo
  • 15,669
  • 5
  • 44
  • 68
Anil
  • 31
  • 5

6 Answers6

2

Use at least parameters (named parameters are even better). Concatenating values into SQL string is error prone and unsafe. E.g.:

Statement stmt = null;
String query = "select * from login where username=? and password=?";
try {
        stmt = con.createStatement();
        stmt.setString(1, username);
        stmt.setString(2, password);
        ResultSet rs = stmt.executeQuery(query);
        while (rs.next()) {
           //...
        }
    } catch (SQLException e ) {
        //TODO handle e
    } finally {
        if (stmt != null) { stmt.close(); }
    }
}
bpgergo
  • 15,669
  • 5
  • 44
  • 68
1

There's no space between your username and the and keyword. This will parse to

select * from login where username=usernameand password =password

You're also missing single quotes around the values you're inserting into the statement. Try:

ResultSet rs = St.executeQuery("select * from login where username = '" + username + "' and password = '" + password + "'");

I'd also recommend reading about Using Prepared Statements in the Java Tutorials.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
1

values for username and password should be in quotes

ResultSet rs = St.executeQuery("select * from login where username='" + username + "' and password ='" + password + "'");
Alexander Zhugastrov
  • 12,678
  • 2
  • 20
  • 22
0

Try this (you are missing few white spaces and quote marks):

ResultSet rs = 
  St.executeQuery(
    "select * from login where username=\""+username+"\" and password =\""+password + "\"");

Also read about Named parameters in JDBC and SQL injection.

Community
  • 1
  • 1
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
0

single quotations are missing before the value and password ,I think it should be :

ResultSet rs = St.executeQuery("select * from login where username='"+username+"' and password ='"+password+"'");
a.u.r
  • 1,253
  • 2
  • 21
  • 32
0
ResultSet rs = St.executeQuery("select * from login where username='"+username+"' and password ='"+password+"'");

Best way to execute query take it out and try it on your own... for ex.

String query = "select * from login where username='"+username+"' and password = '"+password+"'";

//Print your query and execute it in your sql client you ll get to know if it works!
System.out.println(query);

ResultSet rs = St.executeQuery(query);
RThomas
  • 10,702
  • 2
  • 48
  • 61