Сори за то что массивно..
Кидает в конце Data truncated for column 'type' at row 1.. немогу найти ошибку.. буду рад подсказки.
Создание обьектов и передача данных каждому.
передача параметров в базу.
Данные купона.
Таблица в базе данных.
Кидает в конце Data truncated for column 'type' at row 1.. немогу найти ошибку.. буду рад подсказки.
Создание обьектов и передача данных каждому.
Код:
public void createCoupon(Coupon coupon) throws SQLException{
long id [] = {32,33,34,35,36};
Date start_date [] = {Date.valueOf("2016-04-12"),Date.valueOf("2015-05-2"),
Date.valueOf("2017-06-13"),Date.valueOf("2016-07-23"),Date.valueOf("2016-12-15")};
Date end_date [] = {Date.valueOf("2017-04-12"),Date.valueOf("2016-05-2"),
Date.valueOf("2018-06-13"),Date.valueOf("2017-07-23"),Date.valueOf("2018-12-15")};
String title [] = {"1+1","1+2","50% Discount","20% Discount","80% Discount"};
String message [] = {"Perfect for family","Greate for couple","Singles only","Best Summer vacation","Ski"};
int amount [] = {20,35,45,100,150};
double price [] = {30.5,20.4,15,3,20.8,40.2};
String image [] = {"Picure1","Picure2","Picure3","Picure4","Picure5"};
CouponType coup_type [] = {CouponType.SPORTS,CouponType.TRAVELING,CouponType.FOOD,CouponType.ELECTRICITY,CouponType.CAMPING};
CouponDBDAO dbdao = new CouponDBDAO();
//creating 5 coupons
for(int i = 0; i < title.length;i++){
long comp_id = id[i];
String t = title[i];
Date s_date = start_date[i];
Date e_date = end_date[i];
int am = amount[i];
String mess = message[i];
double pr = price[i];
String im = image[i];
CouponType ty = coup_type[i];
coupon = new Coupon(comp_id,t, s_date, e_date, am, mess, pr, im, ty);
dbdao.createCoupon(coupon);
}
}
Код:
public void createCoupon(Coupon coupon) throws SQLException {
String cr_coupon = "INSERT INTO coupon (id,title,start_date,end_date,amount,type,message,price,image) values(?,?,?,?,?,?,?,?,?)";
PreparedStatement pre = con.prepareStatement(cr_coupon);
pre.setLong(1, coupon.getId());
pre.setString(2, coupon.getTitle());
pre.setDate(3, coupon.getStartDate());
pre.setDate(4, coupon.getEndDate());
pre.setInt(5, coupon.getAmount());
pre.setObject(6, coupon.getType());
pre.setString(7, coupon.getMessage());
pre.setDouble(8, coupon.getPrice());
pre.setString(9, coupon.getImage());
pre.execute();
coupons = new TreeSet<>();
coupons.add(coupon);
System.out.println("Inserting success");
}
Код:
public class Coupon {
private long id;
private String title;
private Date startDate;
private Date endDate;
private int amount;
private String message;
private double price;
private String image;
private CouponType type;
public Coupon(long id ,String title, Date startDate, Date endDate, int amount, String message, double price,
String image, CouponType type) {
setId(id);
setTitle(title);
setStartDate(startDate);
setEndDate(endDate);
setAmount(amount);
setMessage(message);
setPrice(price);
setImage(image);
setType(type);
}
Код:
create table coupon(
id bigint not null, title varchar(200) not null, start_date date not null, end_date date not null, amount int not null,
type enum('RESTAURANTS','ELECTRICITY','FOOD','HEALTH','SPORTS','CAMPING','TRAVELING') not null,message varchar(400) not null,price double not null,
image varchar(400) not null,primary key(id)
);