I am working with Postgresql database and Java. I am trying to insert into Postgresql database using prepared statement so I am using Batch insert as well. And I am trying to insert 1000 records at a time.
And below is the exception I am getting when I am trying to insert into one of my tables in batches.
java.sql.BatchUpdateException: Batch entry 0 INSERT into data_week (name, address, version, type, size, fax, phone) values('helloooooo', '360 S', '5.0beta4', NULL, '-1', '673', '300') was aborted. Call getNextException to see the cause.
Does anyone know what does thise error means? And also, can I use batch insert for Postgresql database or not?
EDIT: Below is my schema for the table data_week
CREATE TABLE data_week
(
name character varying(256),
address character varying(256),
version character varying(256),
type integer,
size integer,
fax character varying(256),
phone integer,
created_date timestamp without time zone NOT NULL DEFAULT (now() - '7 days'::interval),
updated_date timestamp without time zone NOT NULL DEFAULT now()
)
Related
Java 11. PostgreSQL.
Having following table in db:
TABLE public.account (
id bigserial NOT NULL,
account_id varchar(100) NOT NULL,
display_name varchar(100) NOT NULL,
is_deleted bool NULL DEFAULT false,
);
There are about 1000 rows in this table. In the code I have a static method, which return random string - Helper.getRandomName()
How, using JDBC, in this table (public.account) for all rows replace "display_name" value with value of Helper.getRandomName()?
This is a SQL question. You need to run an update query:
UPDATE public.account set display_name = ?
And provide the new name as the parameter. The absence of a WHERE clause means that all rows will be affected.
If you want to do this for each row individually, then it's harder. You'll want to do a select statement to find all the IDs, and then you can prepare a batch of updates using JDBC, adding a where clause for each ID.
JDBC is just a thin Java wrapper around plain SQL execution.
I am not getting proper result , I tried below query , and getting result some strange decimals
CREATE TABLE "EXAMPLE"
("LOCATION_I" NUMBER NOT NULL ENABLE,
"START_TS" DATE,
"END_TS" DATE,
CONSTRAINT "TABLE1_PK" PRIMARY KEY ("LOCATION_I")
) ;
Insert into EXAMPLE (LOCATION_I,START_TS,END_TS) values (1,to_date('14-11-17','DD-MM-RR'),to_date('15-11-17','DD-MM-RR'));
Insert into EXAMPLE (LOCATION_I,START_TS,END_TS) values (2,to_date('13-11-17','DD-MM-RR'),to_date('14-11-17','DD-MM-RR'));
Insert into EXAMPLE (LOCATION_I,START_TS,END_TS) values (3,to_date('12-11-17','DD-MM-RR'),to_date('13-11-17','DD-MM-RR'));
select LOCATION_I, avg(END_TS - START_TS) from EXAMPLE where END_TS is not null GROUP BY LOCATION_I;
Please suggest solution ..
this is a part of bigger sql . .....................................................................................................................................................................................................................................................................
I can't create the table in DB2 via Eclipse. I've been stuck for a long time and I've searched a lot for the following error:
Error SQLCODE=-204
Below is my code:
CREATE TABLE BaseEntity(
wts Timestamp NOT NULL,
siteID NOT NULL,
oid varchar2(11),
PRIMARY KEY (oid),
AccelerationVector varchar2(8),
DeadReckoningAlgorithm varchar2(8),
Orientation varchar2(8),
WorldLocation varchar2(8),
VelocityVector varchar2(8)
)
com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704, SQLERRMC=NOT, DRIVER=3.63.123
at com.ibm.db2.jcc.am.fd.a(fd.java:679)
at com.ibm.db2.jcc.am.fd.a(fd.java:60)
at com.ibm.db2.jcc.am.fd.a(fd.java:127)
at com.ibm.db2.jcc.am.ho.b(ho.java:2317)
at com.ibm.db2.jcc.am.ho.c(ho.java:2300)
at com.ibm.db2.jcc.t4.cb.l(cb.java:370)
at com.ibm.db2.jcc.t4.cb.a(cb.java:62)
at com.ibm.db2.jcc.t4.q.a(q.java:50)
at com.ibm.db2.jcc.t4.tb.b(tb.java:220)
at com.ibm.db2.jcc.am.io.lc(io.java:3318)
at com.ibm.db2.jcc.am.io.b(io.java:4275)
at com.ibm.db2.jcc.am.io.dc(io.java:759)
at com.ibm.db2.jcc.am.io.executeUpdate(io.java:742)
at testDB.XmlToDBSchema.insertIntoDB(XmlToDBSchema.java:37)
at testDB.XmlToDBSchema.createDBSchma(XmlToDBSchema.java:191)
at testXMLPar.testXML.main(testXML.java:16)
The error -204 refers to an undefined name which can have several reasons. See here for an overview. In your case the statement has several issues:
siteID does not have a type,
primary key is in the middle of the statement, this should be moved to the end
varchar2 can only be used if the database has been enabled for it, else you may get this error messages
To correct the errors you have to rewrite the statement, use data types where needed and to make sure that varchar2 support is enabled (check get db cfg).
When i try to use DBUnit to insert a record into a H2 table i get an Exception caused by:
Caused by: org.h2.jdbc.JdbcSQLException:
Cannot parse "TIMESTAMP" constant "1970-00-01";
SQL statement: insert into PUBLIC.TABLE (COLX, COLY, COLZ) values (?, ?, ?)
Caused by: java.lang.IllegalArgumentException:
1970-0-1 at org.h2.util.DateTimeUtils.parseDateValue(DateTimeUtils.java:276)
None of the values are for the required timestamp column, so dbUnit seems to try to insert a default value which raises the problem.
Note: the 1970-0-1 is not the 1970-00-01 as in the table description (see below).
I am not sure whre i could configure the behaviour. Anyways her's some of my setup which could help identify the error:
Create statement:
create table MYTABLE (
"UUID" binary default random_uuid() not null,
"COL2" varchar(18) not null,
"COL3" varchar(20),
"COL4" timestamp default '1970-00-01',
...
The DataSet XML (as said the three values are not including the timestamp column)
<dataset>
<MYTABLE COLX="text" COLY="1" COLZ="Text"/>
i have also set
dbConfig.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new H2DataTypeFactory());
I was thinking of two possibilities:
1. Is dbUnit trying to parse the value from the column info?
2. Is there a standard default generator for timestamp/date fields in dbUnit?
So any ideas what i need to do to have the correct default value will be inserted?
There is no month 0. January is month 1. So you need to use '1970-01-01' instead of '1970-00-01'.
I want to create a log activity for each user when they login from my apps and insert into MySQL database with the structure like
id_log (int, primary key, auto increment)
username (varchar)
time (timestamp)
The problem is every pc has a different timestamp and I only know to get timestamp on a local machine, is there any way to create a log activity based on a timestamp from a PC that storing database? I ask the other and said to use log4j but I still don't get it.
You can use MySQL's NOW() function, but the TIMESTAMP datatype automatically initialises to the current time by default:
CREATE TABLE logs (
id_log SERIAL,
username VARCHAR(255),
time TIMESTAMP
);
INSERT INTO logs (username) VALUES ('eggyal');
See it on sqlfiddle.