본문 바로가기

JDBC

INSERT (기본 -> DBclose 수정)

결과물

 

 

DB에서 미리 생성 후 JAVA에서 입력해야 테이블에 행이 삽입 됨.

 

main

public class mainClass {

	public static void main(String[] args) {
		DBConnection.initConnection();
		
		//생성
		InsertTest it = new InsertTest();
		
		int count = it.insertData("4", "정수동", 26);	//return 값
		
		if (count == 1) {
			System.out.println("데이터가 " + count + "개 추가 되었습니다");
		}
	}
}

InsertTest

public class InsertTest {
	
	public InsertTest() {
//		try {
//			Class.forName("oracle.jdbc.driver.OracleDriver");
//			
//			System.out.println("Driver Loading Success");
//			
//			
//		} catch (ClassNotFoundException e) {
//
//			e.printStackTrace();
//		}
	}
	
	
/*	
	//정보 얻어오기 conn
	public Connection getConnection() {			
		
		Connection conn = null;		
		
		try {
			conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.7.40:1521:xe",
												"hr", "hr");	
			System.out.println("Oracle Connection Success");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
	*/
	//conn 상태 생성
	public int insertData(String id, String name, int age) {
		
		//Query -> String 대문자로
		String sql = " INSERT INTO USERTEST(ID, NAME, AGE, JOINDATE)"
				+ " VALUES('" + id + "', '" + name + "', " + age + ", SYSDATE)";
				//"" + "" 시,  작은 따옴표를 넣어줘야 함
		
		System.out.println("sql : " + sql);
		
		//Connection conn = this.getConnection();
		//위의 메서드 호출
		Connection conn = DBConnection.getConnection();
		Statement stat = null;
		//상태를 볼 수 있음.
		
		int count = 0;
		//몇개가 변경되었는지?
		
		try {
			stat = conn.createStatement();	//conn을 통해 url상태를 얻어와 stat에 대입
			
			count = stat.executeUpdate(sql);	//sql String대입, update는 리턴 값이 있음
			
			System.out.println("성공적으로 추가 되었습니다");
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {//해방
			
		/*
				try {
					if (stat != null) {		//if문을 try안으로
					stat.close();
					}
					if (conn != null) {
						conn.close();
					}
					
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				
			}
			*/
			DBClose.close(stat, conn, null);
		}
		return count;
		
	}
}

 

 

DBConnection

public class DBConnection {

	public static void initConnection() {
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			
			System.out.println("Driver Loading Success");
			
			
		} catch (ClassNotFoundException e) {

			e.printStackTrace();
		}
	}
	
	public static Connection getConnection() {			
		
		Connection conn = null;		
		
		try {
			conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.7.40:1521:xe",
												"hr", "hr");	
			System.out.println("Oracle Connection Success");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
}

 

DBClose

import javax.naming.spi.DirStateFactory.Result;

public class DBClose {

	public static void close(Statement stmt, Connection conn, ResultSet rs) {
		
			try {
				if (stmt != null) {
					stmt.close();
				}
				if (conn != null) {
					conn.close();
				}if (rs != null) {		//Select
					rs.close();
				}
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			
		}
	}
}

'JDBC' 카테고리의 다른 글

Delete  (0) 2020.06.30
Select  (0) 2020.06.30
Select 한개의 데이터  (0) 2020.06.30
Update  (0) 2020.06.30
JDBC 기본 연결  (0) 2020.06.30