138 lines
4.8 KiB
Java
138 lines
4.8 KiB
Java
package dao.specification;
|
|
|
|
import dao.indentItemAbstractDAO;
|
|
import entities.IndentItem;
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
import java.sql.*;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import static dao.specification.abstractDAO.Delete;
|
|
import static util.SQLDatabaseConnection.close;
|
|
import static util.SQLDatabaseConnection.getConnection;
|
|
|
|
public class indentItemDAO implements indentItemAbstractDAO {
|
|
|
|
private Connection connection = null;
|
|
private PreparedStatement preparedStatement = null;
|
|
private ResultSet resultSet = null;
|
|
|
|
@Override
|
|
public int insert(@NotNull IndentItem IItem) {
|
|
int flag;
|
|
String insert_sql = "insert into indentItem(name, initialPrice, discount, " +
|
|
"finalPrice, description, indentID) values (?,?,?,?,?,null)";
|
|
try {
|
|
connection = getConnection();
|
|
preparedStatement = connection.prepareStatement(insert_sql);
|
|
|
|
preparedStatement.setString(1, IItem.getName());
|
|
preparedStatement.setFloat(2, IItem.getInitialPrice());
|
|
//折扣和折后价null值的判断
|
|
if (IItem.getDiscount().compareTo(0F) > 0 //折扣和0比较,返回1为合法值
|
|
|| !String.valueOf(IItem.getDiscount()).equals(" ")) {
|
|
preparedStatement.setFloat(3, IItem.getDiscount());
|
|
IItem.setFinalPrice(IItem.getInitialPrice() * IItem.getDiscount());//计算最终价格
|
|
preparedStatement.setFloat(4, IItem.getFinalPrice());
|
|
} else {
|
|
preparedStatement.setNull(3, Types.FLOAT);
|
|
preparedStatement.setNull(4, Types.FLOAT);
|
|
}
|
|
//描述
|
|
if (!IItem.getDescription().equals(" ")) {
|
|
preparedStatement.setString(5, IItem.getDescription());
|
|
} else {
|
|
preparedStatement.setNull(5, Types.NVARCHAR);
|
|
}
|
|
|
|
flag = preparedStatement.executeUpdate(insert_sql);
|
|
} catch (SQLException e) {
|
|
throw new RuntimeException(e);
|
|
} finally {
|
|
close(connection, preparedStatement, resultSet);
|
|
}
|
|
return flag;
|
|
}
|
|
|
|
@Override
|
|
public int delete(@NotNull IndentItem IItem) {
|
|
String delete_sql = "delete from indentItem where id = ?";
|
|
// int flag;
|
|
// try {
|
|
// connection = getConnection();
|
|
// preparedStatement = connection.prepareStatement(delete_sql);
|
|
// preparedStatement.setLong(1, item.getId());
|
|
// flag = preparedStatement.executeUpdate(delete_sql);
|
|
//
|
|
// } catch (SQLException e) {
|
|
// throw new RuntimeException(e);
|
|
// } finally {
|
|
// close(connection,preparedStatement,resultSet);
|
|
// }
|
|
//
|
|
// return flag;
|
|
return Delete
|
|
(connection, preparedStatement, resultSet, delete_sql, IItem.getId());
|
|
}
|
|
|
|
|
|
@Override//TODO 这个要考虑的太多了,先跳过去
|
|
public int update(IndentItem IItem) {
|
|
return 0;
|
|
}
|
|
|
|
@Override
|
|
public List<IndentItem> search(IndentItem IItem) {
|
|
List<IndentItem> list = new ArrayList<>();
|
|
String selectAll_sql = "select * from indentItem";
|
|
try {
|
|
connection = getConnection();
|
|
preparedStatement = connection.prepareStatement(selectAll_sql);
|
|
preparedStatement.executeQuery();
|
|
|
|
while (resultSet.next()) {
|
|
IItem = new IndentItem();
|
|
SetAttribute(IItem, resultSet);
|
|
list.add(IItem);
|
|
}
|
|
} catch (SQLException e) {
|
|
throw new RuntimeException(e);
|
|
} finally {
|
|
close(connection, preparedStatement, resultSet);
|
|
}
|
|
return list;
|
|
}
|
|
|
|
@Override
|
|
public IndentItem searchID(Long id) {
|
|
IndentItem IItem = null;
|
|
String selectID_sql = "select * from indentItem where id = ?";
|
|
try {
|
|
connection = getConnection();
|
|
preparedStatement = connection.prepareStatement(selectID_sql);
|
|
preparedStatement.setLong(1, id);
|
|
preparedStatement.executeQuery();
|
|
while (resultSet.next()) {
|
|
IItem = new IndentItem();
|
|
SetAttribute(IItem, resultSet);
|
|
}
|
|
|
|
} catch (SQLException e) {
|
|
throw new RuntimeException(e);
|
|
} finally {
|
|
close(connection, preparedStatement, resultSet);
|
|
}
|
|
return IItem;
|
|
}
|
|
|
|
private void SetAttribute(@NotNull IndentItem item, @NotNull ResultSet rs) throws SQLException {
|
|
item.setId(rs.getLong("id"));
|
|
item.setName(rs.getString("name"));
|
|
item.setInitialPrice(rs.getFloat("initialPrice"));
|
|
item.setDiscount(rs.getFloat("discount"));
|
|
item.setFinalPrice(rs.getFloat("finalPrice"));
|
|
item.setDescription(rs.getString("description"));
|
|
}
|
|
}
|