I want to update datetime and a string in Memo field using UCanAccess. For example - 5/27/2015 System : some string, this is all I have to update in memo field.
What I have tried - I am converting date to a string using following code:
DateFormat df = new SimpleDateFormat("yyyyMMdd");
Date today = Calendar.getInstance().getTime();
String reportDate = df.format(today);
and then using update query of ucanaccess like this:
st.executeUpdate(" update tblCaseInventory set fldContactNotes = " + reportDate + "' System : CAR Report '" + " where fldCaseNumber = " + rs1.getInt("fldCaseNumber"));
but I am getting an error:
Unexpected token : System.
How to achieve this?
You should use a PreparedStatement to run a parameterized query, something like this:
String sql =
"UPDATE tblCaseInventory " +
"SET fldContactNotes=? " +
"WHERE fldCaseNumber=? ";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
DateFormat df = new SimpleDateFormat("yyyyMMdd");
java.util.Date today = Calendar.getInstance().getTime();
String reportDate = df.format(today);
ps.setString(1, reportDate + " System : CAR Report");
ps.setInt(2, rs1.getInt("fldCaseNumber"));
ps.executeUpdate();
}
Related
I have this SQL query
#Query( value = "select tc_users.id as userid,tc_devices.id as deviceid, tc_devices.name, tc_devices.lastupdate as hora_fecha,\n" +
"tc_positions.speed as velocidad, tc_positions.latitude, tc_positions.longitude, tc_positions.devicetime\n" +
"from tc_devices\n" +
"join tc_users on tc_users.id = tc_devices.userid \n" +
"join tc_positions on tc_devices.id= tc_positions.deviceid and \n" +
"tc_positions.devicetime=(select max(devicetime) from tc_positions where tc_positions.deviceid= tc_devices.id) and tc_users.id= ?1 ;", nativeQuery = true)
List<resumen_entity> resu(Integer userid);
This SQL query return this data
result of query in SQL workbench
but when using it with my spring project it returns this json
result of query by springboot
a really need help with this problem
I try with another type of SQL query but spring returns the same result
#Query( value = "select distinct tc_users.id as userid,tc_devices.id as deviceid, " +
"tc_devices.name, tc_devices.lastupdate as hora_fecha, " +
"tc_positions.speed as velocidad, tc_positions.latitude, " +
"tc_positions.longitude, tc_positions.devicetime " +
"from tc_users, tc_devices,tc_positions where tc_users.id =tc_devices.userid " +
"and tc_devices.id= tc_positions.deviceid " +
"and tc_positions.devicetime=(select max(devicetime) " +
"from tc_positions where tc_positions.deviceid = tc_devices.id) and tc_users.id= ?1 ",
nativeQuery = true)
List<resumen_entity> resu(Integer userid);
I have a big problem with Spring Data in #Query.
I have the following query :
SELECT created_at::DATE "date",
count(*)
FROM
absence
WHERE
created_at::DATE between '2018-05-27' AND '2018-05-31'
GROUP BY created_at::DATE;
this query works in postgres without any problem now in spring to use it :
#Query("SELECT created_at\\:\\:DATE \"date\",count(*) FROM absence " +
"WHERE created_at\\:\\:DATE between '?1' AND '?2' " +
"GROUP created_at\\:\\:DATE", nativeQuery = true)
fun getAbsenceCount(beginDate: String,endDate: String): List<AbsenceCount>
This query doesn't work at all, the problem is that spring can't recognize the parather beginDate (?1) & endDate (?2).
I tried a lot of solution from stackoverflow like solution and solution 2 but I can't get rid of this problem.
I don't know if it's intented or it's a bug in spring.
Try to simply use the classic SQL cast function:
#Query(value = "" +
"select " +
" cast(a.created_at as date) as createdAt, " +
" count(*) as quantity " +
"from " +
" absence a " +
"where " +
" cast(a.created_at as date) between ?1 and ?2 " +
"group " +
" cast(a.created_at as date)" +
"", nativeQuery = true)
fun getAbsenceCount(beginDate: String, endDate: String): List<AbsenceCount>
where AbsenceCount is a projection like this (java):
public interface AbsenceCount {
LocalDate getCreatedAt();
Long getQuantity();
}
Hi i have to pass parameter to a report...with a concat-date ....but its showing error...
like this...am having in list box with 2011-2012,2012-2013...here is my oracle query
DATE FOMART IN DATABASE:25-Mar-2012
oracle query
select tran_date,
ROUND (nvl(sum(WALKIN_WITHOUT_CGROUP),0)/ COUNT(*),2)APC
from OUTLET_PAYMODE_REPORT_FACT
Before Open:
var sqld= new String();
yearParam = params["Year"].value;
yearParam = yearParam.split("-");
startYear = yearParam[0];
endYear = yearParam[1];
this.queryText =this.queryText +" where TRAN_DATE between '" + startYear + "01-Mar-' and '" + endYear + "30-Mar-'"+
"group by tran_date"
but am getting error like this.....
org.eclipse.birt.report.data.oda.jdbc.JDBCException: SQL statement does not return a ResultSet object.
SQL error #1:ORA-01861: literal does not match format string
I think, your sql string should be
" where TRAN_DATE between '01-Mar-" + startYear +
"' and '30-Mar-" + endYear + "' " +
"group by tran_date"
I have an xml which i parse to read the values of the nodes and i get these values in to a custom list for ex Validation.
Now after parsing the xml for values when i try to insert the values in to the table in the H2Database i end up getting the syntax error.
Sample xml which i parse
<DmtTask xsi:type="SqlTask">
<TaskSeverity></TaskSeverity>
<TaskCategory></TaskCategory>
<TaskTitle></TaskTitle>
<TaskMessage></TaskMessage>
<TaskCode></TaskCode>
<DSqlGenerated></DSqlGenerated>
<TaskName>VALIDATE_COMPANIES_MISSING_AVAILABILITY_DATE</TaskName>
<SqlQuery>DROP TABLE CLIENT_VALIDATION.COMPANIES_MISSING_AVAILABILITY_DATE IF EXISTS;
CREATE TABLE CLIENT_VALIDATION.COMPANIES_MISSING_AVAILABILITY_DATE AS
(SELECT * FROM CLIENT.COMPANIES WHERE AVAILABILITY_DATE = '');
</SqlQuery>
<ReportError>SELECT 'Missing Availability Date' AS ERROR_DESC,
ORGANIZATION_NAME,AVAILABILITY_DATE,COUNTRY,EMAIL_ADDRESS,INSTANT_MESSENGER_ADDRESS,REVERSE_DEBIT_CREDIT,KEEP_DEBIT_CREDIT_AND_REVERSE_SIGN
FROM CLIENT_VALIDATION.COMPANIES_MISSING_AVAILABILITY_DATE</ReportError>
<MetricSchemaName>DMTSCRIPTS</MetricSchemaName>
</DmtTask>
Dynamic Query which i wrote to update the table from code behind. TaskEditorText is an object of custom collection List
for (int i = 0; i < taskEditorText.Count; i++)
{
//string sqlGenerated = "false";
db.ExecuteNonQuery("INSERT INTO " + Schema + ".DMTScriptTasks(DmtScriptId,TaskSeverity,TaskCategory,TaskTitle,TaskMessage,TaskCode,TaskName,Execute,UpdatedUser,UpdatedTime) VALUES('" + id + "','" + taskEditorText[i].TaskSeverity + "','" + taskEditorText[i].TaskCategory + "','" + taskEditorText[i].TaskTitle + "','" + taskEditorText[i].TaskMessage + "','" + taskEditorText[i].TaskCode + "','" + taskEditorText[i].TaskName + "','" + true + "','" + CreatedUser + "','" + CreatedDate + "');");
}
Sample value which results in the syntax error
DROP TABLE CLIENT_VALIDATION.COMPANIES_INVAL_AVAILABILITY_DATE IF EXISTS;
CREATE TABLE CLIENT_VALIDATION.COMPANIES_INVAL_AVAILABILITY_DATE AS
(SELECT *
FROM CLIENT.COMPANIES
WHERE
AVAILABILITY_DATE != ''
AND
AVAILABILITY_DATE NOT LIKE '____-__-__');
I tried replacing the special characters, wrote regular expression, used Regex.Escape nothing worked for me. I don't have any control on the xml i parse. Even i am new to the H2Database. So if any knows a way to insert values of this type in to the column in the database can you please help me.
Here the database which we are using is H2DataBase ( Java database).
PreparedStatement prep = reader.Connection.prepareStatement("INSERT INTO " + Schema + ".DMTScriptTasks(DmtScriptId,TaskSeverity,TaskCategory,TaskTitle,TaskMessage,TaskCode,TaskName,Execute,UpdatedUser,UpdatedTime,SqlQuery) VALUES (?,?,?,?,?,?,?,?,?,?,?)");
Then loop through the collection
for (int i = 0; i < taskEditorText.Count; i++)
{
prep.setString(1, id);
prep.setString(2, taskEditorText[i].TaskSeverity);
prep.setString(3, taskEditorText[i].TaskCategory);
prep.setString(4, taskEditorText[i].TaskTitle);
prep.setString(5, taskEditorText[i].TaskMessage);
prep.setString(6, taskEditorText[i].TaskCode);
prep.setString(7, taskEditorText[i].TaskName);
prep.setString(8, "true");
prep.setString(9, CreatedUser);
prep.setString(10, CreatedDate);
prep.setString(11, taskEditorText[i].SqlQuery);
//batch insert
prep.addBatch();
reader.Connection.setAutoCommit(false);
prep.executeBatch();
reader.Connection.setAutoCommit(true);
}
Here taskEditorText is a List and the database i am trying to insert values is H2Database.
Then I did batch update of the insert query.
I am getting the missing right paranthesis error. If I remove the comments around iterator.next() statement, its working fine. Unable to figure out whats wrong. There is NO "(" in the data I pass.
String ORACLE_SUM_QUERY = "select item_number, sum(system_quantity) from ITEMS " +
"where sndate = ? and item_id in" +
" (select item_id from ap.system_items where org_id = 4 " +
" and segment1 in ";
......
while (iterator.hasNext()) {
//iterator.next();
String oracleQuery = String.format(ORACLE_SUM_QUERY + "(%s)) GROUP BY item_number", iterator.next());
preparedStat = connection.prepareStatement(oracleQuery);
preparedStat.setDate(1, getSnDate());
The error seems to indicate that the SQL statement you are building up in oracleQuery has an incorrect number of parenthesis. It would probably be helpful to print that SQL statement out before passing it to the prepareStatement call to make debugging easier.
My guess is that the string that is returned by iterator.next() is not what you expect.
Try rewriting your code as
String ORACLE_SUM_QUERY = "select item_number, sum(system_quantity) from ITEMS " +
"where sndate = ? and item_id in" +
" (select item_id from ap.system_items where org_id = 4 " +
" and segment1 in (";
......
while (iterator.hasNext())
{
ORACLE_SUM_QUERY = ORACLE_SUM_QUERY + String.format("%s", iterator.next());
if(iterator.hasNext())
ORACLE_SUM_QUERY = ORACLE_SUM_QUERY + ",";
}
ORACLE_SUM_QUERY = ORACLE_SUM_QUERY + ")) GROUP BY item_number";
preparedStat = connection.prepareStatement(ORACLE_SUM_QUERY);
preparedStat.setDate(1, getSnDate());
This may not get it exactly as I can't test it, but it might get you closer.
Share and enjoy.