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";
Related
I am new in derby library. why I got this error when I use the auto_increment in my query?
here is my java code
this.conn.createStatement().execute(create table user("user_id int auto_increment, PRIMARY KEY(user_id))");
I tried this in mysql server and its works but in derby I got this error
java.sql.SQLSyntaxErrorException: Syntax error: Encountered "auto_increment" at line 1
why I got this error?
Derby does not have auto_increment as a keyword. In derby you need to use identity columns to implement auto increment behaviour
For example
CREATE TABLE students
(
id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
name VARCHAR(24) NOT NULL,
address VARCHAR(1024),
CONSTRAINT primary_key PRIMARY KEY (id)
) ;
Above statement will create Student table with id as auto increment column and primary key as well.
Hope this helps
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!
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
I use embedded Apache derby for my application. I have a SQL script called createdb.sql that creates all tables in a database and populates it with initial data, e.g.:
SET SCHEMA APP;
CREATE TABLE study (
study_id bigint not null GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
name varchar(50) not null,
note varchar(1000) DEFAULT '',
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
deleted boolean DEFAULT false,
UNIQUE(name),
CONSTRAINT primary_key PRIMARY KEY (study_id)
);
INSERT INTO "APP"."STUDY" (NAME) VALUES ('default');
CREATE TABLE img (
img_id bigint not null GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
filename varchar(200) not null,
path varchar(300) not null,
flipped boolean DEFAULT false,
type smallint not null,
note varchar(1000) DEFAULT '',
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (img_id)
);
ALTER TABLE img ADD COLUMN dpix integer DEFAULT -1;
ALTER TABLE img ADD COLUMN dpiy integer DEFAULT -1;
The question is how do I load this file and execute all the statements using java? I'm trying different function but they all don't work. For example,
Statement s = conn.createStatement();
s.execute(sqlStr);
or
Statement s = conn.createStatement();
s.executeUpdate(sqlStr);
where sqlStr is a String variable containing the contents of the createdb.sql file. How do I execute all the SQL commands contained in the script so that I can create all the tables and initialize them? Btw, the SQL script works, as I use it in SQuirreL SQL Client to manualy create and initialize the database. Now I would like to do it from within my application.
The below tutorial give how to run a mysql script(.sql file) . What you have to do is that Change the mysql db connection to derby db and run. It will work.
http://www.mkyong.com/jdbc/how-to-run-a-mysql-script-using-java/
Here is an alternative way to run a MySQL script without using any third party library.
http://coreyhulen.wordpress.com/2010/04/07/run-a-sql-script-for-mysql-using-java/
With Derby, you generally use the 'ij' tool to do this:
http://db.apache.org/derby/docs/10.9/tools/ttoolsij98878.html
If you want to do this from a Java program of your own, rather than from the command line, you'll want to study the 'runscript' feature of ij; see this related question:
How to run sql scripts in order to update a Derby schema from java code?
I'm trying to embed h2 to test my mysql-application (integration-test)
I added com.h2database:h2:1.3.170 via maven and run the following code:
public class InMemoryTest
{
#Test
public void test() throws Exception {
Class.forName("org.h2.Driver");
Connection conn = DriverManager.
getConnection("jdbc:h2:mem:test;MODE=MySQL;IGNORECASE=TRUE;INIT=RUNSCRIPT FROM 'src/test/resources/test.sql'");
}
}
which gives me the following Exception:
Syntax error in SQL statement "
CREATE TABLE IF NOT EXISTS ""usr_avatar"" (
""usr_avatar_id"" INT(11) NOT NULL AUTO_INCREMENT,
""usr_avatar_user_id"" INT(11) NOT NULL,
""usr_avatar_img"" BLOB NOT NULL,
PRIMARY KEY (""usr_avatar_id""),
UNIQUE KEY ""usr_avatar_id_UNIQUE"" (""usr_avatar_id""),
UNIQUE KEY ""usr_avatar_user_id_UNIQUE"" (""usr_avatar_user_id""),
KEY ""usr_user_id"" (""usr_avatar_user_id""),
KEY ""fk_user_id"" (""usr_avatar_user_id"")
) AUTO_INCREMENT[*]=1 ";
Apparently, the "AUTO_INCREMENT" causes this?
Since this is valid MySQL (I exported the dump from my real database using MySQL Workbench), I'm a bit confused since h2 claims to support MySQL?
Here are a few lines from the .sql:
DROP TABLE IF EXISTS `usr_avatar`;
CREATE TABLE IF NOT EXISTS "usr_avatar" (
"usr_avatar_id" int(11) NOT NULL AUTO_INCREMENT,
"usr_avatar_user_id" int(11) NOT NULL,
"usr_avatar_img" blob NOT NULL,
PRIMARY KEY ("usr_avatar_id"),
UNIQUE KEY "usr_avatar_id_UNIQUE" ("usr_avatar_id"),
UNIQUE KEY "usr_avatar_user_id_UNIQUE" ("usr_avatar_user_id"),
KEY "usr_user_id" ("usr_avatar_user_id"),
KEY "fk_user_id" ("usr_avatar_user_id")
) AUTO_INCREMENT=1 ;
DROP TABLE IF EXISTS `usr_restriction`;
CREATE TABLE IF NOT EXISTS "usr_restriction" (
"usr_restriction_id" int(11) NOT NULL AUTO_INCREMENT,
"usr_restriction_user_id" int(11) DEFAULT NULL,
"usr_restriction_ip" varchar(39) DEFAULT NULL,
"usr_restriction_valid_from" date NOT NULL,
"usr_restriction_valid_to" date DEFAULT NULL,
PRIMARY KEY ("usr_restriction_id"),
UNIQUE KEY "usr_restriction_id_UNIQUE" ("usr_restriction_id"),
KEY "user_id" ("usr_restriction_user_id"),
KEY "usr_user_id" ("usr_restriction_user_id")
) AUTO_INCREMENT=1 ;
What are my options? Should I export the dump with a different software and force it to be plain SQL? Which software could do that? Or am I doing something wrong?
The problem is that H2 doesn't support AUTO_INCREMENT=1, which you have specified in the SQL statement. Try removing it. I don't think it's necessary for MySQL either.
The source SQL exported from MySQL has double-quotes surrounding it's literals. The first DROP statement also has a "back-tick" (`). But when H2 is reporting the error, H2 is showing the literals surrounded by double-double quotes. I think this is the problem.
Try a couple of things. First, take the back-tick in the DROP statement and convert it to single quotes. If that doesn't work, convert all of the double-quotes to single-quotes. If that doesn't work, remove all of the quotes.
I think H2 is trying to create tables with the double-quotes as a part of the actual table names/column names and this is causing it to bomb.
H2 doesn't support AUTO_INCREMENT=1.
Use this instead:
ALTER TABLE table_name ALTER COLUMN id RESTART WITH 1;