CREATE USER DBTEST IDENTIFIED BY a1234;
GRANT CONNECT, RESOURCE TO BY DBTEST;
ALTER USER DBTEST DEFAULT TABLESPACE USERS;
ALTER USER DBTEST QUOTA UNLIMITED ON USERS;
CREATE TABLE member(
name VARCHAR2(20) NOT NULL,
age NUMBER(3),
height NUMBER(10, 2),
logtime DATE
);
DESC member;
JAVA에서 데이터를 만들면 아래에서 확인이 가능하다!
SELECT * FROM member;
package ch01_driver;
import java.sql.Connection;
import java.sql.DriverManager;
public class DriverConnect {
public static void main(String[] args) {
// Oracle JDBC 드라이버 클래스 메모리에 로드
try {
Class.forName("oracle.jdbc.OracleDriver");
System.out.println("로딩 성공!");
} catch (Exception e) {
System.out.println("로딩 실패~");
e.printStackTrace();
}
// 연결 정보
String url = "jdbc:oracle:thin:@localhost:1521:xe";
String id = "dbtest";
String pwd = "a1234";
Connection con = null;
try {
// DB 연결 객체 생성
con = DriverManager.getConnection(url, id, pwd);
System.out.println("연결 성공...");
} catch (Exception e) {
System.out.println("연결실패~");
e.printStackTrace();
}
}
}
package ch01_driver;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Scanner;
class Insert {
// 연결 정보
private String url = "jdbc:oracle:thin:@localhost:1521:xe";
private String id = "dbtest";
private String pwd = "a1234";
public Insert() {
// Oracle JDBC 드라이버 클래스 메모리에 로드
try {
Class.forName("oracle.jdbc.OracleDriver");
System.out.println("로딩 성공!");
} catch (Exception e) {
System.out.println("로딩 실패~");
e.printStackTrace();
}
}// Insert
public Connection getConnection() {
Connection con = null;
try {
// DB 연결 객체 생성
con = DriverManager.getConnection(url, id, pwd);
System.out.println("연결 성공...");
} catch (Exception e) {
System.out.println("연결실패~");
e.printStackTrace();
}
return con;
}// getConnection
// 회원 추가
public void insertData() {
Scanner scanner = new Scanner(System.in);
System.out.print("이름 입력 >");
String name = scanner.nextLine();
System.out.print("나이 입력 >");
int age = scanner.nextInt();
System.out.print("키 입력 >");
double height = scanner.nextDouble();
Connection con = null;
PreparedStatement pstmt = null;
int res = 1;
try {
// 쿼리 문자열
String sql = "insert into member values(?,?,?,sysdate)";
con = getConnection(); // connection 객체 생성
pstmt = con.prepareStatement(sql); // 쿼리문 실행 객체 생성
pstmt.setString(1, name); // 쿼리문 완성
pstmt.setInt(2, age);
pstmt.setDouble(3, height);
res = pstmt.executeUpdate(); // 쿼리문 실행
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 사용 자원 종료
if (pstmt != null)
pstmt.close();
if (con != null)
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println(res + "개의 행이 추가 되었습니다..");
}
}// Insert
public class ImsertTest {
public static void main(String[] args) {
Insert db = new Insert();
// db.getConnection();
db.insertData();
}
}
package ch03_select;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
####
class Select {
// 연결 정보
private String url = "jdbc:oracle:thin:@localhost:1521:xe";
private String id = "dbtest";
private String pwd = "a1234";
public Select() {
// Oracle JDBC 드라이버 클래스 메모리에 로드
try {
Class.forName("oracle.jdbc.OracleDriver");
System.out.println("로딩 성공!");
} catch (Exception e) {
System.out.println("로딩 실패~");
e.printStackTrace();
}
}// Insert()
public Connection getConnection() {
Connection con = null;
try {
// DB 연결 객체 생성
con = DriverManager.getConnection(url, id, pwd);
System.out.println("연결 성공...");
} catch (Exception e) {
System.out.println("연결실패~");
e.printStackTrace();
}
return con;
}// getConnection
// 회원 목록
public void selectData() {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet res = null;
try {
String sql = "select * from member";
con = this.getConnection();
pstmt = con.prepareStatement(sql);
res = pstmt.executeQuery();
while (res.next()) {
String name = res.getString("name");
int age = res.getInt("age");
double height = res.getDouble("height");
String logtime = res.getString("logtime");
System.out.println(name + "\t" + age + "\t" + height + "\t" + logtime);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (res != null)
res.close();
if (pstmt != null)
pstmt.close();
if (con != null)
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
} // Select
public class SelectTest {
public static void main(String[] args) {
Select db = new Select();
//db.getConnection();
db.selectData();
}
}
package ch04_Update;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Scanner;
class Update {
// 연결 정보
private String url = "jdbc:oracle:thin:@localhost:1521:xe";
private String id = "dbtest";
private String pwd = "a1234";
public Update() {
// Oracle JDBC 드라이버 클래스 메모리에 로드
try {
Class.forName("oracle.jdbc.OracleDriver");
//System.out.println("로딩 성공...");
} catch (Exception e) {
//System.out.println("로딩 실패~");
e.printStackTrace();
}
} // Insert()
public Connection getConnection() {
Connection con = null;
try {
// DB 연결 객체 생성
con = DriverManager.getConnection(url, id, pwd);
//System.out.println("연결 성공...");
} catch (Exception e) {
//System.out.println("연결 실패~");
e.printStackTrace();
}
return con;
} // getConnection()
// 회원 나이 수정
public void updateData() {
Scanner scanner = new Scanner(System.in);
System.out.print("수정 이름 입력 > ");
String name = scanner.next();
Connection con = null;
PreparedStatement pstmt = null;
int su = 0;
try {
String sql = "update member set age=age+1 where name=?";
con = this.getConnection();
pstmt = con.prepareStatement(sql);
pstmt.setString(1, name);
su = pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(pstmt != null) pstmt.close();
if(con != null) con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println(su + "개의 행이 수정 되었습니다..");
}
} // Update
public class UpdateTest {
public static void main(String[] args) {
Update db = new Update();
db.updateData();
}
}
package ch05_delete;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Scanner;
class Delete {
// 연결 정보
private String url = "jdbc:oracle:thin:@localhost:1521:xe";
private String id = "dbtest";
private String pwd = "a1234";
public Delete() {
// Oracle JDBC 드라이버 클래스 메모리에 로드
try {
Class.forName("oracle.jdbc.OracleDriver");
//System.out.println("로딩 성공...");
} catch (Exception e) {
//System.out.println("로딩 실패~");
e.printStackTrace();
}
} // Insert()
public Connection getConnection() {
Connection con = null;
try {
// DB 연결 객체 생성
con = DriverManager.getConnection(url, id, pwd);
//System.out.println("연결 성공...");
} catch (Exception e) {
//System.out.println("연결 실패~");
e.printStackTrace();
}
return con;
} // getConnection()
// 회원 삭제
public void deleteData() {
Scanner scanner = new Scanner(System.in);
System.out.print("삭제 이름 입력 > ");
String name = scanner.next();
Connection con = null;
PreparedStatement pstmt = null;
int su = 0;
try {
String sql = "delete from member where name=?";
con = this.getConnection();
pstmt = con.prepareStatement(sql);
pstmt.setString(1, name);
su = pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(pstmt != null) pstmt.close();
if(con != null) con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println(su + "개의 행이 삭제 되었습니다..");
}
} // Delete
public class DeleteTest {
public static void main(String[] args) {
Delete db = new Delete();
db.deleteData();
}
}