DTO
UserDto.java
package dto;
import java.io.Serializable;
public class UserDto implements Serializable {
private String id;
private String name;
private int age;
private String joindate;
public UserDto() {
}
public UserDto(String id, String name, int age, String joindate) {
super();
this.id = id;
this.name = name;
this.age = age;
this.joindate = joindate;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getJoindate() {
return joindate;
}
public void setJoindate(String joindate) {
this.joindate = joindate;
}
@Override
public String toString() {
return "UserDto [id=" + id + ", name=" + name + ", age=" + age + ", joindate=" + joindate + "]";
}
}
INSERT
jdbctest.java
package db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class jdbctest {
public jdbctest() {
try {
Class.forName("oracle.jdbc.driver.OracleDriver"); // 이 클래스가 존재를 하냐 안하냐 할수 있게 해준다.
System.out.println("Driver Loding Success!!");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public Connection getConnection() {
Connection conn = null;
try {
conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:xe", "hr", "hr");
System.out.println("DB Connection Success!!");
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public int Insert(String id, String name, int age) {
//쿼리문을 작성할땐 앞뒤로 한칸씩 떨어트려 놓자
String sql = " INSERT INTO USERDTO(ID, NAME, AGE, JOINDATE) "
+ " VALUES('" + id + "', '" + name + "', " + age + ", SYSDATE) ";
Connection conn = getConnection(); // db 정보 얻어오기
Statement stmt = null;
int count = 0;
System.out.println("sql:" + sql);
try {
stmt = conn.createStatement(); // db 현상태를 얻어오기
count = stmt.executeUpdate(sql); // sql문을 실행시켜준다.
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
if(stmt != null) {
stmt.close();
}
if(conn != null) {
conn.close();
}
}catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return count;
}
}
mainClass.java
package main;
import db.jdbctest;
import dto.UserDto;
public class mainClass {
public static void main(String[] args) {
/*
JDBC
java database connectivity
*/
jdbctest jt = new jdbctest();
int n = jt.Insert("ddd", "손오천", 21);
if(n > 0){
System.out.println("정상적으로 추가 되었습니다.");
}
}
}
DELETE
jdbctest.java
public int Delete(String id) {
//쿼리문을 작성할땐 앞뒤로 한칸씩 떨어트려 놓자
String sql = " DELETE FROM USERDTO WHERE ID = '" + id + "'";
Connection conn = getConnection(); // db 정보 얻어오기
Statement stmt = null;
int count = 0;
System.out.println("sql:" + sql);
try {
stmt = conn.createStatement(); // db 현상태를 얻어오기
count = stmt.executeUpdate(sql); // sql문을 실행시켜준다.
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
if(stmt != null) {
stmt.close();
}
if(conn != null) {
conn.close();
}
}catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return count;
}
mainClass.java
public static void main(String[] args) {
/*
JDBC
java database connectivity
*/
jdbctest jt = new jdbctest();
int n = jt.Delete("ddd");
if(n > 0){
System.out.println("정상적으로 삭제 되었습니다.");
}
}
UPDATE
jdbctest.java
public int Updata(String id, String name, int age) {
//쿼리문을 작성할땐 앞뒤로 한칸씩 떨어트려 놓자
String sql = " UPDATE USERDTO SET NAME = '" + name + "', AGE = " + age
+ " WHERE ID = '" + id + "'";
Connection conn = getConnection(); // db 정보 얻어오기
Statement stmt = null;
int count = 0;
System.out.println("sql:" + sql);
try {
stmt = conn.createStatement(); // db 현상태를 얻어오기
count = stmt.executeUpdate(sql); // sql문을 실행시켜준다.
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
if(stmt != null) {
stmt.close();
}
if(conn != null) {
conn.close();
}
}catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return count;
}
mainClass.java
public static void main(String[] args) {
/*
JDBC
java database connectivity
*/
jdbctest jt = new jdbctest();
int n = jt.Updata("aaa", "손오공", 31);
if(n > 0){
System.out.println("정상적으로 변경 되었습니다.");
}
}
'IT > JDBC' 카테고리의 다른 글
DB연결 데이터 입출력(2) (0) | 2020.03.25 |
---|---|
JDBC DB연결셋팅 (0) | 2020.03.03 |
댓글