I'm a newbie in Java, and I'm working on an app for some medical tests. I got a window where the practician enters the demographic and pathological data of the subject. For the pathologies, there's a series of checkboxes for the most frequent pathologies, and a combobox with a textfield to add other ones. My problem with the following piece of code is that the conditions in the prepared statement are not working, and therefore, the program is always trying to add a new column, even when it's existing. Obviously, if the pathology is existing in the combobox, I want a new entry in the database. And if not, I want a new column to be created after its name, and an entry to be created in this new column. I've tried to use Stringbuilder, like in this post : SQL conditional statement ; but it doesn't solved the problem. Nor in using regular statement. I'm using Eclipse with an H2 Database embedded in the app. Does anyone can light my candle ? Thanks.
try{
Statement state5=DatabaseConnection.getInstance().createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
String query5="SELECT * FROM PATHOLOGIES";
ResultSet resultNames=state5.executeQuery(query5);
while (resultNames.next()){
ResultSetMetaData metaPatho=resultNames.getMetaData();
for(int i=1; i<=metaPatho.getColumnCount(); i++){
metaNomsDB=metaPatho.getColumnName(i);
if (metaNomsDB.equals (nomDBFinal)){
PreparedStatement newDataPatho2 = DatabaseConnection.getInstance().prepareStatement("insert into PATHOLOGIES (ID_SUJET, "+nomDBFinal+") "
+ "values (?, ?)");
newDataPatho2.setString(1, result4.getString("ID_SUJET"));
newDataPatho2.setString(2, nomDBFinal);
newDataPatho2.executeUpdate();
newDataPatho2.close();
}
else {
PreparedStatement newDataPatho3 = DatabaseConnection.getInstance().prepareStatement("ALTER TABLE PATHOLOGIES ADD "+nomDBFinal+" varchar(60)");
newDataPatho3.executeUpdate();
newDataPatho3.close();
PreparedStatement newDataPatho4 = DatabaseConnection.getInstance().prepareStatement("insert into PATHOLOGIES (ID_SUJET, "+nomDBFinal+") "
+ "values (?, ?)");
newDataPatho4.setString(1, result4.getString("ID_SUJET"));
newDataPatho4.setString(2, nomDBFinal);
newDataPatho4.executeUpdate();
newDataPatho4.close();
}
}
}
state5.close();
result4.close();
state4.close();
resultNames.close();
} catch(Exception e){
e.printStackTrace();
}
The stacktrace:
org.h2.jdbc.JdbcSQLException: Duplicate column name "MAL_AU_GENOU"; SQL statement:ALTER TABLE PATHOLOGIES ADD MAL_AU_GENOU varchar(60) [42121-176]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:344)
at org.h2.message.DbException.get(DbException.java:178)
at org.h2.message.DbException.get(DbException.java:154)
at org.h2.table.Table.setColumns(Table.java:399)
at org.h2.table.TableBase.<init>(TableBase.java:45)
at org.h2.table.RegularTable.<init>(RegularTable.java:75)
at org.h2.schema.Schema.createTable(Schema.java:585)
at org.h2.command.ddl.AlterTableAlterColumn.cloneTableStructure(AlterTableAlterColumn.java:318)
at org.h2.command.ddl.AlterTableAlterColumn.copyData(AlterTableAlterColumn.java:226)
at org.h2.command.ddl.AlterTableAlterColumn.update(AlterTableAlterColumn.java:158)
at org.h2.command.CommandContainer.update(CommandContainer.java:79)
at org.h2.command.Command.executeUpdate(Command.java:254)
at org.h2.jdbc.JdbcPreparedStatement.executeUpdateInternal(JdbcPreparedStatement.java:158)
at org.h2.jdbc.JdbcPreparedStatement.executeUpdate(JdbcPreparedStatement.java:144)
at com.app.MenuInfoSujet$BoutonMenuEpreuves.actionPerformed(MenuInfoSujet.java:823)