Appfuse 3.5 and DB2 - java

I am trying to create a webapp using appfuse. By default appfuse configures the app to work with a MySQL database, however I'd like use a DB2 database. Since Appfuse uses hibernate and Spring this should be a fairly straightforward configuration change but I haven't been able to get it to work. I get the following error on all my SQL calls:
create table role (
id bigint generated by default as identity,
description varchar(64),
name varchar(20),
primary key (id)
);
HHH000389: Unsuccessful: create table role (id bigint generated by default as identity, description varchar(64), name varchar(20), primary key (id))
DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=;;imary key (id));END-OF-STATEMENT, DRIVER=4.19.26
Here is how I have configured hibernate and the jdbc connection:
<jdbc.groupId>com.ibm.db2</jdbc.groupId>
<jdbc.artifactId>db2jcc4</jdbc.artifactId>
<jdbc.version>10.5</jdbc.version>
<jdbc.driverClassName>com.ibm.db2.jcc.DB2Driver</jdbc.driverClassName>
<jdbc.url>jdbc:db2://<IP Address>:<Port>/<DBName>:currentSchema=<schemaName>;</jdbc.url>
<jdbc.username><username></jdbc.username>
<jdbc.password><password></jdbc.password>
<jdbc.validationQuery><![CDATA[SELECT 1 FROM sysibm.sysdummy1;]]></jdbc.validationQuery>
<hibernate.dialect>org.hibernate.dialect.DB2Dialect</hibernate.dialect>
I don't understand where I'm going wrong. I can use the same db2jcc4.jar and connection parameters to connect to the DB through netbeans and copy and paste the sql from above and it executes without error. So I don't believe it's a syntax error as the SQLCODE=104, SQLSTATE42601 indicates. I'm at a loss of what I'm doing wrong. Any help you can give me is greatly appreciated!

I'm not sure if this is the correct answer but it worked: I removed the validation Query.
If anyone knows of a better answer or why this worked please share.
Thank you!

Related

Cannot apply Flyway migration for a PostgreSQL table

In my Java app, I have the following migration file:
-- code omitted for brevity
create table if not exists demo_table
(
id bigint not null,
"company" varchar(50) not null,
"name" varchar(50) not null
);
create unique index if not exists demo_table_uuid_company_key
on demo_table (uuid, "company");
create index if not exists demo_table_name_company_key
on demo_table ("name", "company");
Although I can run the sql part part part or at a time on PostgreSQL query window, when running my Java app, it throws the following error:
"Unable to create index (name, company) on table demo_table: database column 'name' not found. Make sure that you use the correct column name which depends on the naming strategy in use (it may not be the same as the property name in the entity, especially for relational types)"
I tried many thing e.g. removing the related migration row from flyway_schema_history table, delete indexes on demo_table, etc. But still the same error. If I try to remove double quotes ("") from name, it gives checksum error. So, as the name is reserve word, I use with double quotes. How can I fix it?
On the other hand, I am not sure if I should change these parameters on application.yml:
spring:
flyway:
enabled: true
jpa:
hibernate:
ddl-auto: update
Some minor issues with the script:
Missing comma on first column of create.
This column is also called id but the index references uuid.
Resolving this allowed the script to work perfectly for me (with the quotes as you have them)
If you make these changes and get a checksum error, please run flyway repair

Access DB autoincrement doesn't let me open the table

I just managed to connect with a local .accdb Access Database and am now trying to create my tables. I run these two lines with Java and there are no errors during running these SQL-Commands:
Statement st = d.createStatement();
st.executeUpdate("CREATE TABLE suppliers ( [name] TEXT(255), [adress] TEXT(255), [ID] AUTOINCREMENT PRIMARY KEY);");
st.executeUpdate("CREATE TABLE quality ( [name] TEXT(255) );");
st.close();
But now as I want to open the just created tables with Microsoft Access there is a difference between the two generated tables.
I can open the table "quality" normally and insert some values.
Now if I'm trying to open "suppliers" I get this error message:
It is in German and means "The database '' could not been opened. Either the database doesn't get recognized by your application or the file is damaged."
So I tried to remove the "[ID] AUTOINCREMENT PRIMARY KEY" and it works fine without this. So the error must be caused by the "AUTOINCREMENT".
To connect with the database I use ucanacess:
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
c = DriverManager.getConnection("jdbc:ucanaccess://C:/bin/Test.accdb");
Have I done something wrong with my "AUTOINCREMENT"?
I am able to reproduce your issue with UCanAccess 4.0.2. It appears to be related to the [ID] column being specified last. That is, this causes the issue:
sql = "CREATE TABLE suppliers ( [name] TEXT(255), [adress] TEXT(255), [ID] AUTOINCREMENT PRIMARY KEY)";
... but specifying the columns in this order works okay:
sql = "CREATE TABLE suppliers ([ID] AUTOINCREMENT PRIMARY KEY, [name] TEXT(255), [adress] TEXT(255))";
I will pass this information along to the UCanAccess development team.

Table View does not exist

i am using SQuirrel SQL Client Version 3.7 to view my derby database
the username for this connection is umar and password is umar ..
when i run an insert query inside this connection i get the following error
i am using netbeans and it doesn't work there as well my code gives the same error .. what do i do to fix this
here is the statement i use for creating ADMINISTRATORS TABLE
CREATE TABLE ADMINSTRATORS(ADMIN_ID BIGINT NOT NULL GENERATED ALWAYS AS IDENTITY,username VARCHAR(30) NOT NULL,password VARCHAR(16) NOT NULL,CONSTRAINT ADMIN_PK PRIMARY KEY (ADMIN_ID) )
it was a spelling mistake in my sql query
CREATE TABLE ADMINISTRATORS(ADMIN_ID BIGINT NOT NULL GENERATED ALWAYS AS IDENTITY,username VARCHAR(30) NOT NULL,password VARCHAR(16) NOT NULL,CONSTRAINT ADMIN_PK PRIMARY KEY (ADMIN_ID) )
please someone close this question
Maybe you're not pointing to the database you think you are, try issuing this before your insert:
use UMAR
Is your JDBC url having your database name ?
Ex.
private static String jdbcURL= "jdbc:derby://localhost:1527/UMAR";

Insert Returning Query For MySQL in JOOQ

am trying to use the following code to get the auto generated id . My back end is MySQL. Code looks like this
Record record = create.insertInto(CANDIDATE, CANDIDATE.FIRST_NAME,
CANDIDATE.LAST_NAME,CANDIDATE.EXTRACTED_NAME)
.values("Charlotte", "Roche","Charlotte Roche")
.returning(CANDIDATE.ID)
.fetchOne();
System.out.println(record.getValue(CANDIDATE.ID));
I am getting NullPointerException. I took a look at http://www.jooq.org/javadoc/latest/org/jooq/InsertReturningStep.html .
It says
Derby, H2, Ingres, MySQL, SQL Server only allow for retrieving IDENTITY column values as "generated key". If other fields are requested, a second statement is issued. Client code must assure transactional integrity between the two statements.
As per my understanding in Mysql auto_increment works as IDENTITY. Can anybody please throw some light on how to achieve this for MySQL
I have taken a look at this SO Question on a similar topic and tried following
Result<?> record =
create.insertInto(CANDIDATE, CANDIDATE.FIRST_NAME, CANDIDATE.LAST_NAME,CANDIDATE.EXTRACTED_NAME)
.values("Charlotte", "Roche","Charlotte Roche")
.returning(CANDIDATE.ID)
.fetch();
System.out.println(record.size());
Though it inserts record in the backend but it prints the record.size() as zero
I'm know that I'm late for the party.
But I hope I can help someone with similar problem,
Derby, H2, Ingres, MySQL, SQL Server only allow for retrieving IDENTITY column values as "generated key". If other fields are requested, a second statement is issued. Client code must assure transactional integrity between the two statements.
The words "generated key" is the problem.
You can check if your table id is AUTO_INCREMENT or not by using SHOW CREATE TABLE $table_name. I think it is not.
P/s: I'm using MySQL
Just did a test inserting a record and retrieving the generated id from within a Spring service without any problem.
So yes, auto_increment in MySQL works as IDENTITY with jOOQ.
The MySQL table looks like this:
CREATE TABLE persons (
`id` mediumint(9) NOT NULL AUTO_INCREMENT,
`first_name` varchar(64) NOT NULL,
`last_name` varchar(64) NOT NULL,
primary key(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
and the service like this:
public Result<PersonsRecord> insertPerson(String firstName, String lastName) {
Result<PersonsRecord> result =
dsl
.insertInto(
PERSONS,
PERSONS.FIRST_NAME,
PERSONS.LAST_NAME)
.values(
firstName,
lastName)
.returning(PERSONS.ID)
.fetch();
logger.debug("Person ID: " + result.getValue(0, PERSONS.ID));
return result;
}
The generated id is available straight away after executing the insert:
Person ID: 4
Maybe there is a problem with transaction.
Insert might not yet persisted those values in database, so nothing is fetched.
Also I think that IDENTITY in case of MySQL is not made by AUTO_INCREMENT but PRIMARY KEY (...)
https://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html

Eclipse + MySql + Hibernate, a good intro?

I'm looking for a tutorial explaining how to work with these 3 technologies, found this one, but it's working with HyperSql DB (yeah, I edited hibernate.cfg.xml to connect with MySql... but I just received a bunch of errors).
Your table creation script is wrong for the hibernate generator strategy you're currently using. As I said, your primary key should be defined as autoincrement:
CREATE TABLE COURSES (
COURSE_ID int(11) NOT NULL AUTO_INCREMENT,
COURSE_NAME varchar(20) DEFAULT NULL,
PRIMARY KEY (COURSE_ID)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
You should let SchemaExport generate your DDL for you, it will typically prevent such mistakes ;)
You might try setting <generator class="identity">. But native should also be working if you have set the database column to be auto_increment.
Problem solved using "Toad for MySQL" for creating the table, when setting the column to be the primary key, I just cleaned the "Default value" and did set the AutoIncrement property to true.

Categories