1

I want to be able to update my table using the codes that I tried but it always shows the error just like that

I tried the prepared statement but I don't know if I did it correctly.

So the code looks like this,

try{
    String sql = "update master.Employees set EmployeeName = '" + jTextField1.getText() + "'" + ",Address = '" + jTextField3.getText() + "'" + ",PhoneNumber = '" +
            jTextField4.getText() + "'" + ",EmailAddress = '" + jTextField5.getText() + "'" + "WHERE EmployeeID = " + jTextField1.getText();
    Statement update = myconObj.createStatement();
    update.executeUpdate(sql);
}
catch (SQLException e){
    e.printStackTrace();
}

i tried to enter the employee Id which is the primary key of my database to be able to update but it always shows the error:

java.sql.SQLSyntaxErrorException: Columns of type 'INTEGER' cannot hold values of type 'CHAR'.

I think I messed up the grab structure because it shows this one

¬í sr %org.netbeans.lib.ddl.impl.CreateTable¢”j ™   xr +org.netbeans.lib.ddl.impl.ColumnListCommand2›Œî]2)³ L columnst Ljava/util/Vector;xr )org.netbeans.lib.ddl.impl.AbstractCommandø8¦Œ·q•ª Z executionWithExceptionZ    newObjectL addpropst Ljava/util/Map;L formatt Ljava/lang/String;L nameq ~ L ownerq ~ L quoteStrq ~ L spect ,Lorg/netbeans/lib/ddl/DatabaseSpecification;xp pt :create table [{object.owner}.]{object.name}
(
    {columns}
)t  EMPLOYEESpppsr java.util.VectorÙ—}[€;¯ I capacityIncrementI elementCount[ elementDatat [Ljava/lang/Object;xp       ur [Ljava.lang.Object;ÎXŸs)l  xp   
sr %org.netbeans.lib.ddl.impl.TableColumn;¦±5‘b  I decsizeZ nullableI sizeI typeL checkeq ~ L constraintColumnsq ~ L defvalq ~ xr -org.netbeans.lib.ddl.impl.AbstractTableColumn¸Ô§y[k  Z   newColumnZ  newObjectL addpropsq ~ L cnameq ~ L formatq ~ L nameq ~ L otypeq ~ L refcolq ~ L reftabq ~ xppt 
EMPLOYEEIDt ³{column.name} {column.type}[({column.size}[,{column.decsize}])][ {column.type.suffix}][ default {default.value}][ {column.notnull}not null][ check ({check.condition})] primary keyt 
EMPLOYEEID_PKt PRIMARY_KEYpp        
   pppsq ~ pt EMPLOYEENAMEt §{column.name} {column.type}[({column.size}[,{column.decsize}])][ {column.type.suffix}][ default {default.value}][ {column.notnull}not null][ check ({check.condition})]q ~ t COLUMNpp       2   pppsq ~ pt ADDRESSq ~ q ~ q ~ pp       2   pppsq ~ pt PHONENUMBERq ~ q ~ q ~ pp       
   pppsq ~ pt EMAILADDRESSq ~ q ~ q ~ pp       2   pppsq ~ pt IMAGEq ~ q ~ !q ~ pp       2   pppppppx
4
  • 2
    Can you share your table's definition? Commented May 9, 2019 at 13:52
  • the table is created in the database within the netbeans and y primary key is the employeeID which is integere. Commented May 9, 2019 at 14:02
  • 2
    Please learn how to use prepared statements. Concatenating values into a query string is unsafe as it opens your code to SQL injection, and in addition it can cause all kinds of other errors. Commented May 9, 2019 at 15:11
  • Thank you for the tip, I'll keep that in mind Commented May 9, 2019 at 15:26

6 Answers 6

2

Try to used PreparedStatement like below :

 try
      {
          String sql = "UPDATE `master.Employees` SET `EmployeeName`=?, `Address `=?, `PhoneNumber`=?, `EmailAddress`=? WHERE `EmployeeID `=?";
         PreparedStatement ps = con.prepareStatement(sql);
         //putting value for all placeholder (?)
         ps.setString(1,jTextField2.getText());
         ps.setString(2,jTextField3.getText());
         ps.setInt(3,Integer.parseInt(jTextField4.getText()));
         ps.setString(4,jTextField5.getText());
         ps.setInt(5,Integer.parseInt(jTextField1.getText()));

         int i=0;
         i = ps.executeUpdate();
         if(i>0)
             {
         //do   something   
             }
     }
     catch(Exception e)
      {

      e.printStackTrace();

      }
Sign up to request clarification or add additional context in comments.

1 Comment

Glad i helped :)
0

You need to convert the value of jTextField1.getText (), to pass an integer. You can do it in the following way before doing the update

int id = Integer.parseInt (jTextField1.getText ());

1 Comment

edit the question and put the DDL scheme as comments @Mureinik to help you better
0

maybe other field is Integer, you can check PhoneNumber this is desgined by Integer?

1 Comment

Yes, the employeeId and phone number is integer
0

The function call jTextField1.getText() basically returns a String object and the database is expecting an integer. Try Integer.parseInt(jTextField1.getText()) instead.

7 Comments

I did what you said but, it shows the same error again
As @Mureinik suggested we will need to look at the DDL of the employees table.
uhm, i'm sorry but where can i find the DDL of my database in netbeans???
You will need to go to the services tab. Select on the database you are writing to and expand. Select the Tables folder and highlight the Employees table and right click. One option should be 'Grab Structure'.
I think i messed up that part
|
0

The problem may be spacing.

instead of

+ "WHERE EmployeeID = " + jTextField1.getText();

you should write

+ " WHERE EmployeeID = " + jTextField1.getText();

Comments

0

Remove '' from integer value phone number so that your code looks like this

String sql = "update master.Employees set EmployeeName = '" + jTextField1.getText() + "'" + ",Address = '" + jTextField3.getText() + "'" + ",PhoneNumber = " +
        jTextField4.getText() + " + ",EmailAddress = '" + jTextField5.getText() + "'" + "WHERE EmployeeID = " + jTextField1.getText();
Statement update = myconObj.createStatement();

Hint:

Char='123434'
Integer=12345

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.