Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"data exception: string data, right truncation" on INSERT with prepared statement

When attempting to execute a preparedStatement with parameters in this code

public void Insert(String TreeType, String LastWatered, String PlantDate) {
    try {
        String G = "INSERT INTO Trees (TreeType, LastWatered, PlantDate) VALUES 
(?, ?, ?)";
        PreparedStatement preparedStatement = conn.prepareStatement(G);
        preparedStatement.setString(1, TreeType);
        preparedStatement.setString(2, LastWatered);
        preparedStatement.setString(3, PlantDate);
        preparedStatement.executeUpdate();
    } catch (SQLException ex) {
        Logger.getLogger(TreeArr.class.getName()).log(Level.SEVERE, null, ex);
    }

}

I get this error

    Sep 09, 2018 2:15:51 AM pat.ConnectDb Insert
    SEVERE: null
    net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::4.0.4 data exception: 
    string data, right truncation;  table: TREES column: TREETYPE
    at 
net.ucanaccess.jdbc.UcanaccessPreparedStatement.executeUpdate(UcanaccessPreparedStatement.java:269)
    at pat.ConnectDb.Insert(ConnectDb.java:41)
    at pat.AddTreeGUI.GoButton1ActionPerformed(AddTreeGUI.java:218)
    at pat.AddTreeGUI.access$100(AddTreeGUI.java:13)
    at pat.AddTreeGUI$2.actionPerformed(AddTreeGUI.java:67)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
     ...
Caused by: org.hsqldb.HsqlException: data exception: string data, right truncation;  table: TREES column: TREETYPE
    at org.hsqldb.error.Error.error(Unknown Source)
    at org.hsqldb.Table.enforceTypeLimits(Unknown Source)
    at org.hsqldb.Table.insertSingleRow(Unknown Source)
    at org.hsqldb.StatementDML.insertSingleRow(Unknown Source)
    at org.hsqldb.StatementInsert.getResult(Unknown Source)
    at org.hsqldb.StatementDMQL.execute(Unknown Source)
    at org.hsqldb.Session.executeCompiledStatement(Unknown Source)
    at org.hsqldb.Session.execute(Unknown Source)
    ... 46 more
Caused by: org.hsqldb.HsqlException: data exception: string data, right truncation
    at org.hsqldb.error.Error.error(Unknown Source)
    at org.hsqldb.error.Error.error(Unknown Source)
    at org.hsqldb.types.CharacterType.convertToTypeLimits(Unknown Source)
    ... 53 more

however, it works when I input a real string eg

preparedStatement.setString(1, "Hello World");

How can this be done be acheved?

like image 875
Thomas Theron Avatar asked Aug 04 '26 11:08

Thomas Theron


1 Answers

data exception: string data, right truncation; table: TREES column: TREETYPE

The error message is telling you that the string value in the Java variable TreeType is longer than the maximum allowable length of a string in the column TreeType as defined in the Trees table. You'll either have to make the column wider or make your string shorter.

like image 57
Gord Thompson Avatar answered Aug 07 '26 01:08

Gord Thompson