我注意到,当我尝试将记录插入“主键”列中时,即使我要为该列输入的值不为null,也会遇到NULL not allowed for column
错误。有谁知道为什么会这样!?下面是我的PreparedStatement.toString()以及堆栈跟踪。非常感谢您的帮助!
PreparedStatement = 'prep0: MERGE INTO results (rank,lastname,firstname,country,time) VALUES (?,?,?,?,?); {1: '1', 2: 'smith', 3: 'john', 4: 'USA', 5: '2:30'}'...
Exception in thread "main" org.h2.jdbc.JdbcSQLException: NULL not allowed for column "FIRSTNAME"; SQL statement:
MERGE INTO results (rank,lastname,firstname,country,time) VALUES (?,?,?,?,?); [23502-193]`
at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
at org.h2.message.DbException.get(DbException.java:179)
at org.h2.message.DbException.get(DbException.java:155)
at org.h2.table.Column.validateConvertUpdateSequence(Column.java:311)
at org.h2.table.Table.validateConvertUpdateSequence(Table.java:784)
at org.h2.command.dml.Merge.merge(Merge.java:157)
at org.h2.command.dml.Merge.update(Merge.java:106)
at org.h2.command.CommandContainer.update(CommandContainer.java:98)
at org.h2.command.Command.executeUpdate(Command.java:258)
at org.h2.jdbc.JdbcPreparedStatement.execute(JdbcPreparedStatement.java:201)
at Table.merge(Table.java:272)
at CSV.writeToDatabase(CSV.java:643)
at CLI.combineData(CLI.java:152)
at CLI.run(CLI.java:48)
at CLI.main(CLI.java:163)`
这是创建查询的方法:
void merge( Map<String,Object> values ) throws SQLException
{
if ( values.isEmpty() )
{
System.out.println("No data in values map...");
return;
}
query = new StringBuilder("MERGE INTO " + name + " (");
List<String> attrs = new ArrayList<>(values.keySet());
for ( int a=0; a < attrs.size(); a++ )
{
query.append( a > 0 ? "," : "" ).append( attrs.get(a).toLowerCase() );
}
query.append(") VALUES (");
for ( int a=0; a < attrs.size(); a++ )
{
query.append( a > 0 ? ",?" : "?" );
}
query.append(");");
PreparedStatement ps = db.conn.prepareStatement(query.toString());
for ( int v=0; v < attrs.size(); v++ )
{
Object value = values.get(attrs.get(v));
if ( value == null )
{
ps.setString(v+1, null);
}
else if ( value instanceof String || value instanceof Date)
{
ps.setString(v+1, value.toString() );
}
else if ( value instanceof Integer )
{
ps.setInt(v+1, (Integer) value);
}
else if ( value instanceof Double )
{
ps.setDouble(v+1, (Double) value);
}
else
{
ps.setString(v+1, null);
}
}
System.out.println("PreparedStatement = '" + ps.toString() + "'...");
ps.execute();
}