Shaggy Lid
Shaggy Lid

Reputation: 55

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

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

Upvotes: 1

Views: 2085

Answers (6)

Muhammad Fayaz
Muhammad Fayaz

Reputation: 1

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

Upvotes: 0

Swati
Swati

Reputation: 28522

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();

      }

Upvotes: 2

minus
minus

Reputation: 2786

The problem may be spacing.

instead of

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

you should write

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

Upvotes: 0

rickskeggs
rickskeggs

Reputation: 24

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

Upvotes: 0

jeffrey
jeffrey

Reputation: 31

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

Upvotes: 0

Angabo
Angabo

Reputation: 129

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 ());

Upvotes: 0

Related Questions