Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
package dal;

import addofficer.bl.common.OfficerDTO;
import addofficer.bl.common.Response;
import java.sql.Connection;
import java.sql.Statement;

/**
 *
 * @author Harmeen
 */
public class DAL {
private ConnectionManager objDBConnection = new ConnectionManager("DESKTOP-19VP1EG\\SQLEXPRESS","addOfficer","host","tsoh123");    
            
            
    public Response saveOfficerInDB(OfficerDTO officerDTO) {
        Response objResponse = new Response();
        try{
            Connection objConnection = objDBConnection.getConnection();
            Statement stmt = objConnection.createStatement();                           
            String SQLInsertQuery = "Insert into officerdata(officerID,name,mobileNO,emailID) "
                    + "Values('"+officerDTO.officerID+"','"+officerDTO.name+"',"+officerDTO.mobileNO+",'"+officerDTO.emailID+")";            
            int recordsInserted = stmt.executeUpdate(SQLInsertQuery);
            Response objresponse = new Response();
            
             
            
            if(recordsInserted > 0)
                
            {
              objResponse.addInformationMessage("Record added successfully.");
            }
            else
            {
                objResponse.addInformationMessage("Failed to add officer in database.");
            }
            objConnection.close();
        }
        // Handle any errors that may have occurred.
        catch (Exception e) {
            e.printStackTrace();
            objResponse.AddErrorMessage("Failed to add Officer due to: "+e.getMessage());
        }
            return objResponse;
    }
        }


What I have tried:

please help me to solve this error
Posted
Updated 25-Aug-20 0:32am
Comments
Richard MacCutchan 25-Aug-20 6:29am    
You have unmatched quote characters in your string. Look closely and you will see where they are missing.

This
String SQLInsertQuery = "Insert into officerdata(officerID,name,mobileNO,emailID) "
                    + "Values('"+officerDTO.officerID+"','"+officerDTO.name+"',"+officerDTO.mobileNO+",'"+officerDTO.emailID+")";  
is a horrible and wrong way to build a query, 1) for the reason you have shown - it's error prone, 2) it allows SQL injection .. please look up the java way to perform a parameterized query in java

Here's one link that shows how to build the query properly How to Fix SQL Injection Using Java PreparedStatement & CallableStatement[^] and there this How To Write Named Parameterized Query In Java[^] and possibly this http://tutorials.jenkov.com/jdbc/preparedstatement.html[^]
 
Share this answer
 
v2
Comments
samreen naveed 25-Aug-20 6:49am    
okay thankyou
The problem you have noticed is explained in the error message - you are missing a closing quote:
"Values('"+officerDTO.officerID+"','"+officerDTO.name+"',"+officerDTO.mobileNO+",'"+officerDTO.emailID+")"
                                                                                  ^   
             ^

But that is a very bad idea. Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900