package partice;
import com.sun.source.tree.BreakTree;
import java.sql.*;
public class Test {
public static void main(String[] args) throws Exception{
String url = "jdbc:mysql://localhost:3306/testDB";
String user = "root";
String pwd = "1234";
Connection con = DriverManager.getConnection(url, user, pwd);
Statement stmt = con.createStatement();
ResultSet rs = null;
// Insert
{
String insertSql = "INSERT INTO customer VALUES (6, '손흥민', '영국 토트넘', '010-3333-1234'); ";
int ret = stmt.executeUpdate(insertSql);
System.out.println(ret);
}
// Update
{
String updateSql = "UPDATE customer SET address = '한국 서울' WHERE custid = 6; ";
int ret = stmt.executeUpdate(updateSql);
System.out.println(ret);
}
// Select
{
String selectSql = "SELECT * FROM customer; ";
rs = stmt.executeQuery(selectSql);
while (rs.next()) {
// row 1
System.out.println(rs.getInt("custid" ) + " | " +
rs.getString("name") + " | " +
rs.getString("address") + " | " +
rs.getString("phone"));
}
}
// Select 1 row
{
String selectDetailSql = "SELECT * FROM customer WHERE custid = 6; ";
rs = stmt.executeQuery(selectDetailSql);
if (rs.next()) {
// row 1
System.out.println(rs.getInt("custid" ) + " | " +
rs.getString("name") + " | " +
rs.getString("address") + " | " +
rs.getString("phone"));
}
}
// DELETE
{
String deleteSql = "DELETE + " +
"FROM Customer " +
"WHERE custid = 6; ";
int ret = stmt.executeUpdate(deleteSql);
System.out.println(ret);
}
if (rs != null) rs.close();
stmt.close();
con.close();
}
}
Connection con = DriverManager.getConnection(url, user, pwd);
Statement stmt = con.createStatement();
String updateSql = "UPDATE Customer SET address = '한국 서울', WHERE customerId = 6; ";
stmt.executeUpdate(updateSql);
Connection con = DriverManager.getConnection(url, user, pwd);
// ? 순서대로 index 1부터 적용된다.
String updateSql = "UPDATE Customer SET address = ?, WHERE customerId = ?; ";
Statement pstmt = con.prepareStatement(updateSql);
pstmt.setString(1, "한국 서울"); // 즉, ?의 첫 번째인 1번 Index에 "한국 서울" 값을 업데이트한다.
pstmt.setInt(2, 6); // 2번 Index인 customerId에 6을 넣는다.
pstmt.executeUpdate();
package partice;
// --CustomerDto
public class CustomerDto {
private int custId;
private String name;
private String address;
private String phone;
public CustomerDto() {}
public CustomerDto(int custId, String name, String address, String phone) {
this.custId = custId;
this.name = name;
this.address = address;
this.phone = phone;
}
// get
public int getCustId() {
return custId;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public String getPhone() {
return phone;
}
// set
public void setCustId(int custId) {
this.custId = custId;
}
public void setName(String name) {
this.name = name;
}
public void setAddress(String address) {
this.address = address;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "CustomerDto [custId=" + custId + ", name=" + name + ", address=" + address + ", phone=" + phone + "]";
}
}
// --Main
List<CustomerDto> list = listCustomer();
// 생성이 모두 완료 됐고, for문을 통해 print 처리
for (CustomerDto dto : list) {
System.out.println(dto);
}
// --listCustomer
public List<CustomerDto> listCustomer() {
Connection con = DriverManager.getConnection(url, user, pwd);
List<CustomerDto> list = new ArraysList<>();
String selectSql = "SELECT * FROM Customer; ";
PrepareStatement pstmt = con.prepareStatement(selectSql);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
// row로 읽은 1개 행의 데이터를 Customer Dto를 통해 인스턴스를 생성한다.
CustomerDto dto = new Customer();
dto.setCustomerId(rs.getInt("customerId"));
dto.setName(rs.getString("name"));
dto.setAddress(rs.getString("address"));
dto.setPhone(rs.getString("phone"));
// 인스턴스를 ArrayList에 추가한다.
list.add(dto);
}
return list;
}
참고 사이트