package com.xzy.dao;
import com.xzy.dao.FlowerDao;
import com.xzy.pojo.Flower;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class FlowerDaoImpl implements FlowerDao {
public List<Flower> selAll(){
List<Flower> list=new ArrayList<>();
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
try{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/flower?serverTimezone=UTC&characterEncoding=utf-8","root","Xiao1234!");
ps=conn.prepareStatement("select * from flower");
rs=ps.executeQuery();//rs是一行数据,只要是jdbc,都是一行行通过游标指引
while(rs.next()){ //re,next() 将光标从当前位置向前移一行。
list.add(new Flower(rs.getInt(1),rs.getString(2),rs.getDouble(3),rs.getString(4)));
}
}catch (ClassNotFoundException e){
e.printStackTrace();
}catch (SQLException e){
e.printStackTrace();
}finally {
try {
rs.close();
ps.close();
conn.close();
}catch (SQLException e){
e.printStackTrace();
}
}
return list;
}
@Override
public int insFlower(Flower flower) {
int index=0;
Connection conn=null;
PreparedStatement ps=null;
try{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/flower?serverTimezone=UTC&characterEncoding=utf-8","root","Xiao1234!");
ps=conn.prepareStatement("insert into flower values (default ,?,?,?)");//!!!! default ?
ps.setObject(1,flower.getName());
ps.setObject(2,flower.getPrice());
ps.setObject(3,flower.getProduction());
index=ps.executeUpdate();
}catch (ClassNotFoundException e){
e.printStackTrace();
}catch (SQLException e){
e.printStackTrace();
}finally {
try {
ps.close();
conn.close();
}catch (SQLException e){
e.printStackTrace();
}
}
return index;
}
}