Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7737375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:58:50+00:00 2026-06-01T07:58:50+00:00

My teacher and I are having a debate about whether it is possible to

  • 0

My teacher and I are having a debate about whether it is possible to SQL inject into a prepared statement. I understand that normally you couldn’t, but the professor insists on using sql concatenation instead of using (?).

Now I am trying to break my code, but I am having no luck.

public Users getUserByUsername(String username) throws SQLException {
    StringBuffer sql = new StringBuffer();

    sql.append("select * from users as  u, user_type_lookup as l, user_types as t ");
    sql.append("where u.users_id=l.user_id and l.type_id=t.user_types_id and u.username='");
    sql.append(username);
    sql.append("';");

    System.out.println(sql.toString());

    PreparedStatement ps = conn.prepareStatement(sql.toString());
    ResultSet rs = ps.executeQuery(sql.toString());

    if (!rs.next()) {
        return null;
    }

    String password = rs.getString("password");
    String type = rs.getString("description");
    int id = rs.getInt("users_id");
    int incorrect_logins = rs.getInt("incorrect_logins");
    Time wait_time = rs.getTime("wait_time");

    Users u = new Users(id, username, password, type, incorrect_logins,
            wait_time);
    return u;
}

Inserts I have tried:

string: '; DELETE FROM users WHERE 1 or users_id = '
string: ';delete from users where username<>'
//The only one that worked    
string: stan' or 'a'<>'b

SQL output(Results in a java error):

select * from users as  u, user_type_lookup as l, user_types as t where u.users_id=l.user_id and l.type_id=t.user_types_id and u.username=''; DELETE FROM users WHERE 1 or users_id = '';

SQL output (works as intended):

select * from users as  u, user_type_lookup as l, user_types as t where u.users_id=l.user_id and l.type_id=t.user_types_id and u.username='stan';

Error message:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your    
SQL syntax; check the manual that corresponds to your MySQL server version for the   
right syntax to use near 'DELETE FROM users WHERE 1 or users_id = ''' at line 1

Server: Tomcat 7

Database: MySQL

IDE: Eclipse

Language: Java

So please help me break my code!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T07:58:51+00:00Added an answer on June 1, 2026 at 7:58 am

    You can’t add a separate statement inside the SQL of the prepared-statement, but you can break it by, for example:

    • using ' OR 'x' = 'x as the username (so that the query will do a Cartesian join across all users and types mappings between them); this will greatly harm performance if users and user_type_lookup are large tables, and would be an excellent start on a denial-of-service attack.
    • using ' OR (SELECT stored_procedure_that_deletes_things()) = 1 (so that the query will invoke a stored-procedure that has deleterious effects).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My teacher specifically requested that we split a sentence into words without using String.split()
My teacher asked me to do a program using execlp that will do the
My teacher told us that there is a default class in Java for binary
I read my teacher's code, but I don't understand. Could anyone please tell me
Apparently my teacher believes that even if we don't have time to learn stuff
in class we went over the subset_of/2 predicate that my teacher gave as follows:
I have Teacher, Student, and Parent models that all belong to User. This is
I'm pushing elements into an array during a while statement. Each element is a
The following switch statement is not catching a cookie that does not exist. switch
We are having a lot of trouble interpreting our teacher. We asked for clarification

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.