integer on 4 positions on apache derby - java

I want the ID for the client to be on 4 positions like "0007" on apache derby but the following request doesn't work:
create table client(id int (5) not null AUTO_INCREMENT primary key, fname varchar(20) not null, lname varchar(20) not null,phnum int(10) not null, email varchar(60) not null ) ;
it throws this exception:
[Exception, Error code 30 000, SQLState 42X01] Erreur de syntaxe : Encountered "(" at line 1, column 28.
how can I make it happen ?

The Derby int has no width. It is soley an integer.
You may use formatting, to use the id as a 4 digit wide string.
Probably you will use your Derby data later in Java thrua a ORM. It is not recomended to use business data as an object id. Let the id an auto-incrementet value to identify your object. You may add another field (like code) to have the client code stored (char(4) with a unique index to avoid duplicates).
(4. Only preview 10.000 clients, that seems a quiet low limit)

Related

SymmetricDS length for type varchar cannot exceed N

so I'm trying to use symmetricDS for replicating java h2 database to postgres. I'm using the zip file simple configuration. Here is what happen. I followed the getting started guide, download the symmetricds, and try the demo, then I tried with my own configuration with some table in the trigger. But:
If I replicate the table without varchar field in h2 it works perfectly fine.
If I have a table that has varchar field in it, it crash during creating the table.
JdbcSqlTemplate - ERROR: length for type varchar cannot exceed 10485760
Position: 161. Failed to execute: CREATE TABLE "asset"(
"db_id" BIGINT NOT NULL DEFAULT nextval('"asset_db_id_seq"'),
"id" BIGINT NOT NULL,
"account_id" BIGINT NOT NULL,
"name" VARCHAR(2147483647) NOT NULL,
"description" VARCHAR(2147483647),
"quantity" BIGINT NOT NULL,
"decimals" SMALLINT NOT NULL,
"initial_quantity" BIGINT NOT NULL,
"height" INTEGER NOT NULL,
"latest" BOOLEAN DEFAULT 'TRUE' NOT NULL,
PRIMARY KEY ("db_id")
)
indeed a clear error saying the varchar should not exceed 255, but that's how the source database is, is there anyway to force any varchar to TEXT type? or are there any other way around this? or is this a bug in symmetricds has yet to be solved?
Thanks.
I managed to go way around this by creating the table on target database manually. Here is what I did before running bin/sym.
generate query for table I want to create using dbexport by bin/dbexport --engine corp-000 --compatible=postgres --no-data table_a table_b > samples/create_asset_and_trade.sql
modify the flaw in generated query file samples/create_asset_and_trade.sql. in my case it's the length of the varchar.
after fixing that, run the generated query to fill in the target database using dbimport. bin/dbimport --engine store-001 samples/create_asset_and_trade.sql.
running bin/sym should be okay now, it'll detect that the table is already created, and skip the table creation step.
This is not the ideal way, but it should work for now.

JPA serial numbers using a customer ID generation

I get this error when I try to generate serial numbers for ID using a random generator instead of the built-in strategies:
run:
[EL Info]: 2015-03-27 18:22:05.047--ServerSession(1185812646)--EclipseLink, version: Eclipse Persistence Services - 2.5.0.v20130507-3faac2b
[EL Info]: connection: 2015-03-27 18:22:09.295--ServerSession(1185812646)--file:/C:/Users/Sobhie/Desktop/people/build/classes/_peoplePU login successful
[EL Warning]: 2015-03-27 18:22:09.498--ServerSession(1185812646)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: BLOB/TEXT column 'R' used in key specification without a key length
Error Code: 1170
Call: CREATE TABLE PERSON (R LONGBLOB NOT NULL, FNAME VARCHAR(255), ID BIGINT, LNAME VARCHAR(255), X INTEGER, PRIMARY KEY (R))
Query: DataModifyQuery(sql="CREATE TABLE PERSON (R LONGBLOB NOT NULL, FNAME VARCHAR(255), ID BIGINT, LNAME VARCHAR(255), X INTEGER, PRIMARY KEY (R))")
BUILD SUCCESSFUL (total time: 13 seconds)
You current custom primary key strategy result in primary keys mapped as blobs (so probably a serialized object)).
Out of the box, BLOB or TEXT cannot be used as PRIMARY KEY in mysql as
a) mysql has limit on how many characeters can form part of the index (so whole TEXT content is out of the question),
b) there is no size constraint for TEXT (for example TEXT(512))
To use TEXT as Primary Key, the index length must be declared explicit (so mysql will look only at the first X characters).
CREATE TABLE PERSON (R TEXT NOT NULL, FNAME VARCHAR(255), ..., KEY ix_length_r (R(255)))
If your R values are not unique in the first XX characters it will not work.
But the mysql ix limit lenght, comes down to 255 for unicodecharacters and to 765 for latin-1. So for unicode you are basically as good as using VARCHAR(255) as your primary key.
You mentioned random number generator for your primary keys but they are mapped as blobs. I don't believe having keys longer than 255 chars makes any sense (it is a huge space about 2E462, so more than atoms in the universe!!!), so you should limit yourself to having them as simple varchar.
But most likely you wanted to use simple Long, but you messed up with your mappings. Impossible to say without the code though.

Java SQL simple update syntax issue

I have a table named books with bookID, bookName, count , orderCount
i'd like to write an sql query that will update all books.orderCount to books.orderCount+1.
How shall i do that using executeQuery("UPDATE books...."); ?
I'm having troubles with the syntax.
I've tried to search info on the net however most articles are about INSERT or DELETE commands and the only article that was related suggested to retrieve orderCount to Java, update it and then write it back to SQL. if possible i prefer to avoid it as it may cause serious problems (Locks on records are not needed for this task so i can not use them to avoid problems)
this should be pretty straight forward,
UPDATE books
SET orderCount = orderCount + 1
If it's about a primary key:
Also, you can AUTO INCREMENT.
CREATE TABLE Persons
(
P_Id int NOT NULL AUTO_INCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
PRIMARY KEY (P_Id)
)
To let the AUTO_INCREMENT sequence start with another value, use the following SQL statement:
ALTER TABLE Persons AUTO_INCREMENT=100
To insert a new record into the "Persons" table, we will not have to specify a value for the "P_Id" column (a unique value will be added automatically):
INSERT INTO Persons (FirstName,LastName)
VALUES ('Lars','Monsen')

com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect date value: '' for column 'Date_Of_Birth' at row 1

I'm trying to update the personal detail of a user through a java panel.Panel has fields like user_id(autogenerated),name,date of birth.
problem is when i enter nothing to the date of birth field in java panel and then save it. It gives me the above mentioned error.
i tried to verify it by inserting null to the date of birth(Date datatype) field directly using the mysql database.There it gives no error.
Why is it not taking null string when i insert through java panel but is taking when insert directly using mysql.
CREATE TABLE `userpersonaldetail` (
`User_Id` int(10) unsigned NOT NULL auto_increment,
`Name` varchar(45) default NULL,
`Date_Of_Birth` date default NULL,
`Address` varchar(300) default NULL,
`Phone` varchar(20) default NULL,
`Mobile` varchar(20) default NULL,
`Email` varchar(50) default NULL,
PRIMARY KEY (`User_Id`),
CONSTRAINT `FK_userpersonaldetail_1` FOREIGN KEY (`User_Id`) REFERENCES `usermaster` (`User_Id`)
)
And the portion of the code where exception occurs is:
try
{
con=(Connection) DBConnection.getConnection();
pstmt=(PreparedStatement) con.prepareStatement("Update userpersonaldetail set "+
"name=?,date_of_birth=?,address=?,phone=?,mobile=?,email=? where user_id=?");
pstmt.setInt(7,perBean.getUserId());
pstmt.setString(1,perBean.getName());
pstmt.setString(2,perBean.getDateOfBirth());
pstmt.setString(3,perBean.getAddress());
pstmt.setString(4,perBean.getPhone());
pstmt.setString(5,perBean.getMobile());
pstmt.setString(6,perBean.getEmail());
int i=pstmt.executeUpdate();
}
here perBean is the javaBean which retrieves values from the gui.In one of the test case i kept the date_of_birth text box null which is giving error while storing in DB.
My initial guess would be the field has been defined as 'NOT NULL' which means it will force you to enter a value...
If you were to do a mysql dump of the table (or view it in some tool) you'll probably find it defined such as:
`someDT` datetime NOT NULL
I replaced older version of my-sql-connector jar (to be found in lib folder of the server) with the latest. That solved my problem.
Ahh, i see now, you can't insert '' as a date. you will need to pass a date.
If an attribute does not have a value but you have it mentioned in the column list, forcing you to give something there, you need to use
statement.setNull(index, datatype)
to it. Setting to "" is not the same thing as setting to null.

CMP 2.0 bean auto-generated primary key WAS 6.1

is it possible to map bean's key field with identity primary key column in DB2?
Sample table:
CREATE TABLE ADDRESS (
ID INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (
START WITH 1
INCREMENT BY 1
MINVALUE 1
MAXVALUE 2147483647
NO CYCLE
CACHE 20
NO ORDER ),
Line1 VARCHAR(255) NOT NULL,
Line2 VARCHAR(255),
City VARCHAR(255) NOT NULL,
Postcode VARCHAR(6) NOT NULL,
Country VARCHAR(50) NOT NULL,
Latitude DOUBLE,
Longitude DOUBLE
)
AUDIT NONE
DATA CAPTURE NONE
CCSID UNICODE;
ejbCreate methods have been tailored NOT TO set ID field, but it gets initialized with default for integer type - 0 so i'm getting DuplicateKeyException on second and following calls to ejbCreate.
What is the best way to implement IDENTITY behavior? I found many examples for JBoss but nothing for WAS.
It was easy with JPA, but CMP 2.0 is a must at this time
Override method ejbPostCreate. You will be able to retrieve the generated ID from there, and update your model and your code in order to avoid duplicate IDs.
For instance, take a look at http://forums.sun.com/thread.jspa?threadID=699131

Categories

Resources