尝试使用此代码中的参数执行prepareStatement
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);
}
}
我收到此错误
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
但是,当我输入一个真实的字符串时,它就可以工作
preparedStatement.setString(1, "Hello World");
如何做到这一点?
答案 0 :(得分:0)
数据异常:字符串数据,右截断;表格:TREES栏:TREETYPE
错误消息告诉您Java变量TreeType
中的字符串值长于TreeType
表中定义的列Trees
中字符串的最大允许长度。您要么必须使列变宽,要么使字符串变短。