I try to build a Insert Query with ? parameters, however my console is showing an error when I try to execute the query because the column names are wrong. However the table has exactly the follwoing column names. Here my code:
String query = "INSERT INTO " + table +" ( ? ) VALUES ( ? ) ;"; PreparedStatement pstmt = null;
ArrayList<String> listWithQuotesFields = new ArrayList<String>();
for(String element : fields){
listWithQuotesFields.add(element);
}
String finalStringFields = StringUtils.join(listWithQuotesFields.stream().toList(),",");
log.error(finalStringFields);
ArrayList<String> listWithQuotesValues = new ArrayList<String>();
for(String element : values){
listWithQuotesValues.add("'"+element+"'");
}
String finalStringValues = StringUtils.join( listWithQuotesValues.stream().toList(),",");
log.error(finalStringValues);
pstmt = getConnection().prepareStatement(query);
pstmt.setString(1, finalStringFields);
pstmt.setString(2, finalStringValues);
log.error("Prepared Statement:" + pstmt);
ResultSet rs = pstmt.executeQuery();
Errors: 2022-05-11 12:11:40.827 [main] ERROR Database.InsertQuery - Username,Email,Password 2022-05-11 12:11:40.829 [main] ERROR Database.InsertQuery - 'testInjection','test','test' Connected Driver name: Microsoft JDBC Driver 8.2 for SQL Server Driver version: 8.2.1.0 Product name: Microsoft SQL Server Product version: 15.00.2080 2022-05-11 12:11:41.886 [main] ERROR Database.InsertQuery - Prepared Statement:SQLServerPreparedStatement:1 com.microsoft.sqlserver.jdbc.SQLServerException: Invalid column name '@P0'. at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:262) at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1632) at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:600) at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:522) at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7225) at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:3053) at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:247) at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:222) at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeQuery(SQLServerPreparedStatement.java:444) at Database.InsertQuery.BuildQuery(InsertQuery.java:116)