Throwing MySQLSyntaxErrorException regarding syntax of SQL in Struts - mysql

I am making a project on Struts framework.
I am getting a exception from server while inserting data in a table using SQL and I am unable to find the solution.
The error is as following:
javax.servlet.ServletException: 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 ',,,,,,,'yes','yes',,,,'admin')' at line 1
My code which is generating the error is as following:
sql="Insert into field_visit_details (visit_date,block,village,flw_category,
flw_name,flw_contact_no,aware_zinc,
aware_ors,past_zinc,past_ors,qty_avail_zinc_ten,qty_avail_zinc_twe,qty_avail_zinc_syr,
qty_avail_ors,qty_disp_zinc_ten,qty_disp_zinc_twe,qty_disp_zinc_syr,qty_disp_ors,
stockout_zinc,stockout_ors,diar_cases_seen,diar_cases_reff,diar_deaths_less_than_five,
added_by)
values
('"+field_visit_date+"','"+block_row_one+"','"+village_row_one+"','"+flw_category_row_one
+"','"+flw_name_row_one+"','"+flw_contact_no_row_one+"','"+aware_zinc_row_one+"','"+
aware_ors_row_one+"','"+past_zinc_row_one+"','"+past_ors_row_one+"','"+
qty_avail_zinc_ten_row_one+"','"+qty_avail_zinc_twe_row_one+"','"+
qty_avail_zinc_syr_row_one+"','"+qty_avail_ors_row_one+"','"+qty_disp_zinc_ten_row_one
+"','"+qty_disp_zinc_twe_row_one+"','"+qty_disp_zinc_syr_row_one+"','"+
qty_disp_ors_row_one+"','"+stockout_zinc_row_one+"','"+stockout_ors_row_one+
"','"+diar_cases_seen_row_one+"','"+diar_cases_reff_row_one+"','"+diar_deaths_row_one
+"','"+loginid+"')";
System.out.println(sql);
int x=stmt.executeUpdate(sql);
if(x>0)
SUCCESS="admin";
Table Structure is as following:
CREATE TABLE IF NOT EXISTS `field_visit_details` (
`field_visit_id` int(11) NOT NULL auto_increment,
`visit_date` date NOT NULL,
`block` varchar(100) NOT NULL,
`village` varchar(100) NOT NULL,
`flw_category` varchar(45) NOT NULL,
`flw_name` varchar(100) NOT NULL,
`flw_contact_no` varchar(13) NOT NULL,
`aware_zinc` varchar(10) NOT NULL,
`aware_ors` varchar(10) NOT NULL,
`past_zinc` varchar(10) NOT NULL,
`past_ors` varchar(10) NOT NULL,
`qty_avail_zinc_ten` int(11) NOT NULL,
`qty_avail_zinc_twe` int(11) NOT NULL,
`qty_avail_zinc_syr` int(11) NOT NULL,
`qty_avail_ors` int(11) NOT NULL,
`qty_disp_zinc_ten` int(11) NOT NULL,
`qty_disp_zinc_twe` int(11) NOT NULL,
`qty_disp_zinc_syr` int(11) NOT NULL,
`qty_disp_ors` int(11) NOT NULL,
`stockout_zinc` varchar(11) NOT NULL,
`stockout_ors` varchar(11) NOT NULL,
`diar_cases_seen` int(11) NOT NULL,
`diar_cases_reff` int(11) NOT NULL,
`diar_deaths_less_than_five` int(11) NOT NULL,
`added_by` varchar(100) NOT NULL,
PRIMARY KEY (`field_visit_id`) )
ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
The query when I printed in the console was :
Insert into field_visit_details (visit_date,block,village,flw_category,flw_name,
flw_contact_no,aware_zinc,aware_ors,past_zinc,past_ors,qty_avail_zinc_ten,
qty_avail_zinc_twe,qty_avail_zinc_syr,qty_avail_ors,
qty_disp_zinc_ten,qty_disp_zinc_twe,qty_disp_zinc_syr,qty_disp_ors,stockout_zinc,
stockout_ors,diar_cases_seen,diar_cases_reff,diar_deaths_less_than_five,added_by)
values
('2014-07-02','ASDASD','','asha','asd+','99','yes','yes','yes','yes',
'12','12','12','12','12','12','12','12','yes','yes','12','12','12','admin')
But the thing is that the query is inserting the data successfully in the table but still server is showing the above error.
I even used the above printed query directly in phpmyadmin and worked well without any problem.

You have a syntax exception in the SQL statement. The error message you have posted point to it.
So, you need to change SQL statement and fix syntax errors. For this purpose you should rewrite it to use PraparedStatement like in the example JDBC PreparedStatement example – Insert a record.
And use appropriate to the database column types set methods when setting parameters to the prepared statement.

Related

mySQL error 1064 on Forward Engineering

Using mySQL workbench, i created an EER diagram and populated it with appropriate data for a class I am in. This is the sample of the generated code that creates the issue:
DROP TABLE IF EXISTS `bhk13`.`person` ;
SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `bhk13`.`person` (
`per_id` MEDIUMINT GENERATED ALWAYS AS () VIRTUAL,
`per_fname` VARCHAR(15) NOT NULL,
`per_lname` VARCHAR(30) NOT NULL,
`per_city` VARCHAR(45) NOT NULL,
`per_state` CHAR(2) NOT NULL,
`per_street` VARCHAR(45) NOT NULL,
`per_zip` INT NOT NULL,
`per_phone` BIGINT NOT NULL,
`per_email` VARCHAR(100) NOT NULL,
`per_ssn` INT UNSIGNED NOT NULL,
`per_gender` ENUM('m', 'f', 'o') NOT NULL,
`per_dob` DATE NOT NULL,
`per_is_emp` ENUM('y', 'n') NOT NULL,
`per_is_alm` ENUM('y', 'n') NOT NULL,
`per_is_stu` ENUM('y', 'n') NOT NULL,
`per_note` VARCHAR(255) NULL,
PRIMARY KEY (`per_id`))
ENGINE = InnoDB;
SHOW WARNINGS;
when I attempt to forward engineer the database, it generates the following error:
Thread started
Executing SQL script in server
ERROR: Error 1064: 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 ') VIRTUAL,
`per_fname` VARCHAR(15) NOT NULL,
`per_lname` VARCHAR(30) NOT NUL' at line 2
SQL Code:
CREATE TABLE IF NOT EXISTS `bhk13`.`person` (
`per_id` MEDIUMINT GENERATED ALWAYS AS () VIRTUAL,
`per_fname` VARCHAR(15) NOT NULL,
`per_lname` VARCHAR(30) NOT NULL,
`per_city` VARCHAR(45) NOT NULL,
`per_state` CHAR(2) NOT NULL,
`per_street` VARCHAR(45) NOT NULL,
`per_zip` INT NOT NULL,
`per_phone` BIGINT NOT NULL,
`per_email` VARCHAR(100) NOT NULL,
`per_ssn` INT UNSIGNED NOT NULL,
`per_gender` ENUM('m', 'f', 'o') NOT NULL,
`per_dob` DATE NOT NULL,
`per_is_emp` ENUM('y', 'n') NOT NULL,
`per_is_alm` ENUM('y', 'n') NOT NULL,
`per_is_stu` ENUM('y', 'n') NOT NULL,
`per_note` VARCHAR(255) NULL,
PRIMARY KEY (`per_id`))
ENGINE = InnoDB
SQL script execution finished: statements: 9 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
What is causing this issue and how can I fix it? All of the data is consistent

update with concat in where clause mysql

I have this schema:
CREATE TABLE `devolucion_medicamento` (
`id_devolucion_medicamento` int(11) NOT NULL,
`id_devolucion` int(11) NOT NULL,
`id_medicamento` int(11) NOT NULL,
`cantidad` int(11) DEFAULT NULL,
`fec_mov` timestamp NULL DEFAULT NULL,
`activo` tinyint(1) DEFAULT NULL,
`lote` varchar(45) DEFAULT NULL,
`fec_venc` date DEFAULT NULL,
`tamano` varchar(45) NOT NULL,
`inventario` varchar(45) NOT NULL
)
and i have to update the value of "inventario" where id_medicamento,fec_venc,tamano and inventario are equals to some concatenated string value that i get from an app.
I thought on something like this
UPDATE devolucion_medicamento
SET
inventario="AAAAAA",
WHERE concat(id_medicamento,lote,fec_venc,tamano,inventario)="54062018-09-308"
but i don't know if it is possible in mysql.
I'm having this error:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE concat(id_medicamento,lote,fec_venc,tamano,inventario)="54062018-09-308"' at line 4
I'm too new to comment... but it appears there is a comma after inventario="AAAAAA". If so, MySQL is expecting another set expression.

What is wrong with my code for phpmyadmin?

I tried to make a database is phpmyadmin, but I keep getting an error.
1064 - 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 '0) NULL,
Datum Verkocht DATE NULL,
Personeel VARCHAR(30) NULL,
`Min' at line 9
That's for this code
CREATE TABLE `veilingen`.`Veilingen`(
`Nr` INT(3) NOT NULL,
`Artikel` VARCHAR(100) NULL,
`Datum Geveild` DATE NULL,
`Tel. Verkoper` INT(10) NULL,
`Datum Ingeleverd` DATE NULL,
`Kast`
SET
(0) NULL,
`Datum Verkocht` DATE NULL,
`Personeel` VARCHAR(30) NULL,
`Minimum` DECIMAL NULL,
`Koop Nu` DECIMAL(100) NULL,
`Geveild Voor` DECIMAL(100) NULL,
`Betaald` ENUM(0) NULL,
`Cadeaubon` DECIMAL(100) NULL,
`Versturen` VARCHAR(100) NULL,
`Opmerkingen` VARCHAR(100) NULL,
PRIMARY KEY(`Nr`(1))
) ENGINE = MyISAM;
Sorry for the mess, I can't get the code span to work.
Can somebody help me?
Following is the right syntax for create table in php, you can edit this code according to your table name,column name and it's datatype.
CREATE TABLE employee (
empId int(11) NOT NULL AUTO_INCREMENT,
empName varchar(255) NOT NULL,
empDept varchar(255) NOT NULL,
PRIMARY KEY (empId)
)

Im getting an error when creating my database (#1064)

I was trying to create a database using phpmyadmin but I keep on getting error
#1064 - 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 ') NOT NULL,First Name VARCHAR(30) NOT NULL,Last Name VARCHAR(30) NOT' at line 3
Im using Tomcat version 8.0. Below is the query I am Executing
CREATE TABLE `cs485_lab5`.`Orders` (
`Item Number` VARCHAR(20) NOT NULL,
`Price` DOUBLE(10) NOT NULL,
`First Name` VARCHAR(30) NOT NULL,
`Last Name` VARCHAR(30) NOT NULL,
`Shipping Address` VARCHAR(50) NOT NULL,
`Credit Card` VARCHAR(20) NOT NULL,
`CCN` INT(16) NOT NULL,
PRIMARY KEY (`Credit Card`)
) ENGINE = InnoDB;
Don't specify a length for DOUBLE. Also, if you are designing the table, don't put spaces in the column names. Use names that don't need escaping. Having to use escape characters just clutters up queries unnecessarily.
So:
CREATE TABLE Orders (
`ItemNumber` VARCHAR(20) NOT NULL,
`Price` DOUBLE NOT NULL,
`FirstName` VARCHAR(30) NOT NULL,
`LastName` VARCHAR(30) NOT NULL,
`ShippingAddress` VARCHAR(50) NOT NULL,
`CreditCard` VARCHAR(20) NOT NULL,
`CCN` INT(16) NOT NULL,
PRIMARY KEY (`CreditCard`)
) ENGINE = InnoDB;
Here is a SQL Fiddle.

Error trying to create table in MySQL via terminal

I am trying to create a table in my database via terminal.
Here is my syntax:
CREATE TABLE `users` (
PRIMARY KEY(id) NOT NULL AUTO_INCREMENT,
`last_name` VARCHAR NOT NULL,
`first_name` VARCHAR NOT NULL,
`gender` VARCHAR NOT NULL,
`fav_color` VARCHAR NOT NULL,
`birthdate` DATE NOT NULL
);
I am getting this error:
ERROR 1064 (42000): 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 'NOT NULL AUTO_INCREMENT,
`last_name` VARCHAR NOT NULL,
`first_name` VARCHA' at line 2
What am i dong wrong here?
Besides the issues that Jens and Marc pointed out, You have to declare the length of your Varchar fields in order for this statement to work, like so:
CREATE TABLE `test`.`users` (
`id` INT NOT NULL AUTO_INCREMENT,
`last_name` VARCHAR(45) NOT NULL,
`first_name` VARCHAR(45) NOT NULL,
`gender` VARCHAR(45) NOT NULL,
`fav_color` VARCHAR(45) NOT NULL,
`birthdate` DATE NOT NULL,
PRIMARY KEY (`id`));
the syntax of your create statement is wrong:
the correct one is this:
CREATE TABLE `users` (
`id` int NOT NULL AUTO_INCREMENT,
`last_name` VARCHAR(255) NOT NULL,
`first_name` VARCHAR(255) NOT NULL,
`gender` VARCHAR(255) NOT NULL,
`fav_color` VARCHAR(255) NOT NULL,
`birthdate` DATE NOT NULL,
PRIMARY KEY(`id`)
);
For more Information see the offical documentation.
it should be
id int primary key auto_increment not null
You're trying to define a primary key on a field that doesn't exist. Keys cannot be "not null" and definitely cannot be "auto_increment".