Trying to create CRUD application using jdbc and mysql. I have Person.class and two tables in Database:
class Person {
String name;
String surname;
List<String> phones;
}
Tables:
`phone`
(
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`owner` INT UNSIGNED NOT NULL,
`number` VARCHAR(50) NOT NULL,
CONSTRAINT `PK_phone` PRIMARY KEY (`id`)
)
`person`
(
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`surname` VARCHAR(150) NOT NULL,
`name` VARCHAR(150) NOT NULL,
CONSTRAINT `PK_phonebook` PRIMARY KEY (`id`)
)
ALTER TABLE `phone`
ADD CONSTRAINT `FK_phone_person`
FOREIGN KEY (`owner`) REFERENCES `person` (`id`) ON DELETE Cascade ON UPDATE Cascade
;
SET FOREIGN_KEY_CHECKS=1 ;
How i can add person with field List phones to database using Servlets???
For example: Harry Smith +37521987902
+56727172713
+45679012214
The idea is simple, you can use this way
Insert your person
get the id of that person
insert the list of phone
You can use it like this :
try {
Class.forName(driver);
Connection connection = DriverManager.getConnection(DB_URL, DB_username, DB_password);
String query = "INSERT INTO person (surname, name) values(?, ?)";
PreparedStatement preparedStatement = connection.prepareStatement(query);
preparedStatement.setString(1, "hello");
preparedStatement.setString(2, "jdbc");
int affectedRows = preparedStatement.executeUpdate();
long id = 0;
if (affectedRows > 0) {
ResultSet generatedKeys = preparedStatement.getGeneratedKeys();
if (generatedKeys.next()) {
id = generatedKeys.getLong(1);
} else {
throw new SQLException("Creating user failed, no ID obtained.");
}
}
connection.setAutoCommit(false);
PreparedStatement ps = connection.prepareStatement("INSERT INTO phone (owner, number) values(?, ?)");
for (String phone : listePhone) {
preparedStatement.setLong(1, id);
preparedStatement.setString(2, phone);
ps.addBatch();
}
ps.executeBatch();
connection.commit();
}
You can learn how to execute multiple statement in one shot, using statement-batching
Related
When i try to get an element by a few arguments in my Servlet, its values returns NULL. What can I do?
Class DAO:
public Premio getByDataLoteriaHorario(String date, int idLoteria, int idHorario) throws SQLException, ClassNotFoundException, ParseException {
Connection connection = ConnectionFactory.getConnection();
ResultSet rs;
Premio premio = new Premio();
String sqlString = "select * from Premio where dataPremio = ? AND idLoteria = ? AND idHorario = ?";
DateFormat df = new SimpleDateFormat("YYYY/MM/DD");
java.sql.Date data = new java.sql.Date(df.parse(date).getTime());
PreparedStatement preparedStatement = (PreparedStatement) connection.prepareStatement(sqlString);
preparedStatement.setDate(1, data);
preparedStatement.setInt(2, idLoteria);
preparedStatement.setInt(3, idHorario);
rs = preparedStatement.executeQuery();
while(rs.next()) {
premio.setIdPremio(rs.getInt("idPremio"));
premio.setIdLoteria(rs.getInt("idLoteria"));
premio.setIdHorario(rs.getInt("idHorario"));
premio.setIdGrupo(rs.getInt("idGrupo"));
premio.setPremio(rs.getString("premio"));
premio.setDataPremio(rs.getString("dataPremio"));
}
preparedStatement.close();
rs.close();
return premio;
}
The MySQL Table:
CREATE TABLE Premio (
idPremio INT NOT NULL AUTO_INCREMENT,
idLoteria INT,
idHorario INT,
idGrupo INT,
premio1 VARCHAR(4),
premio2 VARCHAR(4),
premio3 VARCHAR(4),
premio4 VARCHAR(4),
premio5 VARCHAR(4),
premioSoma VARCHAR(5),
premio7 VARCHAR(4),
dataPremio DATE,
FOREIGN KEY (idLoteria) REFERENCES Loteria(idLoteria),
FOREIGN KEY (idHorario) REFERENCES Horario(idHorario),
FOREIGN KEY (idGrupo) REFERENCES Bicho(idGrupo),
PRIMARY KEY (idPremio)
);
I think the error is when you call the database to award and dataPremio you send them in the rs.getString but I do not see them in your table I remain attentive to an answer
I change all the Date types to String and it works! ¯\_(ツ)_/¯
I want to insert two rows into master and detail table.
Master and detail table with autoincremented id.
CREATE TABLE MI_User (
id_user NUMBER(11) NOT NULL,
age NUMBER(3),
name_user varchar2(128),
constraint USER_PK PRIMARY KEY (id_user));
CREATE TABLE Friends (
id_friend NUMBER(11) not null,
name VARCHAR2(64),
id_user NUMBER(11) NOT NULL,
constraint FRIEND_PK PRIMARY KEY (id_friend)
);
Model classes are:
public class User {
private String id;
private Integer age;
private String name;
private ArrayList<Friend> friends;
}
public class Friend {
private Long id_user;
private String name;
}
There is example from Hibernate:
tx = session.beginTransaction();
User user = new User(name, age);
employeeID = (Integer) session.save(employee);
tx.commit();
I try to insert with JDBS:
conn = DriverManager
.getConnection("jdbc:oracle:thin:#localhost:1521:xe", "hr", "hr");
ps = conn
.prepareStatement("INSERT INTO MI_USER (BALANCE, AGE, NAME_USER, GENDER, COMPANY, EMAIL, ADDRESS)\n" +
" VALUES (?, ?, ?, ?, ?, ?, ?)");
ps.setDouble(1, user.getDoubleBallans());
ps.setInt(2, user.getAge());
ps.setString(3, user.getName());
ps.executeUpdate();
How to get id_user and insert row into detail table?
I think you can use ps.getGeneratedKeys() method and send Statement.RETURN_GENERATED_KEYS as second parameter in conn.prepareStatement() method.
So I am making a registration page and when I enter all the fields and click signup to submit, enterNewUser(,,,) is called and the fields userId, username,password and role are inserted into the table User. I confirm this by running select * from user; into MYSQL workbench.
Then enterUsername(,,,) is called and I get this error:
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 '(3,'Barry','Allen')' at line 1
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
'(3,'Barry','Allen')' at line 1
public static int enterNewUser(String username,String password, String role){
//int userId = -1;
int ID = 0;
//int ID=-1;
try{
if(checkUserNameAvailable(username)==true){
Class.forName("com.mysql.jdbc.Driver");
cn = DriverManager.getConnection("jdbc:mysql://localhost/log", "root", "root");
String q0 = "Select userId from user ORDER BY userId DESC LIMIT 1"; //get ID of last
Statement st = cn.createStatement();
ResultSet rs = st.executeQuery(q0);
if(rs.next()){
ID = rs.getInt("userId");
ID++;
}
else
ID=1; // Empty Table, so start with ID 1
rs.close();
st.close();
String q1="insert into user values(?,?,?)";
PreparedStatement ps = cn.prepareStatement(q1);
//ps.setInt(1,ID);
ps.setString(1,username);
ps.setString(2,password);
ps.setString(3,role);
ps.executeUpdate();
ps.close();
}
}catch(Exception e){
System.err.println(e.getMessage());
e.printStackTrace();
}
DB_close();
//if(userId!=-1)
// return userId;
return -1;
}
public static boolean enterUsername(int userId, String firstname, String lastname){
try{
Class.forName("com.mysql.jdbc.Driver");
cn = DriverManager.getConnection("jdbc:mysql://localhost/log", "root", "root");
//String q1="INSERT INTO user_profile values(?,?,?)";
String q1 = "INSERT into user_profile (userId, firstname, lastname) VALUES (?,?,?)";
PreparedStatement ps = cn.prepareStatement(q1);
ps.setInt(1, userId);
ps.setString (1, firstname);
ps.setString (2, lastname);
ps.executeUpdate();
ps.close();
return true;
}catch(Exception e){
System.err.println(e.getMessage());
e.printStackTrace();
}
DB_close();
return false;
}
Here is my database structure.
Edit: found the issue, database was not structured properly.
CREATE TABLE user ( userId int(3) NOT NULL AUTO_INCREMENT,
username varchar(20) DEFAULT NULL, password varchar(20) DEFAULT
NULL, role varchar(20) DEFAULT NULL, PRIMARY KEY (userId),
UNIQUE KEY username (username) );
CREATE TABLE user_profile ( userId int(3) NOT NULL DEFAULT '0',
firstName varchar(20) DEFAULT NULL, lastName varchar(20) DEFAULT
NULL, PRIMARY KEY (userId), CONSTRAINT FK FOREIGN KEY
(userId) REFERENCES user (userId) );
Shouldn't following section in method enterUsername
ps.setInt(1, userId);
ps.setString (1, firstname);
ps.setString (2, lastname);
be like this
ps.setInt(1, userId);
ps.setString (2, firstname);
ps.setString (3, lastname);
I don't see the reason for the error message that you posted.
But I see some other things that look like a problem:
ps.setInt(1, userId);
ps.setString (1, firstname);
ps.setString (2, lastname);
The indexes are wrong: instead of 1, 1, 2, it should be 1, 2, 3.
(Frankly, I don't see how the code could possibly work as posted.)
Btw, this also looks wrong in the other method:
insert into user values(?,?,?)
As the table has more than 3 columns, you need to specify their names,
like you did in enterUsername.
Use
String q1 = "INSERT into user_profile (firstname, lastname) VALUES (?,?)";
because your first field is auto increment..So it automatically increment values while inserting values.
I recommended this way,
Delete your current table and create a new one like this
id-->int(30) (AUTO INCREMENT) NOTNULL //Dont need to take care of this field
USER_ID-->int(30) NOT NULL //you should create your own ID and increment it before adding a new person
username-->varchar(100)
password-->varchar(100)
role-->varchar(100)
and usually, call userId exactly same like your code,
String q0 = "Select userId from user ORDER BY USER_ID DESC LIMIT 1"; //get ID of last
Statement st = cn.createStatement();
ResultSet rs = st.executeQuery(q0);
if(rs.next()){
ID = rs.getInt("USER_ID ");
ID++;
}
I am having problems adding data to my database when I am adding "ALTER TABLE" to my tables. I am getting the information from a web page I am working on and is written to a java method that transfers the information to my Database. Everything works fine if I don't use the two ALTER TABLE sentences, but as soon they are added the information will no longer go to my Database. What am I doing wrong? The CREATE TABLE and Java method is listed below. Hope someone can help me!
CREATE TABLE role(
username VARCHAR(15) NOT NULL,
password VARCHAR(15) NOT NULL,
role VARCHAR(6) NOT NULL,
CONSTRAINT username_pk PRIMARY KEY (username)
);
CREATE TABLE customer(
orgnumber INTEGER NOT NULL,
companyname VARCHAR(20) NOT NULL,
contactperson VARCHAR(20),
streetname VARCHAR(30) NOT NULL,
zipcode INTEGER NOT NULL,
city VARCHAR(15) NOT NULL,
phone CHAR(12),
email VARCHAR(30) NOT NULL,
username VARCHAR(15),
CONSTRAINT orgnumber_pk PRIMARY KEY (orgnumber)
);
CREATE TABLE place(
zipcode INTEGER NOT NULL,
city VARCHAR(15),
streetname VARCHAR(30),
CONSTRAINT place_pk PRIMARY KEY (zipcode)
);
ALTER TABLE customer
ADD CONSTRAINT role_fk1 FOREIGN KEY (username)
REFERENCES role;
ALTER TABLE customer
ADD CONSTRAINT place_fk1 FOREIGN KEY (zipcode)
REFERENCES place;
Java method:
public boolean regNewRegister(RegBean newRegister) {
PreparedStatement sqlnewRegister = null;
PreparedStatement sqlnewRole = null;
PreparedStatement sqlnewPlace = null;
String knd = "Customer";
OpenConnection();
boolean ok = false;
try {
/*
* A transaction is started, uses lock.
*/
if (connection == null) {
System.out.println("Went well");
}
connection.setAutoCommit(false);
sqlnewRegister = connection.prepareStatement("insert into customer (ORGNUMBER, CNAME, CONTACTP, STREETN, ZIPC, CITY, PHONE, EMAIL, USERNAME) values(?, ?, ?, ?, ?, ?, ?, ?, ?)");
sqlnewRegister.setInt(1, newRegister.getOrgNumber());
sqlnewRegister.setString(2, newRegister.getCompanyName());
sqlnewRegister.setString(3, newRegister.getContactPerson());
sqlnewRegister.setString(4, newRegister.getStreetName());
sqlnewRegister.setInt(5, newRegister.getZipCode());
sqlnewRegister.setString(6, newRegister.getCity());
sqlnewRegister.setLong(7, newRegister.getPhone());
sqlnewRegister.setString(8, newRegister.getEmail());
sqlnewRegister.setString(9 newRegister.getUsername());
sqlnewRole = connection.prepareStatement("insert into role (USERNAME, PASSWORD, ROLE) values (?, ?, ?)");
sqlnewRole.setString(1, newRegister.getUsername());
sqlnewRole.setString(2, newRegister.getPassword());
sqlnewRole.setString(3, knd);
sqlnewPlace = connection.prepareStatement("insert into place (ZIPC, CITY, STREETN) values (?, ?, ?)");
sqlnewPlace.setInt(1, newRegister.getZipCode());
sqlnewPlace.setString(2, newRegister.getCity());
sqlnewPlace.setString(3, newRegister.getStreetName());
sqlnewRegister.executeUpdate();
sqlnewRole.executeUpdate();
sqlnewPlace.executeUpdate();
connection.commit();
/*
* Transaction ended
*/
ok = true;
} catch (SQLException e) {
Cleaner.rollBack(connection);
String sqlStatus = e.getSQLState().trim();
String statusclass = sqlStatus.substring(0, 2);
if (statusclass.equals("23")) { // Standard code for "integrity constraint violation"
ok = false; // This orgnumber is already registered
} else {
Cleaner.writeMessage(e, "WriteToDB");
}
} finally {
Cleaner.settAutoCommit(connection);
Cleaner.closeSentence(sqlnewRegister);
Cleaner.closeSentence(sqlnewRole);
Cleaner.closeSentence(sqlnewPlace);
}
closeConnection();
return ok;
}
You'll have to insert the role and place before you can insert the customer, as otherwise your referential integrity will be violated.
The two ALTER TABLE statements mean that customer.username must have a corresponding value role.username and customer.zipcode must point to a valid place.zipcode.
As you're inserting the customer first, those records won't exist yet.
EDIT:
Changing the order of executeUpdate to
sqlnewRole.executeUpdate();
sqlnewPlace.executeUpdate();
sqlnewRegister.executeUpdate();
should do the trick.
EDIT2:
One thing to note though, your code will fall over if you have two people with the same ZIP code, as your insert to place will violate the primary key if you try adding it twice...
Table
id int(11) No auto_increment Change Drop Primary Index Unique Fulltext
email varchar(45) latin1_swedish_ci No Change Drop Primary Index Unique Fulltext
billpayment tinyint(1) No Change Drop Primary Index Unique Fulltext
dispatch tinyint(1) No Change Drop Primary Index Unique Fulltext
address varchar(75) latin1_swedish_ci Yes NULL Change Drop Primary Index Unique Fulltext
phone int(11) Yes NULL Change Drop Primary Index Unique Fulltext
created_at datetime No Change Drop Primary Index Unique Fulltext
totalbillamount float Yes NULL Change Drop Primary Index Unique Fulltext
Java Code:
sql = "insert into session_shopping (email,billpayment,dispatch,address,phone,created_at,totalbillamount) values(?,?,?,?,?,?,?)";
ps = (PreparedStatement) con.prepareStatement(sql);
ps.setString(1, email);
ps.setBoolean(2, false);
ps.setBoolean(3, false);
ps.setString(4, "");
ps.setInt(5, 0);
java.util.Date date = new java.util.Date();
long t = date.getTime();
java.sql.Date sqlDate = new java.sql.Date(t);
ps.setDate(6, sqlDate);
ps.setFloat(7, 00.0f);
int newId = ps.executeUpdate();
System.out.println("newId" + newId);
if (newId == 1) {
sql = "select * from session_shopping where id = ?";
ps = (PreparedStatement) con.prepareStatement(sql);
ps.setInt(1, newId);
ResultSet reS = ps.executeQuery();
Session s = new Session();
s.setId(reS.getInt("id"));
s.setEmail(reS.getString("email"));
System.out.println("retreived");
return s;
} else {
System.out.println("unable to save");
}
This code fails because int newId is boolean
What i want to do is. I want to retrieve the row which i added just now.
executeUpdate will return the number of rows affected, not the current row .
Try this
ResultSet rs = aStatement.executeQuery("SELECT LAST_INSERT_ID()");
while(rs.next())
{
key = rs.getInt(1);
}
The value returned by executeUpdate has nothing to do with your ID.
Before we go to getting your ID, you can start off by setting email in the Session from your email variable rather than pulling it back out of the database.
As things stand, the only way I can think of to get the newly inserted ID is by changing your SQL thus:
sql = "select max(id) from session_shopping";
This will give you problems if you get another insert before you pick out the maximum ID though. To prevent that, put the select in the same transaction as your insert.