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 5989605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:07:33+00:00 2026-05-22T23:07:33+00:00

Just last week, I was doing some PHP stuff. I worked a little solution

  • 0

Just last week, I was doing some PHP stuff. I worked a little solution to prevent SQL injections. PHP has been always my man, it has readily 3 solutions for use (maybe more). One is to enable “magic queries” using stripslashes() function. Another one (the recommended) is to use mysql_real_escape_string() function.
That simple and my problem is solved. However, things don’t seem to be that simple when it comes to JSP. I searched and didn’t find any built-in function to strip slashes or do those sort of things (I believe such functionality can be implemented using basic JAVA functions but…).

Please help me protect my database. I heard about PreparedStatement, but really can’t get my head around it? (I feel the real meaning of newbieness).

  • 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-05-22T23:07:33+00:00Added an answer on May 22, 2026 at 11:07 pm

    Just use PreparedStatement instead of Statement.

    I.e. use

    String sql = "INSERT INTO tbl (col1, col2, col3) VALUES (?, ?, ?)";
    preparedStatement = connection.prepareStatement(sql);
    preparedStatement.setString(1, col1);
    preparedStatement.setString(2, col2);
    preparedStatement.setString(3, col3);
    preparedStatement.executeUpdate();
    

    instead of

    String sql = "INSERT INTO tbl (col1, col2, col3) VALUES ('" + col1 + "', '" + col2 + "', '" + col3 + "')";
    statement = connection.createStatement();
    statement.executeUpdate(sql);
    

    The PreparedStatement also offers convenient setter methods for other types, such as setInt(), setDate(), setBinaryStream(), etcetera.

    Please note that this issue is unrelated to JSP. It’s related to Java in general. Writing raw Java code in a JSP class is also considered a poor practice. Best practice is to create a standalone class which does all the DB interaction tasks on a particular table, which is also called a DAO (Data Access Object) class. You can then import/use this DAO class in a servlet class.

    See also:

    • Java Tutorials – JDBC Tutorial – PreparedStatement
    • Difference between Statement and PreparedStatement
    • how to send a ResultSet object in jsp back to html (javascript)?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lately there has been a problem running some of our reports in access. Last
OK, I have just been reading and trying for the last hour to import
I've been desperately trying for about the last week to figure out whatever my
I just began using Mongoid last week. I am running into this association problem
I'm a student and just started learning C++ last week so this question is
I just watched the last Channel 9 vid on the upcoming parallel extensions to
I've noticed that just in the last year or so, many major websites have
I'm just starting to get into Django, and of course as of last night
Just looking for the first step basic solution here that keeps the honest people
I've just inherited a project, and been told that an entire folder, includes/ needs

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.