Running h2 in MODE=MySQL doesn't support MySQL dumps - java

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;

Related

Creating a table in mysql with java named by the user?

The question is, is there a way to create a new table named by the user from a text field. I know its a huge injection port, but i really need new tables, it will work only offline. I tried
String newtable = jTextField1.getText();
PreparedStatement create = conn.prepareStatement("CREATE TABLE IF NOT EXISTS '"+newtable+"'(ID INTEGER NOT NULL AUTO_INCREMENT, IDapol INTEGER, ΗΜΕΡΟΜΗΝΙΑ DATE, ΕΣΟΔΑ DOUBLE, PRIMARY KEY(ID), CONSTRAINT IDapol FOREIGN KEY(IDapol) REFERENCES apol(IDapol)");
but i get an error saying: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near "1718"(ID INTEGER NOT NULL AUTO_INCREMENT, IDapol, INTEGER, ΗΜΕΡΟΜΗΝΙΑ' at line 1
1718 is the value of my textField1.
Any help i could use? Thanks
As per here: https://dev.mysql.com/doc/refman/5.7/en/identifiers.html , "Identifiers may begin with a digit but unless quoted may not consist solely of digits."
Also, currently your code is wide open for an SQL injection attack.
Table names are never in '. Either use backticks (`) or simply nothing:
"CREATE TABLE IF NOT EXISTS " + newtable + " (...)
Your table's name should also start with a character!
CREATE TABLE IF NOT EXISTS 1718
(ID INTEGER NOT NULL AUTO_INCREMENT,
IDapol INTEGER,
ΗΜΕΡΟΜΗΝΙΑ DATE,
ΕΣΟΔΑ DOUBLE,
PRIMARY KEY(ID),
CONSTRAINT IDapol
FOREIGN KEY(IDapol) REFERENCES apol(IDapol);
give table name in `` codes

create table user("user_id int auto_increment"); not working in derby (embedded database)

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

Creating table dynamically by SQL in Servlet

I am getting error in this: prepared statement.
Prepared Statement:
PreparedStatement pStatement=connection.prepareStatement
("CREATE TABLE `details`.?(`ID` VARCHAR(255) NOT NULL,`Score` VARCHAR(255) NULL);
INSERT INTO `testdetails`.? (`ID`) VALUES (?);");
pStatement.setString(1, "usrname");
pStatement.setString(2, "usrname");
pStatement.setString(3, "001");
pStatement.executeUpdate();
Error details:
Severe: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:
You have an error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near
''usrname'(`ID` VARCHAR(255) NOT NULL,`Score` VARCHAR(255) NULL);INSER' at line 1
How to fix this error?
JDBC parameters aren't usually usable for table and column names - they're only for values.
Unfortunately, I think this is one of those cases where you will need to be build the SQL dynamically. You will want to be very careful about which values you allow, of course.
This error appears because, you use #setString, and the string gets wrapped by ''.
To fix this, you can use one of this snippets:
"CREATE TABLE `details`.#tableName#(`ID` VARCHAR(255) NOT NULL,`Score` VARCHAR(255) NULL); INTO `testdetails`.#tableName# (`ID`) VALUES (?);".replaceAll("#tableName#", "usrname");
or
new StringBuilder("CREATE TABLE `details`.").append("usrname").append("(`ID` VARCHAR(255) NOT NULL,`Score` VARCHAR(255) NULL); INTO `testdetails`.").append("usrname").append("(`ID`) VALUES (?);").toString();
and pass resulted string into #prepareStatement() method.

Execute db statements from file

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?

query runs fine in mysql and php but not in java

I have the following mysql query in PHP and it works fine.
$strUpdate = "INSERT INTO `batchfolder`.`newbatch` (`BatchID` ,`Batch` ,`barcode` ,`PG`)VALUES (NULL , '', '1', '')";
and is also fine when i run it directly in database.
However when I run it in java,
try {
conn = DriverManager.getConnection (url, userName, password);
Statement st = conn.createStatement();
st.execute("INSERT INTO `batchfolder`.`newbatch` (`BatchID` ,`Batch` ,`barcode` ,`PG`)VALUES (NULL , '', '1', '')");
st.close();
}
It gives the following error.
Exception in thread "main" java.sql.SQLException: Field 'Pre' doesn't have a default value
Pre is the the next row in the database and it does not have a default value.
My question is, then how does this query run fine in mysql and php.
P.S BatchID is an int(10) autoincremented value in newbatch.
This is my table structure.
CREATE TABLE IF NOT EXISTS `newbatch` (
`BatchID` int(10) NOT NULL AUTO_INCREMENT,
`Batch` varchar(100) NOT NULL,
`barcode` varchar(5) NOT NULL,
`Ly` varchar(5) NOT NULL DEFAULT '0',
`PG` varchar(5) NOT NULL,
`Pre` varchar(5) NOT NULL,
`Flu` varchar(5) NOT NULL,
`FluID` varchar(100) DEFAULT NULL,
`DateCreated` varchar(100) DEFAULT NULL,
`Comments` varchar(500) DEFAULT NULL,
PRIMARY KEY (`BatchID`),
UNIQUE KEY `FluID` (`FluID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1000160 ;
Are you absolutely sure this actually runs in PHP? You may just not be seeing the error message.
Try this again with display_errors = 'on' and error_reporting(E_ALL | E_STRICT);
This is a SQL error, it doesn't depend on the driver. The driver just displays the message to you.
UPDATE:
It looks like Java is turning off MySQL's strict mode for some reason. The only thing that (to my knowledge) should be able to cause this behavior is MySQL strict mode being off.
If strict mode is not enabled, MySQL sets the column to the implicit
default value for the column data type.
You can check the mode that your server is running on by SELECT ##GLOBAL.sql_mode;. Try that in both Java and PHP. If the results differ then that's your answer.
UPDATE2: Jep!
Looked at your table definition, you have Pre defined as not null, but you didnt specify a value for it in the insert, so it should show an error proper.
You have several database fields that are 'NOT NULL', so you must specify these in your INSERT statement. I couldn't imagine how this would work in PHP either.

Categories