"INSERT into FOLDET (FOLDER_ID, FIELD_NAME, OP_VALUE, "
+ "FIELD_VALUE, FOLDER_FIELD_TYPE, DISPLAY_FLAG ) values \n"
+ "( ?, ?, ?, ?, ?, ?) \n" ;
we use the above query to insert values into the table FOLDET
for column FIELD_VALUE the datatype is varchar(32000) , if the string length is greater than 32000 , we want to store the string in new column of CLOB type eg:- say if the new column name is FLD_VAL_EXT of CLOB type added to the table FOLDET
i want a query to add the string to the new column if the value is greater than "32K"
The Oracle JDBC tutorial covers this well: Blob Insert Example
Basically setup the Clob by writing to its character stream before insert.
Related
I am trying to insert some rows using a prepare-bind statement. One of the columns is of type TIMESTAMP. How do I bind the variable for this column with the current epoch time?
I have the following table:
CREATE TABLE orders {
user_id TEXT,
order_time TIMESTAMP,
order_id UUID DEFAULT gen_random_uuid(),
order_details TEXT,
PRIMARY KEY (user_id, order_time DESC, order_id),
FOREIGN KEY (user_id) REFERENCES %s (user_id)
);
I have the following prepare statement code:
String stmt = "INSERT INTO orders " +
"(user_id, order_time, order_details) " +
"VALUES (?, ?, ?);"
PreparedStatement p = connection.prepareStatement(stmt);
Now I am trying to bind the current epoch time System.currentTimeMillis() to variable 2 in this statement. How should I do that?
The corresponding Java type to your SQL's TIMESTAMP is most likely java.sql.Timestamp. You can generate a current timestamp using:
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
So, your prepared statement code might look like:
String stmt = "INSERT INTO orders " +
"(user_id, order_time, order_details) " +
"VALUES (?, ?, ?);"
PreparedStatement p = connection.prepareStatement(stmt);
p.setString(1, "Karthik");
p.setTimestamp(2, new Timestamp(System.currentTimeMillis()));
p.setString(3, "here is some order text");
This is my first post here, if my formatting is not correct/ hard to read, I will change it. Please let me know.
I have been playing with JDBC trying to add basic data to a database, using user input data. The user provides first and last name, email, and a user id is generated using the random function.
The database was created using postgreSQL. I'm trying to add to a table called accounts, which contains the following columns - user_id (integer), first_name (varchar(100)), last_name (varchar(100)), email (varchar(500)).
My program is able to connect to the database successfully, but it's not able to add data to the table.
in the following code, firstName, lastName, and eMail are all strings, while sID is an int.
state = conx.prepareStatement("INSERT INTO accounts VALUES ("+ sID +","+ firstName + "," + lastName + "," + eMail) + ")");
s.executeUpdate();
Normally, I'd hope the data would be added to the table so we can call it a day, but I'm getting an error.
org.postgresql.util.PSQLException: ERROR: column "v" does not exist
Position: 36
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2440)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2183)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:308)
at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:441)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:365)
at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:143)
at org.postgresql.jdbc.PgPreparedStatement.executeUpdate(PgPreparedStatement.java:120)
at Main.main(Main.java:49)
org.postgresql.util.PSQLException: ERROR: column "v" does not exist
Position: 36
Use ? for parameters instead of concatenating their values. Also, you should name the columns in the INSERT statement. For example:
s = conx.prepareStatement(
"INSERT INTO accounts (id, first_name, last_name, email) " +
"VALUES (?, ?, ?, ?)"
);
s.setInt(1, sID);
s.setString(2, firstName);
s.setString(3, lastName);
s.setString(4, email);
int affectedRows = s.executeUpdate();
Here is my code to check to see if a record already exists in the system before entering ther new record to the sql database.
String sql = "INSERT INTO Stock (name, cost_price, selling_price, numberinstock, supplier) VALUES (?, ?, ?, ?,?) "
+ "Select name"
+ " from Stock"
+ "Where not exists (select * from Stock"
+ "where name = "+NameTextField+")";
I am using Java, my sql and a derby database.
What I am trying to do is when a new item is entered into the system, the sql statement will check to see if that items is already in the system.
What is wrong with this sql statement
You cannot do what you want using insert . . . values. So use insert . . . select instead. The code should look like this:
INSERT INTO Stock(name, cost_price, selling_price, numberinstock, supplier)
Select ?, ?, ?, ?, ?
from sysibm.sysdummy1
Where not exists (select * from Stock where name = "+NameTextField+");
However, you should pass the second reference to name as a parameter, just like all the others:
INSERT INTO Stock(name, cost_price, selling_price, numberinstock, supplier)
Select ?, ?, ?, ?, ?
from sysibm.sysdummy1
Where not exists (select 1 from Stock where name = ?);
try using trigger
Create Trigger Modified_Order_Trigger
On Orders
After Update --as per youe need "insert, delete, update"
AS
Insert Into tablename (tables fileds)
SELECT tablefileds
FROM INSERTED
i got a table made in this way
CREATE TABLE WS_NAPACQ00T
( IDE_IDEN varchar2(20) PRIMARY KEY NOT NULL,
CLB_CXML CLOB,
SYS_DATE TIMESTAMP
);
and this java code to put in dt_date current date.
Timestamp dt_date = new Timestamp(System.currentTimeMillis());
String insertXML = "insert into WS_NAPACQ00T (IDE_IDEN, CLB_CXML, SYS_DATE) values ('10', 'test', '"+dt_date+"' ) ";
result = statement.executeQuery(insertXML);
the error is:
"not a valid month"
how can i resolve?
Don't use Statement it can lead to SQLInjection, instead use PreparedStatement as follows.
String insertXML = "insert into WS_NAPACQ00T (IDE_IDEN, CLB_CXML, SYS_DATE) values (?, ?, ?) ";
PreparedStatement statement = connection.prepareStatement(insertXML);
statement.setString(1,"10");
statement.setString(2,"test");
statement.setTimestamp(3, new Timestamp(System.currentTimeMillis()));
result = statement.executeQuery();
Unrelated.
If you want to insert current timestamp you can use CURRENT_TIMESTAMP. SELECT CURRENT_TIMESTAMP from DUAL; will give the current timestamp.
I have a table like this:
create table images (
image_id serial primary key,
user_id int references users(user_id),
date_created timestamp with time zone
);
I then have a tag table for tags that images can have:
create table images_tags (
images_tag_id serial primary key,
image_id int references images(image_id),
tag_id int references tags(tag_id)
);
To get the results I want, I run a query like this:
select image_id,user_id,tag_id from images left join images_tags using(image_id)
where (?=-1 or user_id=?)
and (?=-1 or tag_id in (?, ?, ?, ?)) --have up to 4 tag_ids to search for
order by date_created desc limit 100;
The problem is, I want to limit based on the number of unique image_ids because my output will look like this:
{"images":[
{"image_id":1, "tag_ids":[1, 2, 3]},
....
]}
Notice how I group the tag_ids into an array for output, even though the SQL returns a row for each tag_id and image_id combo.
So, when I say limit 100, I want it to apply to 100 unique image_ids.
Maybe you should put one image on each row? If that works, you can do:
select image_id, user_id, string_agg(cast(tag_id as varchar(2000)), ',') as tags
from images left join
images_tags
using (image_id)
where (?=-1 or user_id=?) and
(?=-1 or tag_id in (?, ?, ?, ?)) --have up to 4 tag_ids to search for
group by image_id, user_id
order by date_created desc
limit 100;
If that doesn't work, then use a CTE:
with cte as (
select image_id, user_id, tag_id,
dense_rank() over (order by date_created desc) as seqnum
from images left join
images_tags
using (image_id)
where (?=-1 or user_id=?) and
(?=-1 or tag_id in (?, ?, ?, ?)) --have up to 4 tag_ids to search for
)
select *
from cte
where seqnum <= 100
order by seqnum;
Select 100 qualifying images first, and then join images_tags.
Use an EXISTS semi-join to satisfy the condition on images_tags and take care to get the parentheses right.
SELECT i.*, t.tag_id
FROM (
SELECT i.image_id, i.user_id
FROM images i
WHERE (? = -1 OR i.user_id = ?)
AND (? = -1 OR EXISTS (
SELECT 1
FROM images_tags t
WHERE t.image_id = i.image_id
AND t.tag_id IN (?, ?, ?, ?)
))
ORDER BY i.date_created DESC
LIMIT 100
) i
LEFT JOIN images_tags t
ON t.image_id = i.image_id
AND (? = -1 OR t.tag_id in (?, ?, ?, ?)) -- repeat condition
This should be faster than a solution with window functions and CTEs.
Test performance with EXPLAIN ANLAYZE. As always run a couple of times to warm up cache.