코드 자체 검토
여기 제가 사용하기 전에 저희 실습에서 제가 작성한 데이터베이스의 조작 클래스를 자평합니다.
package imp;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import bean.Admin;
import bean.Book;
import bean.Lend;
import bean.User;
import tools.ConnectionManager;
import dao.AdminDao;
public class AdminDaoImp implements AdminDao {
public Connection con = null;
PreparedStatement ps = null;
public ResultSet rs = null;
@Override
public Admin login(String a_name, String a_password) {
Admin admin = new Admin();
con = ConnectionManager.getConnection();
String sql = "select * from admin where a_username=? and a_password=?";
try {
ps = con.prepareStatement(sql);
ps.setString(1, a_name);
ps.setString(2, a_password);
rs = ps.executeQuery();
if (rs.next() == false) {
admin.setA_username("00000000");
} else {
admin.setA_username("a_name");
}
} catch (SQLException e) {
e.printStackTrace();
}
return admin;
}
@Override
public int addbooks(Book book) {
int result = 0;
con = ConnectionManager.getConnection();
String sql = "insert into bookinfo(b_name,b_price,b_writer,b_press,b_type,b_location,b_barcoad) values(?,?,?,?,?,?,?)";
try {
ps = con.prepareStatement(sql);
ps.setString(1, book.getB_name());
ps.setString(2, book.getB_price());
ps.setString(3, book.getB_writer());
ps.setString(4, book.getB_press());
ps.setString(5, book.getB_type());
ps.setString(6, book.getB_location());
ps.setString(7, book.getB_barcoad());
result = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
@Override
public List getAllUser() {
List list = new ArrayList();
try {
con = ConnectionManager.getConnection();
ps = con.prepareStatement("select * from userinfo");
rs = ps.executeQuery();
while (rs.next()) {
User user = new User();
user.setR_id(rs.getString("r_ID"));
user.setR_name(rs.getString("r_name"));
user.setR_sex(rs.getString("r_sex"));
user.setR_occ(rs.getString("r_occ"));
user.setR_IDnum(rs.getString("r_IDnum"));
user.setTel(rs.getString("r_tel"));
user.setR_mail(rs.getString("r_mail"));
list.add(user);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
@Override
public List getAllBook() {
List list = new ArrayList();
try {
con = ConnectionManager.getConnection();
ps = con.prepareStatement("select * from bookinfo");
rs = ps.executeQuery();
while (rs.next()) {
Book book = new Book();
book.setB_ID(rs.getString("b_ID"));
book.setB_name(rs.getString("b_name"));
book.setB_price(rs.getString("b_price"));
book.setB_writer(rs.getString("b_writer"));
book.setB_press(rs.getString("b_press"));
book.setB_type(rs.getString("b_type"));
book.setB_location(rs.getString("b_location"));
book.setB_barcoad(rs.getString("b_barcoad"));
list.add(book);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
@Override
public int deletBook(String bid) {
int result = 0;
Connection connection = ConnectionManager.getConnection();
String sql = "DELETE FROM bookinfo WHERE b_ID=?";
try {
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, bid);
result = preparedStatement.executeUpdate();
if (result != 0) {
result = 1;
} else {
result = 0;
}
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
@Override
public Book getbook(String bid) {
Book book = new Book();
con = ConnectionManager.getConnection();
String sql = "select * from bookinfo where b_ID=?";
try {
ps = con.prepareStatement(sql);
ps.setString(1, bid);
rs = ps.executeQuery();
while (rs.next()) {
book.setB_ID(rs.getString("b_ID"));
book.setB_name(rs.getString("b_name"));
book.setB_price(rs.getString("b_price"));
book.setB_writer(rs.getString("b_writer"));
book.setB_press(rs.getString("b_press"));
book.setB_type(rs.getString("b_type"));
book.setB_location(rs.getString("b_location"));
book.setB_barcoad(rs.getString("b_barcoad"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return book;
}
@Override
public int updatebook(String bid, Book book) {
int result = 0;
con = ConnectionManager.getConnection();
String sql = "UPDATE bookinfo SET b_name=?, b_price=?, b_writer=?, b_press=?, b_type=?, b_location=?, b_barcoad=? WHERE b_ID=?;";
try {
ps = con.prepareStatement(sql);
ps.setString(1, book.getB_name());
ps.setString(2, book.getB_price());
ps.setString(3, book.getB_writer());
ps.setString(4, book.getB_press());
ps.setString(5, book.getB_type());
ps.setString(6, book.getB_location());
ps.setString(7, book.getB_barcoad());
ps.setString(8, bid);
result = ps.executeUpdate();
if (result != 0) {
result = 1;
} else {
result = 0;
}
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
@Override
public int borrowBook(Lend lend) {
int result = 0;
con = ConnectionManager.getConnection();
String sql = "insert into lending(r_tel,b_barcoad,l_borrowtime,l_backtime,l_return,l_punishment) values(?,?,?,?,?,?)";
try {
ps = con.prepareStatement(sql);
ps.setString(1, lend.getR_tel());
ps.setString(2, lend.getB_barcoad());
ps.setString(3, lend.getL_borrowtime());
ps.setString(4, lend.getL_backtime());
ps.setString(5, lend.getL_return());
ps.setString(6, lend.getL_punishment());
result = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
@Override
public int returnbook(Lend lend) {
int result = 0;
con = ConnectionManager.getConnection();
String sql = "UPDATE lending SET l_return=? WHERE r_tel=? AND b_barcoad=?;";
try {
ps = con.prepareStatement(sql);
ps.setInt(1, 1);
ps.setString(2, lend.getR_tel());
ps.setString(3, lend.getB_barcoad());
result = ps.executeUpdate();
if (result != 0) {
result = 1;
} else {
result = 0;
}
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
@Override
public void overdueBook() {
List time = getTime();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
for (int i = 0; i < time.size(); i++) {
Lend lend = new Lend();
lend = (Lend) time.get(i);
try {
Date date = new Date();
String times = df.format(date);
Date d1 = df.parse(lend.getL_backtime());
Date d2 = df.parse(times);
long diff = d1.getTime() - d2.getTime();
long punishment = diff / (1000 * 60 * 60 * 24);
if (punishment < 0) {
punishment(lend.getID());
}
} catch (Exception e) {
}
}
}
public void punishment(String ID) {
con = ConnectionManager.getConnection();
String sql = "UPDATE lending SET l_punishment=1 WHERE ID=?;";
try {
ps = con.prepareStatement(sql);
ps.setString(1, ID);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public List getTime() {
List list = new ArrayList();
try {
con = ConnectionManager.getConnection();
ps = con.prepareStatement("SELECT * FROM lending WHERE l_punishment = 0;");
rs = ps.executeQuery();
while (rs.next()) {
Lend lend = new Lend();
lend.setL_backtime(rs.getString("l_backtime"));
lend.setID(rs.getString("ID"));
list.add(lend);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
@Override
public List getoverdue() {
overdueBook();
List list = new ArrayList();
try {
con = ConnectionManager.getConnection();
ps = con.prepareStatement("select * from lending WHERE l_punishment = 1;");
rs = ps.executeQuery();
while (rs.next()) {
Lend lend = new Lend();
lend.setR_tel(rs.getString("r_tel"));
lend.setB_barcoad(rs.getString("b_barcoad"));
lend.setL_backtime(rs.getString("l_backtime"));
list.add(lend);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
}
심사에서 코드의 작성자에게 문제를 제기하는 것이지 코드에서 직접 수정하는 것이 아니다.
다음은 저의 이런 종류의 코드 리뷰입니다.
4
4
4
4
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.