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.
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 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";
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'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;