Table Structure:
CREATE TABLE `cs_menuitem` (
`menuitemid` int(11) NOT NULL AUTO_INCREMENT,
`catid` int(11) DEFAULT NULL,
`itemname` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`menuitemid`),
KEY `fi0` (`catid`)
) ENGINE=InnoDB AUTO_INCREMENT=651879 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='InnoDB free: 9216 kB; (catid) REFER cruzstar_v01/cs_menucate';
Inserted UTF-8 chars using PHP
INSERT INTO `cs_menuitem` (`catid`, `itemname`)
VALUES
(97260, 'as “Sautéed Pastrami” , “Piña Colada Virgin” or “Piña Colada”');
Select query:
SELECT itemname FROM cs_menuitem where menuitemid = 651841;
with the command line, and PHP : (correct result)
as “Sautéed Pastrami” , “Piña Colada Virgin” or “Piña Colada”
But with others (MysqlWorkbench, SequelPro, PhpMyAdmin, PhpStorm, Java), getting: (incorrect result)
as “Sautéed Pastrami†, “Piña Colada Virgin†or “Piña Coladaâ€
In my DB every question has a valid questionID (primary Key) and categoryID (foreign Key to category table).
The problem is: in the Result Set for every question both IDs are 0 instead of what's written in the DB. All other arguments are filled out correctly.
private ArrayList<Question> questions = new ArrayList<Question>();
private Connection connie;
private PreparedStatement psShowQuestions;
psShowQuestions= connie.prepareStatement("SELECT * FROM question");
ResultSet rs = psShowQuestions.executeQuery();
while (rs.next()) {
questions.add(new Question(rs.getInt("questionID"), rs.getInt("categoryID"), rs.getString("question"), rs.getString("rightAns"), rs.getString("wrong1"), rs.getString("wrong2"), rs.getString("wrong3"), rs.getString("hint")));
}
Collections.shuffle(questions);
Edit 1
Here is the original code (in the post I changed the variables from German to English):
The creation of my SQL table:
CREATE TABLE `frage` (
`frageID` int(11) NOT NULL,
`kategorieID` int(11) DEFAULT NULL,
`frage` varchar(200) NOT NULL,
`richtig` varchar(200) NOT NULL,
`falsch1` varchar(200) NOT NULL,
`falsch2` varchar(200) NOT NULL,
`falsch3` varchar(200) NOT NULL,
`hinweis` varchar(200) NOT NULL,
`anzFalsch` int(11) DEFAULT NULL,
`anzRichtig` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
private ArrayList<Frage> fragen = new ArrayList<Frage>();
private Connection connie;
private PreparedStatement psGetFragen;
ResultSet rs = psGetFragen.executeQuery();
while (rs.next()) {
fragen.add(new Frage(rs.getInt("frageID"), rs.getInt("kategorieID"), rs.getString("frage"), rs.getString("richtig"), rs.getString("falsch1"), rs.getString("falsch2"), rs.getString("falsch3"), rs.getString("hinweis")));
}
Collections.shuffle(fragen);
Most likely, there is a problem with the constructor. You might have not set questionID and categoryID in the constructor and therefore you are getting the default value of int as 0.
I am trying to export the .war file but at the time of doing clean and build it throws me errors of liquibase. In the IDE run perfectly fine.
I stress that this happened after adding some roles as indicated in this question: Using ROLES in jhipster?
This is the error thrown by the console:
2018-02-27 11:20:40.764 WARN 8156 --- [as-4-Executor-1] i.g.j.c.liquibase.AsyncSpringLiquibase : Starting Liquibase asynchronously, your database might not be ready at startup!
2018-02-27 11:20:43.048 ERROR 8156 --- [as-4-Executor-1] liquibase : classpath:config/liquibase/master.xml: config/liquibase/changelog/00000000000000_initial_schema.xml::00000000000001::jhipster: Change Set config/liquibase/changelog/00000000000000_initial_schema.xml::00000000000001::jhipster failed. Error: Tabla "JHI_USER" ya existe
Table "JHI_USER" already exists; SQL statement:
CREATE TABLE PUBLIC.jhi_user (id BIGINT AUTO_INCREMENT NOT NULL, login VARCHAR(50) NOT NULL, password_hash VARCHAR(60), first_name VARCHAR(50), last_name VARCHAR(50), email VARCHAR(100), image_url VARCHAR(256), activated BOOLEAN NOT NULL, lang_key VARCHAR(6), activation_key VARCHAR(20), reset_key VARCHAR(20), created_by VARCHAR(50) NOT NULL, created_date TIMESTAMP DEFAULT NOW() NOT NULL, reset_date TIMESTAMP, last_modified_by VARCHAR(50), last_modified_date TIMESTAMP, CONSTRAINT PK_JHI_USER PRIMARY KEY (id), CONSTRAINT ux_user_login UNIQUE (login), CONSTRAINT ux_user_email UNIQUE (email)) [42101-196] [Failed SQL: CREATE TABLE PUBLIC.jhi_user (id BIGINT AUTO_INCREMENT NOT NULL, login VARCHAR(50) NOT NULL, password_hash VARCHAR(60), first_name VARCHAR(50), last_name VARCHAR(50), email VARCHAR(100), image_url VARCHAR(256), activated BOOLEAN NOT NULL, lang_key VARCHAR(6), activation_key VARCHAR(20), reset_key VARCHAR(20), created_by VARCHAR(50) NOT NULL, created_date TIMESTAMP DEFAULT NOW() NOT NULL, reset_date TIMESTAMP, last_modified_by VARCHAR(50), last_modified_date TIMESTAMP, CONSTRAINT PK_JHI_USER PRIMARY KEY (id), CONSTRAINT ux_user_login UNIQUE (login), CONSTRAINT ux_user_email UNIQUE (email))]
2018-02-27 11:20:43.110 ERROR 8156 --- [as-4-Executor-1] i.g.j.c.liquibase.AsyncSpringLiquibase : Liquibase could not start correctly, your database is NOT ready: Migration failed for change set config/liquibase/changelog/00000000000000_initial_schema.xml::00000000000001::jhipster:
Reason: liquibase.exception.DatabaseException: Tabla "JHI_USER" ya existe
Table "JHI_USER" already exists; SQL statement:
CREATE TABLE PUBLIC.jhi_user (id BIGINT AUTO_INCREMENT NOT NULL, login VARCHAR(50) NOT NULL, password_hash VARCHAR(60), first_name VARCHAR(50), last_name VARCHAR(50), email VARCHAR(100), image_url VARCHAR(256), activated BOOLEAN NOT NULL, lang_key VARCHAR(6), activation_key VARCHAR(20), reset_key VARCHAR(20), created_by VARCHAR(50) NOT NULL, created_date TIMESTAMP DEFAULT NOW() NOT NULL, reset_date TIMESTAMP, last_modified_by VARCHAR(50), last_modified_date TIMESTAMP, CONSTRAINT PK_JHI_USER PRIMARY KEY (id), CONSTRAINT ux_user_login UNIQUE (login), CONSTRAINT ux_user_email UNIQUE (email)) [42101-196] [Failed SQL: CREATE TABLE PUBLIC.jhi_user (id BIGINT AUTO_INCREMENT NOT NULL, login VARCHAR(50) NOT NULL, password_hash VARCHAR(60), first_name VARCHAR(50), last_name VARCHAR(50), email VARCHAR(100), image_url VARCHAR(256), activated BOOLEAN NOT NULL, lang_key VARCHAR(6), activation_key VARCHAR(20), reset_key VARCHAR(20), created_by VARCHAR(50) NOT NULL, created_date TIMESTAMP DEFAULT NOW() NOT NULL, reset_date TIMESTAMP, last_modified_by VARCHAR(50), last_modified_date TIMESTAMP, CONSTRAINT PK_JHI_USER PRIMARY KEY (id), CONSTRAINT ux_user_login UNIQUE (login), CONSTRAINT ux_user_email UNIQUE (email))]
liquibase.exception.MigrationFailedException: Migration failed for change set config/liquibase/changelog/00000000000000_initial_schema.xml::00000000000001::jhipster:
Reason: liquibase.exception.DatabaseException: Tabla "JHI_USER" ya existe
Table "JHI_USER" already exists; SQL statement:
CREATE TABLE PUBLIC.jhi_user (id BIGINT AUTO_INCREMENT NOT NULL, login VARCHAR(50) NOT NULL, password_hash VARCHAR(60), first_name VARCHAR(50), last_name VARCHAR(50), email VARCHAR(100), image_url VARCHAR(256), activated BOOLEAN NOT NULL, lang_key VARCHAR(6), activation_key VARCHAR(20), reset_key VARCHAR(20), created_by VARCHAR(50) NOT NULL, created_date TIMESTAMP DEFAULT NOW() NOT NULL, reset_date TIMESTAMP, last_modified_by VARCHAR(50), last_modified_date TIMESTAMP, CONSTRAINT PK_JHI_USER PRIMARY KEY (id), CONSTRAINT ux_user_login UNIQUE (login), CONSTRAINT ux_user_email UNIQUE (email)) [42101-196] [Failed SQL: CREATE TABLE PUBLIC.jhi_user (id BIGINT AUTO_INCREMENT NOT NULL, login VARCHAR(50) NOT NULL, password_hash VARCHAR(60), first_name VARCHAR(50), last_name VARCHAR(50), email VARCHAR(100), image_url VARCHAR(256), activated BOOLEAN NOT NULL, lang_key VARCHAR(6), activation_key VARCHAR(20), reset_key VARCHAR(20), created_by VARCHAR(50) NOT NULL, created_date TIMESTAMP DEFAULT NOW() NOT NULL, reset_date TIMESTAMP, last_modified_by VARCHAR(50), last_modified_date TIMESTAMP, CONSTRAINT PK_JHI_USER PRIMARY KEY (id), CONSTRAINT ux_user_login UNIQUE (login), CONSTRAINT ux_user_email UNIQUE (email))]
at liquibase.changelog.ChangeSet.execute(ChangeSet.java:619)
at liquibase.changelog.visitor.UpdateVisitor.visit(UpdateVisitor.java:51)
at liquibase.changelog.ChangeLogIterator.run(ChangeLogIterator.java:79)
at liquibase.Liquibase.update(Liquibase.java:214)
at liquibase.Liquibase.update(Liquibase.java:192)
at liquibase.integration.spring.SpringLiquibase.performUpdate(SpringLiquibase.java:431)
at liquibase.integration.spring.SpringLiquibase.afterPropertiesSet(SpringLiquibase.java:388)
at io.github.jhipster.config.liquibase.AsyncSpringLiquibase.initDb(AsyncSpringLiquibase.java:82)
at io.github.jhipster.config.liquibase.AsyncSpringLiquibase.lambda$afterPropertiesSet$0(AsyncSpringLiquibase.java:64)
at io.github.jhipster.async.ExceptionHandlingAsyncTaskExecutor.lambda$createWrappedRunnable$1(ExceptionHandlingAsyncTaskExecutor.java:66)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: liquibase.exception.DatabaseException: Tabla "JHI_USER" ya existe
Table "JHI_USER" already exists; SQL statement:
CREATE TABLE PUBLIC.jhi_user (id BIGINT AUTO_INCREMENT NOT NULL, login VARCHAR(50) NOT NULL, password_hash VARCHAR(60), first_name VARCHAR(50), last_name VARCHAR(50), email VARCHAR(100), image_url VARCHAR(256), activated BOOLEAN NOT NULL, lang_key VARCHAR(6), activation_key VARCHAR(20), reset_key VARCHAR(20), created_by VARCHAR(50) NOT NULL, created_date TIMESTAMP DEFAULT NOW() NOT NULL, reset_date TIMESTAMP, last_modified_by VARCHAR(50), last_modified_date TIMESTAMP, CONSTRAINT PK_JHI_USER PRIMARY KEY (id), CONSTRAINT ux_user_login UNIQUE (login), CONSTRAINT ux_user_email UNIQUE (email)) [42101-196] [Failed SQL: CREATE TABLE PUBLIC.jhi_user (id BIGINT AUTO_INCREMENT NOT NULL, login VARCHAR(50) NOT NULL, password_hash VARCHAR(60), first_name VARCHAR(50), last_name VARCHAR(50), email VARCHAR(100), image_url VARCHAR(256), activated BOOLEAN NOT NULL, lang_key VARCHAR(6), activation_key VARCHAR(20), reset_key VARCHAR(20), created_by VARCHAR(50) NOT NULL, created_date TIMESTAMP DEFAULT NOW() NOT NULL, reset_date TIMESTAMP, last_modified_by VARCHAR(50), last_modified_date TIMESTAMP, CONSTRAINT PK_JHI_USER PRIMARY KEY (id), CONSTRAINT ux_user_login UNIQUE (login), CONSTRAINT ux_user_email UNIQUE (email))]
at liquibase.executor.jvm.JdbcExecutor$ExecuteStatementCallback.doInStatement(JdbcExecutor.java:309)
at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:55)
at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:113)
at liquibase.database.AbstractJdbcDatabase.execute(AbstractJdbcDatabase.java:1277)
at liquibase.database.AbstractJdbcDatabase.executeStatements(AbstractJdbcDatabase.java:1259)
at liquibase.changelog.ChangeSet.execute(ChangeSet.java:582)
... 12 common frames omitted
Caused by: org.h2.jdbc.JdbcSQLException: Tabla "JHI_USER" ya existe
Table "JHI_USER" already exists; SQL statement:
CREATE TABLE PUBLIC.jhi_user (id BIGINT AUTO_INCREMENT NOT NULL, login VARCHAR(50) NOT NULL, password_hash VARCHAR(60), first_name VARCHAR(50), last_name VARCHAR(50), email VARCHAR(100), image_url VARCHAR(256), activated BOOLEAN NOT NULL, lang_key VARCHAR(6), activation_key VARCHAR(20), reset_key VARCHAR(20), created_by VARCHAR(50) NOT NULL, created_date TIMESTAMP DEFAULT NOW() NOT NULL, reset_date TIMESTAMP, last_modified_by VARCHAR(50), last_modified_date TIMESTAMP, CONSTRAINT PK_JHI_USER PRIMARY KEY (id), CONSTRAINT ux_user_login UNIQUE (login), CONSTRAINT ux_user_email UNIQUE (email)) [42101-196]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
at org.h2.message.DbException.get(DbException.java:179)
at org.h2.message.DbException.get(DbException.java:155)
at org.h2.command.ddl.CreateTable.update(CreateTable.java:117)
at org.h2.command.CommandContainer.update(CommandContainer.java:101)
at org.h2.command.Command.executeUpdate(Command.java:260)
at org.h2.jdbc.JdbcStatement.executeInternal(JdbcStatement.java:192)
at org.h2.jdbc.JdbcStatement.execute(JdbcStatement.java:164)
at com.zaxxer.hikari.pool.ProxyStatement.execute(ProxyStatement.java:92)
at com.zaxxer.hikari.pool.HikariProxyStatement.execute(HikariProxyStatement.java)
at liquibase.executor.jvm.JdbcExecutor$ExecuteStatementCallback.doInStatement(JdbcExecutor.java:307)
... 17 common frames omitted
´
2018-02-27 11:20:49.728 DEBUG 8156 --- [ restartedMain] i.g.j.c.apidoc.SwaggerConfiguration : Starting Swagger
2018-02-27 11:20:49.729 DEBUG 8156 --- [ restartedMain] i.g.j.c.apidoc.SwaggerConfiguration : Started Swagger in 0 ms
2018-02-27 11:20:51.944 INFO 8156 --- [ restartedMain] municipalidad.obras.ObrasPrivadas4App : Started ObrasPrivadas4App in 17.765 seconds (JVM running for 18.308)
2018-02-27 11:20:51.945 INFO 8156 --- [ restartedMain] municipalidad.obras.ObrasPrivadas4App :
----------------------------------------------------------
Application 'obrasPrivadas4' is running! Access URLs:
Local: http://localhost:8080
External: http://192.168.48.28:8080
Profile(s): [swagger, dev]
----------------------------------------------------------
´
I would greatly appreciate your help. Sorry for the bad English.
I'm starting to create a database program for managing engineers, assigning calls to them and such and having this all linked together. However, I have ran into a problem where when I try to update/change a bit of information in the database through the use of my program it instead changed all the other items in the table to what it has just been changed to. For example, if I changed engineer 1's name to 'a', it would overwrite the other entities in the table so engineer 2 would have its name as 'a' now along with the properties of engineer 1.
I have attached the code I have written to update the table along with the SQL code for my database.
I'd appreaciate if someone could help me understand what is wrong here and I can provide other information when requested.
Thanks
private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {
try{
con = Connect.ConnectDB();
String sql = "update engineers set first_name ='" + textFirstName.getText()+ "',last_name='"+ textLastName.getText()+ "',middle_name='" + textMiddleName.getText()+ "',postcode='" + textPostcode.getText() + "',engineer_address='" + textAddress.getText() + "',engineer_dob='" + textDOB.getText() + "',comments='" + textComments.getText()+ "'";
pst = con.prepareStatement(sql);
pst.execute();
JOptionPane.showMessageDialog(this, "Updated","Engineer",JOptionPane.INFORMATION_MESSAGE);
btnUpdate.setEnabled(false);
}catch(HeadlessException | SQLException ex){
JOptionPane.showMessageDialog(this,ex);
}
-- MySQL Script generated by MySQL Workbench
-- Fri Sep 22 12:56:05 2017
-- Model: New Model Version: 1.0
-- MySQL Workbench Forward Engineering
SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`customers`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`customers` (
`customer_id` INT NOT NULL AUTO_INCREMENT,
`customer_name` VARCHAR(45) NOT NULL,
`telephone` VARCHAR(45) NOT NULL,
`postcode` VARCHAR(45) NOT NULL,
`address` VARCHAR(45) NOT NULL,
`city` VARCHAR(45) NOT NULL,
PRIMARY KEY (`customer_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`engineers`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`engineers` (
`engineer_id` INT NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(45) NOT NULL,
`last_name` VARCHAR(45) NOT NULL,
`postcode` VARCHAR(45) NOT NULL,
`active_job` VARCHAR(45) NOT NULL,
`on_holiday` VARCHAR(45) NOT NULL,
`engineer_address` VARCHAR(45) NOT NULL,
`engineer_postcode` VARCHAR(45) NOT NULL,
`comments` VARCHAR(45) NOT NULL,
`middle_name` VARCHAR(45) NOT NULL,
PRIMARY KEY (`engineer_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`machines`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`machines` (
`serial_number` VARCHAR(45) NOT NULL,
`customer_id` INT NOT NULL,
`meter_reading` VARCHAR(45) NOT NULL,
`install_date` VARCHAR(45) NOT NULL,
PRIMARY KEY (`serial_number`),
INDEX `fk_customer_id_idx` (`customer_id` ASC),
CONSTRAINT `fk_customer_id`
FOREIGN KEY (`customer_id`)
REFERENCES `mydb`.`customers` (`customer_id`)
ON DELETE RESTRICT
ON UPDATE CASCADE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`new_call`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`new_call` (
`call_id` INT NOT NULL AUTO_INCREMENT,
`serial_number` VARCHAR(45) NOT NULL,
`customer_id` INT NOT NULL,
`engineer_id` INT NOT NULL,
`call_fault` VARCHAR(45) NOT NULL,
`call_type` VARCHAR(45) NOT NULL,
`date_recieved` VARCHAR(45) NOT NULL,
`start_date` VARCHAR(45) NOT NULL,
`engineer_dob` VARCHAR(45) NOT NULL,
PRIMARY KEY (`call_id`),
INDEX `fk_serial_number_idx` (`serial_number` ASC),
INDEX `fk_engineer_id_idx` (`engineer_id` ASC),
INDEX `fk1_customer_id_idx` (`customer_id` ASC),
CONSTRAINT `fk_serial_number`
FOREIGN KEY (`serial_number`)
REFERENCES `mydb`.`machines` (`serial_number`)
ON DELETE RESTRICT
ON UPDATE CASCADE,
CONSTRAINT `fk_engineer_id`
FOREIGN KEY (`engineer_id`)
REFERENCES `mydb`.`engineers` (`engineer_id`)
ON DELETE RESTRICT
ON UPDATE CASCADE,
CONSTRAINT `fk1_customer_id`
FOREIGN KEY (`customer_id`)
REFERENCES `mydb`.`customers` (`customer_id`)
ON DELETE RESTRICT
ON UPDATE CASCADE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`users`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`users` (
`username` VARCHAR(45) NOT NULL,
`password` VARCHAR(45) NOT NULL)
ENGINE = InnoDB;
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;
first of all use PreparedStatement.
if you want to update a specific engineer you should identify the data with the sql where section.
in your case that could be:
PreparedStatement statement = con.prepareStatement("update engineers set first_name =? " +
",last_name=?, middle_name=?" +
",postcode=?,engineer_address=?" +
",engineer_dob=?" +
",comments=? " +
"where engineer_id=?"); // <--- WHERE SECTION
statement.setString(1, "Chuck");
statement.setString(2, "Norris");
// and so on and so on...
statement.setInteger(8, idOfEngineer);
statement.executeUpdate();
CREATE TABLE IF NOT EXISTS Account (
`userId` INT NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(45) NOT NULL,
`last_name` VARCHAR(45) NOT NULL,
`email` VARCHAR(45) NULL,
`ICQ` INT NULL,
`home_address` VARCHAR(45) NULL,
`work_address` VARCHAR(45) NULL,
`skype` VARCHAR(45) NULL,
`additional_info` VARCHAR(450) NULL,
PRIMARY KEY (`userId`))
UNIQUE INDEX `userID_UNIQUE` (`userId` ASC)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Account';
how to convert this line: UNIQUE INDEXuserID_UNIQUE(userIdASC) from mySQL syntax to H2 syntax ?
From the site h2database.com :
ALTER TABLE Account ADD CONSTRAINT userID_UNIQUE UNIQUE(userId);