I don't know why the update() method doesn't work with ORACLE database
deleteQuery = "delete from USBRPF where upper(userid) = upper(?)" ;
String s= "ABC " ;
getJdbcTemplate().update(deleteQuery, s.trim());
There's a row with column USERID having data 'ABC ' (there's some spaces character after 'C' character)
It seems to not find out that row.
However, if I change code to below, it works
deleteQuery = "delete from USBRPF where upper(userid) like upper(?)" ;
String s= "ABC " ;
getJdbcTemplate().update(deleteQuery, s.trim() + "%");
or
deleteQuery = "delete from USBRPF where upper(trim(userid)) = upper(?)" ;
String s= "ABC " ;
getJdbcTemplate().update(deleteQuery, s.trim());
Note: all works with MSSQL database, with data migrated from ORACLE.
I guess there's problem with database setting. Could have someone figure it out? Thanks
MODIFIED:
Column information:
ORACLE
BRANCH CHAR(2 CHAR)
COMPANY CHAR(1 CHAR)
DATIME TIMESTAMP(6)
JOBNM CHAR(10 CHAR)
UNIQUE_NUMBER NUMBER(18,0)
USERID CHAR(10 CHAR)
USRPRF CHAR(10 CHAR)
MSSQL
[UNIQUE_NUMBER] [bigint] IDENTITY(1,1) NOT NULL,
[USERID] [nchar](10) NULL,
[COMPANY] [nchar](1) NULL,
[BRANCH] [nchar](2) NULL,
[USRPRF] [nchar](10) NULL,
[JOBNM] [nchar](10) NULL,
[DATIME] [datetime2](6)> NULL,
CHAR is a fixed length type. So even if your data looks like "ABC" in the database, it's stored as "ABC ". CHAR columns will be padded with spaces up their size.
Therefore on the first example you're comparing "ABC " (as stored in the DB) to "ABC" (as passed from Java after the trim() call). On your second and third example you're working around this.
I would recommend that you use VARCHAR2 since it's more natural and more commonly used. If not possible, you could try padding the value that you pass from Java up to the CHAR size as defined in Oracle.
Related
using sql2o (https://github.com/aaberg/sql2o)
when selecting a VARCHAR column that has trailing spaces (for example "some value ") the return value is "some value"
when selecting from mysql cli the result contains the trailing spaces
cant find any documentation how to prevent this from happening
table:
CREATE TABLE names
(
name VARCHAR(100),
PRIMARY KEY (experiment_key, metric_name)
);
code example:
Sql2o sql2o;
String name = "some name with trailing space ";
try (Connection con = sql2o.open()) {
con.createQuery("INSERT INTO names (name) VALUES(:name)")
.addParameter("name", name)
.executeUpdate();
}
String nameFromDB;
try (Connection con = sql2o.open()) {
nameFromDB = con.createQuery("select name from names")
.executeAndFetchFirst(String.class);
}
if (!nameFromDB.equals(name)){
throw new RuntimeException("where did the trailing spaces go ??? :( ");
}
Think I found your answer in Sql2o.
I believe by using String.class, it is using the StringConverter class to convert your query output into a string. At the very bottom of the StringConverter class is this line:
return val.toString().trim();
Found here
How to escape semicolon in Sql ? I used playframework i tried to insert html code inside "values" but when i try using semicolon it's not working ?
CREATE TABLE "R_EMAIL_TEMPLATE"
(
"ID" uuid NOT NULL,
"WP_ID" uuid NOT NULL,
"CODE" text NOT NULL,
"SUBJECT" text NOT NULL,
"CONTENT" text NOT NULL,
"CREATED_AT" timestamp without time zone,
"CREATED_BY" text,
"UPDATED_AT" timestamp without time zone,
"UPDATED_BY" text,
CONSTRAINT "R_EMAIL_TEMPLATE_pkey" PRIMARY KEY ("ID"),
CONSTRAINT "R_EMAIL_TEMPLATE_WP_pkey" FOREIGN KEY ("WP_ID") REFERENCES "C_WP" ("ID")
);
INSERT INTO "R_EMAIL_TEMPLATE"
VALUES (
'30abd6ec-3496-45ff-be54-7f6f9290ebc4',
'30abd6ec-3496-45ff-be54-7f6f9290ebcf',
'user-activation',
'User Registration',
';',
'2018-05-17 19:02:39.643',
'LOGIN',
null,
null
);
Semicolon does not need to be escaped.
It looks like problem is not about semicolon, but about double quotes you used in second INSERT
String values in SQL must be in single quotes. Double quote is reserved for object names such as schema/tables/columns names etc.
so try
INSERT INTO "R_EMAIL_TEMPLATE"
VALUES (
'30abd6ec-3496-45ff-be54-7f6f9290ebc4',
'30abd6ec-3496-45ff-be54-7f6f9290ebcf',
'user-activation',
'User Registration',
';',
'2018-05-17 19:02:39.643',
'LOGIN',
null,
null
);
instead...
Use \ to set escape character and try following:
INSERT INTO "R_EMAIL_TEMPLATE"
VALUES (
'30abd6ec-3496-45ff-be54-7f6f9290ebc4',
'30abd6ec-3496-45ff-be54-7f6f9290ebcf',
'user-activation',
'User Registration',
'\;',
'2018-05-17 19:02:39.643',
'LOGIN',
null,
null
);
I am trying to concatenate two ID numbers together but add a comma between them to separate from each other. I know the mysql statement to use but I am having difficulty translating this to a string in my java program. I can get it to work without the comma but I can't seem to figure out the syntax for adding the comma.
Here is my attempt:
query = "UPDATE table SET idColumn = concat(idColumn, ','"+idNum+") WHERE word = '"+wordVariable+"' ";
To clarify, I have a word table which contains a word column and an ID number column. As an example, my ID column might contain a single number as the ID but after multiple concatenations it might look like:
2,5,4,7,1
Change your query to:
query = "UPDATE table SET idColumn = concat(idColumn, ',"+idNum+"') WHERE word = '"+wordVariable+"' ";
I Think you have made mistake on using inverted commas.
Or you can add comma after ','.
query = "UPDATE table SET idColumn = concat(idColumn, ',' , '"+idNum+"') WHERE word = '"+wordVariable+"' ";
Change:
query = "UPDATE table
SET idColumn = concat(idColumn, ','"+idNum+")
WHERE word = '"+wordVariable+"' ";
To:
query = "UPDATE table SET idColumn = concat( idColumn, ',', ? ) WHERE word = ? ";
You were missing a , comma after ',' in the concat.
And using an instance of PreparedStatement you can set values for query placeholders.
pst.setInt( 1, idNum );
pst.setString( 2, wordVariable );
And, I think, your data table design is not correct on idColumn. Instead of storing comma separated values, you better use a normalized form of tables and data.
I have the following problem:
I have two tables in one data base which consist of the same columns besides the name of the last column. I want to write data into them using Java.
I want to use the same preparedStatement for both tables, where I check with an if-command whether it is table1 or table2. table2 has amount10 as the name for the last column, table1 has amount20 for it. This number is stored in a variable within my code.
Below you can see a (simplified) example and how I tried to let the column name variable but it doesn't work. Is there any way to fix this without copying the whole statement and manually changing the number variable?
String insertData = "INSERT INTO `database`.`"+table+"`
(`person_id`,`Date`,`amount`+"number") VALUES "+
"(?,?,?) ON DUPLICATE KEY UPDATE " +
"`person_id` = ? , " +
"`Date` = ? , " +
"`amount`+"number" = ? ; ";
PreparedStatement insertDataStmt;
This will not work since variables number and table are not going to be magically injected into your insertData string while you are changing them.
I'd to a method prepareInsertstatement(String table, String number) that would return correct PreparedStatement:
public void prepareInsertStatement(Connection conn, Strint table, String number) {
String insertData = "INSERT INTO `database`.`"+table+"`
(`person_id`,`Date`,`amount+"number"') VALUES "+
"(?,?,?) ON DUPLICATE KEY UPDATE " +
"`person_id` = ? , " +
"`Date` = ? , " +
"`amount+"number"' = ? ; ";
PreparedStatement insertDataStmt = conn.prepareStatement(insertData);
return insertDataStmt;
}
Just remember to close the PreparesStatement when you don't need it any more.
I suppose that reason for that is invalid syntax. When you concatenate string for last column name you use code 'amount' + number. If your number value is 20, than concat result will be
'amount'20 that cause invalid syntax exception. Just move one extra ' after number.
"'amount" + number + "'"
Note: log, or just error that appears during this statement execution would be very useful to find right answer for your question.
String x=jTextField1.getText();
After connecting to the database the query is:
String query="INSERT INTO student(A) VALUES('"+a+"') where date=' " +x+ " ';";
stmt.executeUpdate(query);
*a is a string which has a letter P assigned to it.
The error i am getting is "....check your mysql syntax....corresponding to the date='"+x'"; "
I want to compare the date entered in the textfield to the date in the mysql 'date' column and if it is correct,the 'a' value (which is P) should be written in column A in the same row of the date entered...
Please help...
Thank you...
I see a space after/before the single quote.
Furthermore date is also an SQL keyword, so better not use that as field name. You could write
`date`
Addition
Sorry, I realized that I erred (date cannot be a field queried as we are inserting a new record).
Either you mean:
String query = "INSERT INTO student(A) VALUES('P') WHERE CURRENT_DATE() = '2012-05-09'";
Or date is a field, and you just want to set another field:
String query = "UPDATE student SET A = 'P' WHERE `date` = '2012-05-09'";
Inserting new records into same table
This is not allowed to do immediately, so one has to use a temporary table.
CREATE TEMPORARY TABLE tmp (A VARCHAR(1));
INSERT INTO tmp (A)
SELECT 'P' FROM student WHERE dt = '...';
INSERT INTO student(A)
SELECT A FROM tmp;
DROP TABLE tmp;