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

  • Home
  • SEARCH
  • 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 7629471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:50:39+00:00 2026-05-31T05:50:39+00:00

In my java program I used Hybernate technology to access MySQL database table called

  • 0

In my java program I used Hybernate technology to access MySQL database table called items .That table has columns named “itemname itemprice itemid” & my java program has HQL statements to fetch data. Also it has a combo box which populates from the items table. Once we select an itemname from combo box it automatically fill two non editable jtext fields called itemid & itemprice, & another part of the program has codes to get string values from those jtextfields & write those values in another database table called orders using a POJO class.

I want to know that this kind of program can be attacked by sql injections ???, if we use Hibernate it is safe from sql injection attacks ???….
If my program has security threats briefly explain how can I avoid those…

I post some codes here.
This statement to fill combo box

String SQL_QUERY = "Select items.iname,items.iid,items.iprice from Item items";

This statement fills jtextfields. The “selecteditem” variable is the selected index of the combo box.

String SQL_QUERY ="Select items.iname,items.iid,items.iprice from Item items where items.iid = '"+selecteditem+"'";

This method writes data in orders table

 //To send data to the orders table
private void fillordertable(){
    String itemname = (String) jcbItemCode.getSelectedItem();
    String itempric = jtfItemPrice.getText();
    String tmp = jtfQuantity.getText();
    int itemqty = Integer.parseInt(tmp);
    String temp = jtfUnitPrice.getText();
    double unitpric = Double.parseDouble(temp);
  Session session = null;

  //This variables for validating purposes
  String tempcname = jtfName.getText();
  String tempcemail = jtfEmail.getText();
  if(tempcname.equals("") || tempcemail.equals("")){
      jtaDisplay.setText("Check * fields");
  }
  else{
  try{
    SessionFactory sessionFactory = new org.hibernate.cfg.Configuration().configure().buildSessionFactory();
    session =sessionFactory.openSession();
    session.beginTransaction();

    Order order = new Order();
    order.setItcode(itemcode);
    order.setItdiscription(itemdis);
    order.setItqty(itemqty);
    order.setItemprice(unitpric);
    order.setTotprice(unitpric * itemqty);
    order.setOstatus("Placed");

  session.save(order);
  session.getTransaction().commit();
  }
  catch(Exception exc){
  jtaDisplay.setText(exc.getMessage());
  }
  finally{
    session.flush();
    session.close();
  }
  jtaDisplay.setText("Order & customer tables updated successfully !!!");
}
}

It is difficult to understand my whole code if I post it here. So I have posted some codes which I thought helpful to answer my question. If that is not enough please comment.

Thanks!

  • 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-31T05:50:40+00:00Added an answer on May 31, 2026 at 5:50 am
    String SQL_QUERY ="Select items.iname,items.iid,items.iprice from Item items where items.iid = '"+selecteditem+"'";
    

    is susceptible to sql injection if selectedItem is data entered by the user.

    Generating SQL or HQL queries by concatenating strings is in general bad form, and likely to lead to sql injection possibilities.

    The safe way is to use named parameters in any SQL or HQL.

    In your example, which appears to be SQL, after acquiring a Hibernate session, something like:

    String SQL_QUERY ="Select items.iname,items.iid,items.iprice from Item items where items.iid = :selecteditem";    
    SQLQuery query = session.createSQLQuery(SQL_QUERY);    
    query.setParameter("selecteditem", selecteditem);
    List<Object[]> results = query.list();
    

    should be approximately the way you want to code and execute your query.

    Similar things apply to HQL.

    If you just concatenate strings, the evil values entered by a hacker as in the famous xkcd become part of your query and can do awful things.

    If this is not a web form but a desktop application, you may well be in complete control of the values that can get into this variable, but it’s still advisable to try to do these things correctly.

    Another effect of named parameters is that the parametrized sql can be cached and reused for different values of the parameter. So it’s a good idea even without the security concern.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a Java program that will be used on both Windows and Mac.
I did a small example program to insert data to a MySQL database table
I have a Java program that used a BufferedImage attached to a jPanel to
I'm have a java program that uses Apache httpclient api. This is used to
I recently inherited a small Java program that takes information from a large database,
I have a Java program that runs many small simulations. It runs a genetic
In a Java program (Java 1.5), I have a BufferedWriter that wraps a Filewriter,
I had a Java program that I run thousand times based on a loop
I have a java program that will work with a variety of Java Beans.
I have written a java program that is actually works as a gui to

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.