728x90
728x90
BIG
delete구문
package helloworld;
import java.net.ConnectException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
public class m2 {
static PreparedStatement pstmt;
static Connection conn;
private static String id;
private static String name;
private static double height;
private static double weight;
private static int age;
private static Statement stmt;
private static ResultSet rs;
public static void main(String[] args) throws SQLException{
//1.driver loading
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:xe";
String userid = "sample";
String pwd = "1234";
try {
Class.forName(driver);
System.out.println("Oracle Driver Loading Success!");
//2.connection
conn = DriverManager.getConnection(url, userid, pwd);
System.out.println("Oracle Connection Success!");
//3.sql command
//데이터 입력하는 부분
Scanner sc = new Scanner(System.in);
System.out.println("아이디를 입력하세요");
String id = sc.next();
// System.out.println("이름을 입력하세요");
// String name = sc.next();
// System.out.println("키를 입력하세요");
// double height = sc.nextDouble();
// System.out.println("몸무게를 입력하세요");
// double weight = sc.nextDouble();
// System.out.println("나이를 입력하세요");
// int age = sc.nextInt();
String sql = "delete from member where id=?";
pstmt = conn.prepareStatement(sql);
// pstmt.setInt(1, age);
pstmt.setString(1, id);
// pstmt.setDouble(3, height);
// pstmt.setDouble(4, weight);
// pstmt.setInt(5, age);
pstmt.executeUpdate();
//4.result
//stmt = conn.createStatement();
//String quy = "select * from member";
pstmt = conn.prepareStatement("select * from member");
rs = pstmt.executeQuery();
System.out.println("===================================");
while(rs.next()) {
System.out.print("아이디: " + rs.getString("id")+" ");
System.out.print("이 름: " + rs.getString("name")+" ");
System.out.print("키: " + rs.getDouble("height")+" ");
System.out.print("몸무게: " + rs.getDouble("weight")+" ");
System.out.println("나 이: " + rs.getInt("age"));
}
System.out.println("===================================");
}catch(ClassNotFoundException e){
e.printStackTrace();
/*
}catch(NullPointerException e2) {
e2.printStackTrace();
*/
}catch(Exception e3) {
e3.printStackTrace();
}finally {
if(rs!=null) {
try {
rs.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
if (pstmt !=null) {
try {
pstmt.close();
}catch (Exception e) {
e.printStackTrace();
}
if (conn !=null) {
try {
conn.close();
}catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
}
update구문
package helloworld;
import java.net.ConnectException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
public class member {
static PreparedStatement pstmt;
static Connection conn;
private static String id;
private static String name;
private static double height;
private static double weight;
private static int age;
private static Statement stmt;
private static ResultSet rs;
public static void main(String[] args) throws SQLException{
//1.driver loading
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:xe";
String userid = "sample";
String pwd = "1234";
try {
Class.forName(driver);
System.out.println("Oracle Driver Loading Success!");
//2.connection
conn = DriverManager.getConnection(url, userid, pwd);
System.out.println("Oracle Connection Success!");
//3.sql command
//데이터 입력하는 부분
Scanner sc = new Scanner(System.in);
System.out.println("아이디를 입력하세요");
String id = sc.next();
// System.out.println("이름을 입력하세요");
// String name = sc.next();
// System.out.println("키를 입력하세요");
// double height = sc.nextDouble();
// System.out.println("몸무게를 입력하세요");
// double weight = sc.nextDouble();
System.out.println("나이를 입력하세요");
int age = sc.nextInt();
String sql = "UPDATE member SET AGE = ? WHERE id = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, age);
pstmt.setString(2, id);
// pstmt.setDouble(3, height);
// pstmt.setDouble(4, weight);
// pstmt.setInt(5, age);
pstmt.executeUpdate();
//4.result
//stmt = conn.createStatement();
//String quy = "select * from member";
pstmt = conn.prepareStatement("select * from member");
rs = pstmt.executeQuery();
System.out.println("===================================");
while(rs.next()) {
System.out.print("아이디: " + rs.getString("id")+" ");
System.out.print("이 름: " + rs.getString("name")+" ");
System.out.print("키: " + rs.getDouble("height")+" ");
System.out.print("몸무게: " + rs.getDouble("weight")+" ");
System.out.println("나 이: " + rs.getInt("age"));
}
System.out.println("===================================");
}catch(ClassNotFoundException e){
e.printStackTrace();
/*
}catch(NullPointerException e2) {
e2.printStackTrace();
*/
}catch(Exception e3) {
e3.printStackTrace();
}finally {
if(rs!=null) {
try {
rs.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
if (pstmt !=null) {
try {
pstmt.close();
}catch (Exception e) {
e.printStackTrace();
}
if (conn !=null) {
try {
conn.close();
}catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
}
잘 돌아가는 것을 알 수 있다
728x90
반응형
BIG
'🍷DataBase > 🎮Oracle' 카테고리의 다른 글
[Oracle]오라클 TNS:protocol adapter error 잡는 법! (0) | 2023.02.16 |
---|---|
ORA-00970: missing WITH keyword 에러 잡기 (0) | 2023.02.14 |
[Oracle]자바에 데이터베이스 연동하기 2(copyright by 전씨) (0) | 2022.12.21 |
[Oracle]자바에 데이터베이스 연동하기 1 (0) | 2022.12.21 |
[Oracle/오라클]csv같은 외부데이터 오라클 테이블로 가져오기 (0) | 2022.11.17 |
댓글