반응형
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
- @ Builder
- db
- select
- static을 왜사용할까?
- http
- Create
- unity 레이아웃
- MariaDB
- 타입 변수 표기법
- java
- 메소드 지정자
- 타입이 서로 다른 두 데이터 제네릭
- spring
- request
- 요청
- final
- 메소드 정의
- unity 간단 설정
- static
- 데이터베이스
- WriteLine
- DROP
- response
- ForignKey
- c#상속
- C#
- 평면좌표상에서 두점 거리 구하기
- JDBC
- unity 오브젝트
- Database
Archives
- Today
- Total
이론을 싫어!
[jdbc] 자바와 db연동후 update하는법 본문
반응형
update
import java.sql.*;
import java.util.Scanner;
public class DBTestUpdate {
public static void DBUpdate(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="update member" +
" set password= ? " +
" where user_id= ? ";
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setString(1,pw);
preparedStatement.setString(2,user_id);
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);
System.out.println("비밀번호 변경할 아이디를 입력해주세요.");
String user_id=scan.next();
System.out.println("변경할 비밀번호를 입력해주세요.");
String pw=scan.next();
DBUpdate(user_id,pw);
DBSelect();
}
}
위의 코드를 실행하게 되면
pw가 변경된 것을 알수 있고
db 또한 변경되어져 있는것을 알수 있다.
코드를 보면 insert와 똑같다 다만 다른것은 쿼리문 만 다를뿐 나머지는 다른것이 없다.
'JDBC' 카테고리의 다른 글
[jdbc] 자바와 db연동후 insert하는법 (select와 기본응용) (0) | 2023.04.03 |
---|---|
[jdbc] 자바와 db와 연동후 DELETE하는법 (0) | 2023.04.03 |
[JDBC] 실제 데이터베이스에서 데이터 꺼내보기!(statement preparedstatement 차이) (0) | 2023.03.06 |
[JDBC] 초급 무엇인가... 하는 김에 연결까지 해보자...(MariaDB) (2) | 2023.03.06 |