반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- response
- 요청
- http
- 평면좌표상에서 두점 거리 구하기
- 데이터베이스
- static을 왜사용할까?
- 메소드 정의
- WriteLine
- Create
- java
- MariaDB
- final
- 메소드 지정자
- unity 레이아웃
- spring
- 타입 변수 표기법
- JDBC
- unity 간단 설정
- request
- Database
- static
- @ Builder
- 타입이 서로 다른 두 데이터 제네릭
- c#상속
- C#
- DROP
- unity 오브젝트
- db
- select
- ForignKey
Archives
- Today
- Total
이론을 싫어!
[jdbc] 자바와 db와 연동후 DELETE하는법 본문
반응형
Delete
import java.sql.*;
import java.util.Scanner;
public class DBTesDelect {
public static void DBDelect(String user_id,String pw){
String url="jdbc:mariadb://[ip]:[port]/[테이블 명]";
String dbUserId="[계정]";
String dbPassword="[password]";
Connection connection=null;
PreparedStatement preparedStatement=null;
ResultSet rs=null;
try {
Class.forName("org.mariadb.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
connection= DriverManager.getConnection(url,dbUserId,dbPassword);
String sql="DELETE from member" +
" where user_id= ? " +
" and password= ? ";
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setString(1,user_id);
preparedStatement.setString(2,pw);
int affected =preparedStatement.executeUpdate();
if(affected>0){
System.out.println("삭제 성공");
}else{
System.out.println("삭제 실패");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if(rs!=null &&!rs.isClosed()){
rs.close();
}
if(preparedStatement!=null &&!preparedStatement.isClosed()){
preparedStatement.close();
}
if(connection!=null && !connection.isClosed()){
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void DBSelect(){
String url="jdbc:mariadb://[ip]:[port]/[테이블 명]";
String dbUserId="[계정]";
String dbPassword="[password]";
Connection connection=null;
PreparedStatement preparedStatement=null;
ResultSet rs=null;
try {
Class.forName("org.mariadb.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
connection= DriverManager.getConnection(url,dbUserId,dbPassword);
String sql="select member_type ,user_id ,password ,name from member" +
" where member_type = ? ";
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setString(1,"email");
rs=preparedStatement.executeQuery();
while(rs.next()){
String memberType= rs.getString("member_type");
String userId=rs.getString("user_id");
String password=rs.getString("password");
String name=rs.getString("name");
System.out.println(memberType+", "+userId+", "+password+", "+name);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if(rs!=null &&!rs.isClosed()){
rs.close();
}
if( preparedStatement!=null &&!preparedStatement.isClosed()){
preparedStatement.close();
}
if(connection!=null && !connection.isClosed()){
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
//1. Ip(domain) 2. port 3. 계정 4. 패스워드 5. 인스턴스
// System.out.println("가입된 유형을 입력해주세요(email or kakao)");
// String member_type=scan.next();
System.out.println("삭제할 아이디를 입력해주세요");
String user_id=scan.next();
System.out.println("비밀번호를 입력해주세요.");
String pw=scan.next();
DBDelect(user_id,pw);
DBSelect();
}
}
위의 코드를 실행하게 되면
위의 사진처럼 정상실행하는것을 볼수 있고
db에서도 자동으로 변경된것을 볼수 있다.
Delete 또한 Insert와 Update랑 쿼리문만 다를뿐 나머지는 똑같다는 것을 알수 있다.
데이터 다시 삭제
만약에 또한번 똑같은 데이터를 지운다면 실행결과는
위의 사진처럼 삭제 실패라고 뜬다.
소스 자체는 이상이 없다. 한마디로 쿼리문은 에러가 나지 않는다.
근데 왜 삭제 실패라고 뜰까?
이유는 간단하다. 쿼리문은 정상 실행되었지만
사용자가 등록한 데이터가 db에 없기 때문에 삭제 실패라고 뜬다.
'JDBC' 카테고리의 다른 글
[jdbc] 자바와 db연동후 update하는법 (0) | 2023.04.03 |
---|---|
[jdbc] 자바와 db연동후 insert하는법 (select와 기본응용) (0) | 2023.04.03 |
[JDBC] 실제 데이터베이스에서 데이터 꺼내보기!(statement preparedstatement 차이) (0) | 2023.03.06 |
[JDBC] 초급 무엇인가... 하는 김에 연결까지 해보자...(MariaDB) (2) | 2023.03.06 |