📄 customersindb.java
字号:
package com.ttdev.bank;
import java.sql.*;
public class CustomersInDB implements Customers {
private Connection conn;
public CustomersInDB(Connection conn) {
this.conn = conn;
}
public Customer loadCustomer(String customerId) {
try {
PreparedStatement st = conn.prepareStatement("select * from customers where id=?");
try {
st.setString(1, customerId);
ResultSet rs = st.executeQuery();
if (!rs.next()) {
throw new RuntimeException("Customer has been deleted");
}
return new Customer(
customerId,
rs.getString("name"),
rs.getString("address"));
} finally {
st.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public void saveCustomer(Customer customer) {
try {
PreparedStatement st =
conn.prepareStatement("update customers set name=?, address=? where id=?");
try {
st.setString(1, customer.getName());
st.setString(2, customer.getAddress());
st.setString(3, customer.getId());
st.executeUpdate();
} finally {
st.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -