CREATE LINKED TABLE not working with PreparedStatement - jdbc

I'm trying to run the following PreparedStatement:
final PreparedStatement ps = conn.prepareStatement("CREATE LINKED TABLE IF NOT EXISTS " + linkedName + "(?, ?, ?, ?, 'ROADS', ?)");
But when the debugger runs over the previous line I get the error:
Syntax error in SQL statement "CREATE LINKED TABLE IF NOT EXISTS
ROAD_TABLE_LINKED(?,[*] ?, ?, ?, 'ROADS', ?)"; expected "string";
SQL statement: CREATE LINKED TABLE IF NOT EXISTS
ROAD_TABLE_LINKED(?, ?, ?, ?, 'ROADS', ?) [42001-199]
Is it not possible to used PreparedStatement with CREATE LINKED TABLE?
Edit: It works fine if I use a normal statement and insert the parameters.

Unfortunately, you cannot use parameters (?) in DDL commands in H2. You need to replace them with string literals.
Statement s = connection.createStatement();
s.execute("CREATE LINKED TABLE IF NOT EXISTS \""
+ linkedName.replaceAll("\"", "\"\"")
+ "\"('', '"
+ url.replaceAll("'", "''")
+ "', '"
+ user.replaceAll("'", "''")
+ "', '"
+ password.replaceAll("'", "''")
+ "', '"
+ schema.replaceAll("'", "''")
+ "', '"
+ table.replaceAll("'", "''")
+ "')");

Related

FMDB, Swift 3, executeUpdate - compiler build failed

I'm using FMDB with Swift 3.
Everything worked fine on Swift 2 but after making the Swift upgrade I'm getting:
"Command failed due to signal: Killed: 9"
After investigating the cause of it I've found that doing "executeUpdate" with about 24 arguments in the ArgumentsArray cause the compiler to be very slow and finally return compilation error.
When decreasing the number of arguments in the array to 20, the compiler build is still slow but succeed to finish successfully.
Any idea why/help will be welcome... !
Here is my code:
(Build succeed but uncommenting the 4 lines below will make the compilation build failed. Any other 4 lines will have same result of course)
func insertLocalization(_ localization: Localization) -> Bool {
print ("Insert Localization: \(localization.localization_object_id!)#\(localization.spot_object_id!)#\(localization.language_code!)")
sharedInstance.database!.open()
let isInserted = sharedInstance.database!.executeUpdate(
"INSERT INTO localizations (" +
"localization_object_id, " +
"spot_object_id, " +
"language_code, " +
"current_location_enabled, " +
"spot_title, " +
"spot_desc, " +
"local_assistant_phone, " +
"orientation_360_enabled, " +
"direction_n_title, " +
"direction_n_desc, " +
"direction_ne_title, " +
"direction_ne_desc, " +
"direction_e_title, " +
"direction_e_desc," +
"direction_se_title, " +
"direction_se_desc, " +
"direction_s_title, " +
"direction_s_desc, " +
"direction_sw_title, " +
"direction_sw_desc, " +
"direction_w_title, " +
"direction_w_desc, " +
"direction_nw_title, " +
"direction_nw_desc) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
withArgumentsIn: [
// localization.localization_object_id!,
// localization.spot_object_id!,
// localization.language_code!,
// localization.current_location_enabled!,
localization.spot_title!,
localization.spot_desc!,
localization.local_assistant_phone!,
localization.orientation_360_enabled!,
localization.direction_n_title!,
localization.direction_n_desc!,
localization.direction_ne_title!,
localization.direction_ne_desc!,
localization.direction_e_title!,
localization.direction_e_desc!,
localization.direction_se_title!,
localization.direction_se_desc!,
localization.direction_s_title!,
localization.direction_s_desc!,
localization.direction_sw_title!,
localization.direction_sw_desc!,
localization.direction_w_title!,
localization.direction_w_desc!,
localization.direction_nw_title!,
localization.direction_nw_desc!
])
sharedInstance.database!.close()
return isInserted
}
Thanks!
This code compiles without incident for me in Xcode 8.0 (8A218a). But if this isn't working for you, I'd suggest splitting the line up, for example
let values = [localization.localization_object_id!, ..., localization.direction_nw_desc!]
let isInserted = sharedInstance.database!.executeUpdate(
"INSERT INTO localizations (...) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
withArgumentsIn: values)

PreparedStatement - Missing IN or OUT parameter at index - only if use clearParameters()

I'm using Oracle 10; this is my code
String insertEntityItemQuery = " INSERT INTO My_schema.ENTITYITEM (ENTITYITEMID, ENTITYID, FASCICOLOID, CALLERID, PARENTENTITYITEMID) " +
" VALUES ((My_schema.ENTITYITEMIDSequence.NEXTVAL), ? , ?, ?, ?) " ;
PreparedStatement preparedStatement = conn.prepareStatement(insertEntityItemQuery , new String[]{"ENTITYITEMID"} );
preparedStatement.clearParameters();
preparedStatement.setInt(1, entityId);
preparedStatement.setString(2, fascicoloID);
preparedStatement.setString(3, callerID);
preparedStatement.setInt(4, parentEntityItemId);
preparedStatement.executeQuery();
I get
java.sql.SQLException: Missing IN or OUT parameter at index:: 5
but only if I use clearParameters() method, if I comment it the execution goes fine!!
It seems that clearParameters resets the My_schema.ENTITYITEMIDSequence.NEXTVAL
param, but how it is possible?

Insert timestamp with JdbcTemplate in Oracle database ( ORA-01858 )

I've read a lot of stuff about this error, and still not found the mistake.
I'm using JdbcTemplate to insert a row in some table with some timestamp column
I'm pretty sure the timestamp is the problem, as if delete from the insert it works fine)
My code:
private static final String INSERT_CITAS = "INSERT INTO CITAS ("
+ "idCita, idServicio, " + "fechaCita, "
+ "idEstado, idUsuarioInicial) " + "VALUES (?, ?, ?, ?, ?)";
Object[] params = {
idCita,
citaQuenda.getIdServicio(),
getDateToDBFormat(citaQuenda.getFechaCita()),
ESTADO_INICIAL,
USUARIO_INICIAL };
String queryCitas = INSERT_CITAS;
super.getJdbcTemplate().update(queryCitas, params);
protected String getDateToDBFormat(Date fechaCreacion){
return "TO_TIMESTAMP('" +
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(fechaCreacion)
+ "', 'yyyy-mm-dd hh24:mi:ss')" ;
}
And having the next error:
org.springframework.dao.DataIntegrityViolationException: PreparedStatementCallback; SQL [INSERT INTO citas_55 (idCita, idServicio, fechaCita, idEstado, idUsuarioInicial) VALUES (?, ?, ?, ?, ?)];
ORA-01858: a non-numeric character was found where a numeric was expected
I've tried to execute the sql in some SQL editor having success, so I can't be more confused.
Being my params: [461, 100, TO_TIMESTAMP('2015-01-28 00:00:01', 'yyyy-mm-dd hh24:mi:ss'), 1, 8888] This actually works.
INSERT INTO citas (idCita, idServicio, fechaCita, idEstado, idUsuarioInicial) VALUES (457, 100, TO_TIMESTAMP('2015-01-28 00:00:01', 'yyyy-mm-dd hh24:mi:ss') , 1, 8888);
Any kind of help would be appreciated. Thanks in advance!
Don't convert back and forth between dates/timestamps and Strings.
Just pass a java.sql.Timestamp instance as a parameter:
Object[] params = {
idCita,
citaQuenda.getIdServicio(),
new java.sql.Timestamp(citaQuenda.getFechaCita()),
ESTADO_INICIAL,
USUARIO_INICIAL };
String queryCitas = INSERT_CITAS;
super.getJdbcTemplate().update(queryCitas, params);
I will go out on a limb here, and think I may see the problem. getDateToDBFormat() method is returning a String type, which contains the text, "TO_TIMESTAMP(...)". That is not a date or timestamp parameter. It is a string parameter. You need to do this instead:
Remove the TO_TIMESTAMP stuff from getDateToDBFormat() and have it just return the formatted DATE/TIME value (the format you show is not an oracle timestamp, but a DATE type).
change your insert to:
"INSERT INTO CITAS ... VALUES (?, ?, TO_DATE(?,?) , ?, ?)"
Where the parameters to the TO_DATE call are the return from getDateToDBFormat() and the second parameter is the date format mask. However, can't you just get rid of that mess and bind a Java Date type (or jdbc sql equivalent) directly?
That should work.

Inconsistent datatypes: expected DATE got NUMBER - when inserting into DB

I'm trying to insert a row in a DB table and I keep getting
ORA-00932: inconsistent datatypes: expected DATE got NUMBER
I don't know why this is happening since all dates are SYSDATE
String insertNewAlarmStat =
"insert into alarmes (id_alarm, alarm_key, id_notif, sever, urgency, date_hour_start, date_hour_modif, date_hour_end, " +
"state, state_rec, date_hour_rec, id_user_rec, id_system_rec, " +
"type, cause, " +
"num_events, id_entity_g, type_entity_g, " +
"desc_entity_g, problem, " +
"time_urg_act, max_urg_act, time_end, time_arq, lim, rec_oblig, dn, num_events_ps, id_alarm_o, id_notif_o, text_ad, domain, date_hour_reg) " +
"values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, SYSDATE, SYSDATE, SYSDATE, SYSDATE, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, SYSDATE)";
PreparedStatement prpstmt = null ;
try {
prpstmt = conn.prepareStatement(insertNewAlarmStat);
prpstmt.setInt(1, randomNumberGenerator());
prpstmt.setString(2, UUID.randomUUID().toString());
prpstmt.setString(3, UUID.randomUUID().toString());
prpstmt.setInt(4, randomNumberGenerator());
prpstmt.setInt(5, 8);
prpstmt.setInt(6, 8524);
prpstmt.setString(7, UUID.randomUUID().toString());
prpstmt.setString(8, UUID.randomUUID().toString());
prpstmt.setString(9, UUID.randomUUID().toString());
prpstmt.setString(10, UUID.randomUUID().toString());
prpstmt.setString(11, "KABOOM");
prpstmt.setInt(12, 8);
prpstmt.setInt(13, 43);
prpstmt.setString(14, UUID.randomUUID().toString());
prpstmt.setString(15, UUID.randomUUID().toString());
prpstmt.setString(16, UUID.randomUUID().toString());
prpstmt.setString(17, UUID.randomUUID().toString());
prpstmt.setInt(18, 2);
prpstmt.setInt(19, 224);
prpstmt.setInt(20, 2);
prpstmt.setInt(21, 224);
prpstmt.setInt(22, 2);
prpstmt.setInt(23, 224);
prpstmt.setInt(24, 2);
prpstmt.setString(25, UUID.randomUUID().toString());
prpstmt.setString(26, UUID.randomUUID().toString());
prpstmt.setString(27, UUID.randomUUID().toString());
prpstmt.setInt(28, 2);
prpstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I've tryed this way and I've tryed to remove the SYSDATE's and add some:
prpstmt.setDate(number, getCurrentDate()); //getCurrent date returns sql.Date
However, the error is the same
I've been debugging for some time now and I can't seem to find the problem, what am I missing?
Check the position of your columns compared to the values you are sending for datatype agreement.

how to use order by in sql select query to sort complaint no in ascending order

qry1 = "Select * from ser_complaint_master a,ser_complaint_status b,company_master c
where a.complaint_no=b.complaint_no
and a.allocation_code=c.co_code
and c.co_br_code='" + Session["BRCODE"] + "'
and a.Complaint_Date>='" + Frdat + "' and a.Complaint_Date<='" + Todat + "'
and a.status in ('Completed')
and a.complaint_type in('" + cmptype + "')";
How to use ORDER BY in select query if more than one tables are involved.
Add order by a.complaint_no to the end of the query.

Resources