☝🏻오늘 배운 것
1. XML 파일
- XML(eXtensible Markup Language)
- 단순화된 데이터 기술 형식이라는 뜻
- XML에 저장되는 데이터 형식은 Key : Value로 저장되는 map형식이며, key, value 모두 String 형식이다.
2. Properties
- Properties Class
- Map의 후손 클래스
- XML 파일을 읽고, 쓰기위한 IO 관련 클래스
- Key : Value 값은 모두 String 형식이다
- XML 파일을 읽고, 쓰는데에 특화된 메서드 제공
- key를 주면 Value를 반환하는 기능을 가지며, DB의 연결정보 등을 저장해두는 용도로 많이 쓰임
3. storeToXML
- 데이터를 XML 형식으로 저장하는 데 사용되는 메서드 또는 함수
4. Class 세분화 하기
- CreateXMLFile Class
package edu.kh.jdbc.common;
import java.io.FileOutputStream;
import java.util.Properties;
import java.util.Scanner;
public class CreateXMLFile {
public static void main(String[] args) {
try {
Scanner sc = new Scanner(System.in);
Properties prop = new Properties();
System.out.print("생성할 파일 이름 : ");
String fileName = sc.nextLine();
FileOutputStream fos = new FileOutputStream(fileName + ".xml");
prop.storeToXML(fos, fileName);
System.out.println(fileName + ".xml 파일 생성 완료");
}catch(Exception e) {
e.printStackTrace();
}
}
}
- LoadeXMLFile Class
package edu.kh.jdbc.common;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
public class LoadeXMLFile {
public static void main(String[] args) {
try {
Properties prop = new Properties();
FileInputStream fis = new FileInputStream("test-query.xml");
prop.loadFromXML(fis);
System.out.println(prop);
String driver = prop.getProperty("driver");
String url = prop.getProperty("url");
String user = prop.getProperty("user");
String password = prop.getProperty("password");
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
System.out.println(conn);
}catch(Exception e ) {
e.printStackTrace();
}
}
}
- JDBCTemplete Class
package edu.kh.jdbc.common;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;
public class JDBCTemplete {
private static Connection conn = null;
public static Connection getConnection() {
try {
if(conn == null || conn.isClosed()) {
Properties prop = new Properties();
prop.loadFromXML( new FileInputStream("driver.xml") );
String driver = prop.getProperty("driver");
String url = prop.getProperty("url");
String user = prop.getProperty("user");
String password = prop.getProperty("password");
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
conn.setAutoCommit(false);
}
}catch(Exception e) {
System.err.println("[Connection 생성 중 예외 발생]");
e.printStackTrace();
}
return conn;
}
public static void close(Connection conn){
try {
if(conn != null && !conn.isClosed()) conn.close();
}catch(Exception e) {
e.printStackTrace();
}
}
public static void close(Statement stmt) {
try {
if(stmt != null && !stmt.isClosed()) stmt.close();
}catch(Exception e) {
e.printStackTrace();
}
}
public static void close(ResultSet rs) {
try {
if(rs != null && !rs.isClosed()) rs.close();
}catch(Exception e) {
e.printStackTrace();
}
}
public static void commit(Connection conn) {
try {
if(conn != null && !conn.isClosed()) conn.commit();
}catch(Exception e) {
e.printStackTrace();
}
}
public static void rollback(Connection conn) {
try {
if(conn != null && !conn.isClosed()) conn.rollback();
}catch(Exception e) {
e.printStackTrace();
}
}
}
- driver.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>driver</comment>
<entry key= "driver">oracle.jdbc.driver.OracleDriver</entry>
<entry key = "url">jdbc:oracle:thin:@localhost:1521:XE</entry>
<entry key = "user">kh</entry>
<entry key = "password">kh1234</entry>
</properties>
- test-query.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>test-query</comment>
<entry key = "insert">
INSERT INTO TB_TEST
VALUES(?, ?, ?)
</entry>
</properties>
5. insert 실습예제
- TestVO Clas
package edu.kh.jdbc.model.vo;
public class TestVO {
private int testNo;
private String testTitle;
private String testContent;
public TestVO() {}
public TestVO(int testNo, String testTitle, String testContent) {
this.testNo = testNo;
this.testTitle = testTitle;
this.testContent = testContent;
}
public int getTestNo() {
return testNo;
}
public void setTestNo(int testNo) {
this.testNo = testNo;
}
public String getTestTitle() {
return testTitle;
}
public void setTestTitle(String testTitle) {
this.testTitle = testTitle;
}
public String getTestContent() {
return testContent;
}
public void setTestContent(String testContent) {
this.testContent = testContent;
}
@Override
public String toString() {
return "TestVO [testNo=" + testNo + ", testTitle=" + testTitle + ", testContent=" + testContent + "]";
}
}
- Run Clas
package edu.kh.jdbc.run;
import java.sql.SQLException;
import edu.kh.jdbc.model.service.TestService;
import edu.kh.jdbc.model.vo.TestVO;
public class Run {
public static void main(String[] args) {
TestService service = new TestService();
TestVO vo1 = new TestVO(1, "제목1", "내용1");
try {
int result = service.insert(vo1);
if(result > 0) {
System.out.println("insert 성공");
}else {
System.out.println("insert 실패");
}
}catch(SQLException e) {
System.out.println("SQL수행 중 오류 발생");
e.printStackTrace();
}
}
}
- Run2 Clas
package edu.kh.jdbc.run;
import edu.kh.jdbc.model.service.TestService;
import edu.kh.jdbc.model.vo.TestVO;
public class Run2 {
public static void main(String[] args) {
TestService service = new TestService();
TestVO vo1 = new TestVO(70, "제목70", "내용70");
TestVO vo2 = new TestVO(80, "제목80", "내용80");
TestVO vo3 = new TestVO(90, "제목90", "내용90");
try {
int result = service.insert(vo1, vo2, vo3);
if(result > 0) {
System.out.println("insert 성공");
}else {
System.out.println("insert 실패");
}
}catch(Exception e) {
e.printStackTrace();
}
}
}
- TestService Clas
package edu.kh.jdbc.model.service;
import java.sql.Connection;
import java.sql.SQLException;
import static edu.kh.jdbc.common.JDBCTemplete.*;
import edu.kh.jdbc.model.dao.TestDAO;
import edu.kh.jdbc.model.vo.TestVO;
public class TestService {
private TestDAO dao = new TestDAO();
public int insert(TestVO vo1) throws SQLException {
Connection conn = getConnection();
int result = dao.insert(conn, vo1);
if(result > 0) commit(conn);
else rollback(conn);
close(conn);
return result;
}
public int insert(TestVO vo1, TestVO vo2, TestVO vo3) {
Connection conn = getConnection();
int result = 0;
try {
int res1 = dao.insert(conn, vo1);
int res2 = dao.insert(conn, vo2);
int res3 = dao.insert(conn, vo3);
if(res1 + res2 + res3 == 3) {
commit(conn);
result = 1;
}else {
rollback(conn);
}
}catch(Exception e ) {
e.printStackTrace();
}
return result;
}
}
- TestDAO Clas
package edu.kh.jdbc.model.dao;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import edu.kh.jdbc.model.vo.TestVO;
import static edu.kh.jdbc.common.JDBCTemplete.*;
public class TestDAO {
private Statement stmt;
private PreparedStatement pstmt;
private ResultSet rs;
private Properties prop;
public TestDAO() {
try {
prop = new Properties();
prop.loadFromXML(new FileInputStream("test-query.xml"));
}catch(Exception e ) {
e.printStackTrace();
}
}
public int insert(Connection conn, TestVO vo1) throws SQLException{
int result = 0;
try {
String sql = prop.getProperty("insert");
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, vo1.getTestNo());
pstmt.setString(2, vo1.getTestTitle());
pstmt.setString(3, vo1.getTestContent());
result = pstmt.executeUpdate();
}finally {
close(pstmt);
}
return result;
}
}