๐Ÿ’ป

Java ํ”„๋กœ๊ทธ๋ž˜๋ฐ - jdbc ์—ฐ๊ฒฐ ๋ณธ๋ฌธ

KITRI/JAVA

Java ํ”„๋กœ๊ทธ๋ž˜๋ฐ - jdbc ์—ฐ๊ฒฐ

๋˜ํšจ๋‹ˆ 2020. 5. 25. 15:37

 

1. Window > Preferences > Java > Build Path > User Library 

New ์„ ํƒํ•ด์„œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ช… ์ž…๋ ฅ

Add External JARS ์„ ํƒํ•ด์„œ C:\oraclexe\app\oracle\product\11.2.0\server\jdbc\lib\ojdbc6 

 

 

2. ํ”„๋กœ์ ํŠธ ํด๋”์—์„œ ์˜ค๋ฅธ์ชฝ ๋งˆ์šฐ์Šค > Build Path > Add Libraries... > User Library >

์•„๊นŒ ์ถ”๊ฐ€ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ ํƒํ•˜๊ณ  Finish

 

 

 

 

 

 

 

 

์˜ˆ์ œ 1) SELECT ๋ถ€์„œ ํ…Œ์ด๋ธ” ์ •๋ณด๋ฅผ ์กฐํšŒํ•˜๊ธฐ

 

package db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class Ex01 {
	public static void main(String[] args) {
		// 1๋‹จ๊ณ„ : ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ
		try {
			String className = "oracle.jdbc.driver.OracleDriver";
			Class.forName(className);
		}catch(ClassNotFoundException e) {
			e.printStackTrace();
		}
		
		// 2๋‹จ๊ณ„ : ์—ฐ๊ฒฐ์„ ๊ด€๋ฆฌํ•˜๋Š” ๊ฐ์ฒด ์ƒ์„ฑ
		try {
			String url = "jdbc:oracle:thin:@localhost:1521:xe"; 
					   /*"jdbc:oracle:thin:@"192.168.0.23":1521:xe"; ๋กœ ์„ ํƒ ๊ฐ€๋Šฅ*/
			String id = "java";
			String pass = "1234";
			
			Connection conn = DriverManager.getConnection(url, id, pass);
			
			// 3๋‹จ๊ณ„ : ์ž‘์—…๊ด€๋ฆฌ(Query)
			PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM dept");
			
			// 4๋‹จ๊ณ„ : ๊ฒฐ๊ณผ์ฒ˜๋ฆฌ
			ResultSet rs = pstmt.executeQuery();
			
			while(rs.next()) {
				int deptNo = rs.getInt("deptno");
				String deptName = rs.getString("dname");
				String loc = rs.getString("loc");
				
				System.out.println(deptNo + "\t" + deptName + "\t" + loc);
			}
			
			// 5๋‹จ๊ณ„ : ์ข…๋ฃŒ
			rs.close();
			pstmt.close();
			conn.close();
			
		}catch(Exception e) {
			e.printStackTrace();
		}		
	}
}

 

[์ฐธ๊ณ ] PreparedStatement(๋™์ ์ฟผ๋ฆฌ) vs Statement(์ •์ ์ฟผ๋ฆฌ) ์˜ ์ฐจ์ด

 

์ •์  ์ฟผ๋ฆฌ

- ๊ณ ์ •๋œ SQL ํ˜•ํƒœ๋ฅผ ๋งŒ๋“ค์–ด์„œ ์‚ฌ์šฉํ•œ๋‹ค.

- ์™„์ „ํ•œ ์ฟผ๋ฆฌ๋ฌธ์ผ ๋•Œ ์‚ฌ์šฉ

- ์ตœ๊ทผ์—๋Š” ์ž˜ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.

 

๋™์  ์ฟผ๋ฆฌ

- ์ž…๋ ฅ ๊ฐ’์ด๋‚˜ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ์ถ”๊ฐ€ํ•ด ์‹คํ–‰ํ•  ์ฟผ๋ฆฌ๋ฌธ์„ ๋ฌธ์ž์—ด๋กœ SQL๋ณ€์ˆ˜์— ์ž‘์„ฑํ•ด ๋‹ด์•„ ๋งŒ๋“  ํ›„์— ์‹คํ–‰ํ•ด์ฃผ๋Š” ๊ฒƒ. 
- ๋ถˆ์™„์ „ํ•œ ์ฟผ๋ฆฌ๋ฌธ์—์„œ ์ธ์ž๊ฐ’์„ ๊ณ„์† ์‹ค์–ด์„œ ์‹คํ–‰ํ•  ๋•Œ ์‚ฌ์šฉ

 


์˜ˆ์ œ 2) INSERT ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์ž…๋ ฅ๋ฐ›์•„ personํ…Œ์ด๋ธ”์— ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€ํ•˜๊ธฐ

 

๋ฐ์ดํ„ฐ๊ฐ€ ์ž˜ ๋“ค์–ด๊ฐ„ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. 

package db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.Scanner;

public class Ex02 {
	public static void main(String[] args) {
		try {
			// 1๋‹จ๊ณ„ : ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ
			Class.forName("oracle.jdbc.driver.OracleDriver");
			
			// 2๋‹จ๊ณ„ : ์—ฐ๊ฒฐ์„ ๊ด€๋ฆฌํ•˜๋Š” ๊ฐ์ฒด ์ƒ์„ฑ
			String url = "jdbc:oracle:thin:@localhost:1521:xe";
			String id = "java";
			String pass = "1234";
			
			Connection conn = DriverManager.getConnection(url, id, pass);
			
			Scanner sc = new Scanner(System.in);
			System.out.println("๋ฒˆํ˜ธ:");
			int bunho = sc.nextInt();
			
			System.out.println("์ด๋ฆ„:");
			String name = sc.next();

			System.out.println("ํ‚ค:");
			float ki = sc.nextFloat();
			
			//์ •์ ์ฟผ๋ฆฌ
			
			/*String query = "insert into person values('"+ bunho +"','"+name+"'," +ki +"')";
			Statement stmt = conn.createStatement();
			stmt.executeUpdate(query);
			*/
			
			//๋™์ ์ฟผ๋ฆฌ
			String sql = "insert into person values(?,?,?)";
			PreparedStatement pstmt = conn.prepareStatement(sql);
			
			pstmt.setInt(1, bunho);
			pstmt.setString(2, name);
			pstmt.setFloat(3, ki);
			
			int check = pstmt.executeUpdate();
			if(check > 0) System.out.println("๋ฐ์ดํ„ฐ ์ถ”๊ฐ€ ์„ฑ๊ณต");
			
			pstmt.close();
			conn.close();
			sc.close();
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
}

 

 

 


 

SELECT

=> excuteQuery() , ResultSet ์‚ฌ์šฉ

UPDATE, INSERT, DELETE

=> executeUpdate() ์‚ฌ์šฉ

https://docs.oracle.com/en/java/javase/14/docs/api/java.sql/java/sql/PreparedStatement.html#executeQuery()

 

PreparedStatement (Java SE 14 & JDK 14)

All Superinterfaces: AutoCloseable, Statement, Wrapper All Known Subinterfaces: CallableStatement public interface PreparedStatement extends Statement An object that represents a precompiled SQL statement. A SQL statement is precompiled and stored in a Pre

docs.oracle.com

 

์˜ˆ์ œ 3) UPDATE ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ํ•ด๋‹น ๋ฒˆํ˜ธ Person์˜ ํ‚ค๋ฅผ ์ˆ˜์ •ํ•œ๋‹ค.

 

package db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * @Author : ๊น€์†Œํ˜„
 * @Date   : 2020. 5. 26.
 * @Description : Person Table : Update
 */
public class Ex03 {
	public static void main(String[] args) {
		//๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
		} catch(ClassNotFoundException e){
			e.printStackTrace();
		}
		
		//์—ฐ๊ฒฐ๊ด€๋ฆฌ ๊ฐ์ฒด Connection ๊ฐ์ฒด ์ƒ์„ฑ
		Connection conn = null;
		try {
			String url = "jdbc:oracle:thin:@localhost:1521:xe";
			String id = "java";
			String pass = "1234";
			
			conn = DriverManager.getConnection(url, id, pass);
		} catch(SQLException e) {
			e.printStackTrace();
		}
		
		//์ž‘์—…๊ด€๋ฆฌ - PreparedStatement, ๊ฒฐ๊ณผ์ฒ˜๋ฆฌ - ResultSet/int, ์ข…๋ฃŒ - ๊ฐ๊ฐ์˜ close()
		try {
			Scanner sc = new Scanner(System.in);
			System.out.print("๋ฒˆํ˜ธ:");
			int bunho = sc.nextInt();
			
			String sql = "select * from person where bunho=?";
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, bunho);
			
			ResultSet rs = pstmt.executeQuery(); 
			
			if(rs.next()) { //๋‘ ์ค„์ด์ƒ๋ฉด while๋ฌธ  
				System.out.println(rs.getInt("bunho") + "\t" + rs.getString("name") +"\t" + rs.getFloat("ki"));
				System.out.println("ํ‚ค ์ˆ˜์ •:");
				float ki = sc.nextFloat();
				
				String sqlUp = "update person set ki=? where bunho=?";
				pstmt = conn.prepareStatement(sqlUp);
				pstmt.setFloat(1, ki);
				pstmt.setInt(2, bunho);
				
				int check = pstmt.executeUpdate();
				if(check > 0) 
					System.out.println("์ˆ˜์ •์ด ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
				else 
					System.out.println("์ˆ˜์ •์— ์‹คํŒจํ•˜์˜€์Šต๋‹ˆ๋‹ค.");
			} else {
				System.out.println("ํ•ด๋‹น ๋ฒˆํ˜ธ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
			}
			rs.close();
			pstmt.close();
			conn.close();
		} catch(SQLException e) {
			e.printStackTrace();
		}
	}
}

 

 

์˜ˆ์ œ 4) DELETE ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ํ•ด๋‹น ๋ฒˆํ˜ธ Person ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œํ•œ๋‹ค.

 

package db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class Ex04 {
	public static void main(String[] args) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		Scanner sc = null;
		
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
		}catch(ClassNotFoundException e) {
			e.printStackTrace();
		}
		
		try {
			String url = "jdbc:oracle:thin:@localhost:1521:xe";
			String id = "java";
			String pass = "1234";
			
			conn = DriverManager.getConnection(url, id, pass);
		}catch(SQLException e) {
			e.printStackTrace();
		}
		
		try {
			sc = new Scanner(System.in);
			System.out.print("๋ฒˆํ˜ธ:");
			int bunho = sc.nextInt();
			
			String sql = "select * from person where bunho = ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, bunho);
			rs = pstmt.executeQuery();
			
			if(rs.next()) {
				System.out.println("์ •๋ง๋กœ ์‚ญ์ œํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ? (Yes):");
				String value = sc.next();
				if(value.equalsIgnoreCase("YES")) {
					String sqlDel = "delete from person where bunho = ?";
					
					if(pstmt!=null)pstmt.close();
					pstmt = conn.prepareStatement(sqlDel);
					pstmt.setInt(1, bunho);
					
					int check = pstmt.executeUpdate();
					System.out.println("์‚ญ์ œ:" +check);
				}
			}else {
				System.out.println("ํ•ด๋‹น ๋ฒˆํ˜ธ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
			}
		}catch(SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if(sc!=null) sc.close();
				if(pstmt!=null) pstmt.close();
				if(rs!=null) rs.close();
				if(conn!=null) conn.close();
			} catch (Exception e2) {
				// TODO: handle exception
			}
		}
	}
}

 

 

 

์˜ˆ์ œ 5) MVC ํŒจํ„ด ์ด์šฉ ์€ํ–‰ ํ”„๋กœ๊ทธ๋žจ ๋งŒ๋“ค๊ธฐ

 

============Menu============
1. ๊ณ„์ขŒ๊ฐœ์„ค
2. ์ž…๊ธˆ
3. ์ถœ๊ธˆ
4. ์ž”์•ก์กฐํšŒ
5. ์ „์ฒด์ถœ๋ ฅ
6. ํ”„๋กœ๊ทธ๋žจ์ข…๋ฃŒ

 

 

Oracle SQL Developer ์—์„œ bank ํ…Œ์ด๋ธ” ์ƒ์„ฑํ•ด์ค€๋‹ค.

 

 

AccountDao.java

package bank;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Scanner;

import dbUtil.ConnectionProvider;
import dbUtil.JdbcUtil;

public class AccountDao {
	private Connection conn = null;
	private PreparedStatement pstmt = null;
	private ResultSet rs = null;
	private String sql = null;
	
	private Scanner sc = null;
	private AccountDto accountDto = null;
	
	public void makeAccount() { // 1.๊ณ„์ขŒ๊ฐœ์„ค
		sc = new Scanner(System.in);
		accountDto = new AccountDto();
		
		System.out.print("๊ณ„์ขŒ๋ฒˆํ˜ธ:");
		accountDto.setId(sc.next());
		
		System.out.print("์ด๋ฆ„:");
		accountDto.setName(sc.next());
		
		System.out.print("์ž…๊ธˆ์•ก:");
		accountDto.setBalance(sc.nextLong());
		
		System.out.println(accountDto.toString()); //๋ฐ์ดํ„ฐ ํ™•์ธ!
			
		try {
			Connection conn = ConnectionProvider.getConnection();
			sql = "insert into bank values(bank_num_seq.nextval,?,?,?)";
			
			pstmt = conn.prepareStatement(sql);
			
			pstmt.setString(1, accountDto.getId());
			pstmt.setString(2, accountDto.getName());
			pstmt.setLong(3, accountDto.getBalance());
			
			int check = pstmt.executeUpdate();
			System.out.println("insert result:" +check);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JdbcUtil.close(pstmt);
			JdbcUtil.close(conn);
		}
	}
	
	public void deposit() {	// 2.์ž…๊ธˆ
		accountDto = new AccountDto();
		sc = new Scanner(System.in);
		
		System.out.print("๊ณ„์ขŒ๋ฒˆํ˜ธ:");
		accountDto.setId(sc.next());
		
		try {
			conn = ConnectionProvider.getConnection();
			sql = "select balance from bank where id = ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, accountDto.getId());
			rs = pstmt.executeQuery();
			
			if(rs.next()) {
				//System.out.println(rs.getLong("balance")); //ํ™•์ธ
				System.out.print("์ž…๊ธˆ์•ก:");
				long money = sc.nextLong();
				
				accountDto.setBalance(rs.getLong("balance") + money);
				JdbcUtil.close(pstmt);
				
				sql = "update bank set balance = ? where id = ?";
				pstmt = conn.prepareStatement(sql);
				pstmt.setLong(1, accountDto.getBalance());
				pstmt.setString(2, accountDto.getId());
				
				int check = pstmt.executeUpdate();
				System.out.println("์ž…๊ธˆ ํ™•์ธ:" + check);
				
			}else {
				System.out.println("๊ณ„์ขŒ๋ฒˆํ˜ธ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				JdbcUtil.close(rs);
				JdbcUtil.close(pstmt);
				JdbcUtil.close(conn);
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}
	
	public void withdraw() { // 3.์ถœ๊ธˆ
		accountDto = new AccountDto();
		sc = new Scanner(System.in);
		
		System.out.print("๊ณ„์ขŒ๋ฒˆํ˜ธ:");
		accountDto.setId(sc.next());
		
		System.out.print("์ถœ๊ธˆ์•ก:");
		long money = sc.nextLong();
		accountDto.setBalance(money);
		
		try {
			conn = ConnectionProvider.getConnection();
			sql = "select balance from bank where id = ? and balance >= ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, accountDto.getId());
			pstmt.setLong(2, accountDto.getBalance());
			
			rs = pstmt.executeQuery();
			if(rs.next()) {
				accountDto.setBalance(rs.getLong("balance") - money);
				JdbcUtil.close(pstmt);
				
				sql = "update bank set balance=? where id=?";
				pstmt = conn.prepareStatement(sql);
				pstmt.setLong(1, accountDto.getBalance());
				pstmt.setString(2, accountDto.getId());

				int check = pstmt.executeUpdate();
				System.out.println("์ถœ๊ธˆ ํ™•์ธ:" + check);
				
			}else {
				System.out.println("๊ณ„์ขŒ๋ฒˆํ˜ธ ๋˜๋Š” ์ž”์•ก์„ ํ™•์ธํ•˜์„ธ์š”.");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				JdbcUtil.close(rs);
				JdbcUtil.close(pstmt);
				JdbcUtil.close(conn);
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}
	
	public void inquire() {	// 4.์ž”์•ก์กฐํšŒ
		accountDto = new AccountDto();
		sc = new Scanner(System.in);
		
		System.out.print("๊ณ„์ขŒ๋ฒˆํ˜ธ:");
		accountDto.setId(sc.next());
		
		try {
			conn = ConnectionProvider.getConnection();

			sql = "select * from bank where id = ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, accountDto.getId());
			rs = pstmt.executeQuery();
			
			if(rs.next()) {
				accountDto.setNum(rs.getInt("num"));
				accountDto.setId(rs.getString("id"));
				accountDto.setName(rs.getString("name"));
				accountDto.setBalance(rs.getLong("balance"));

				System.out.println(accountDto.getNum() +"\t" + accountDto.getId() + "\t"
							+ accountDto.getName() + "\t" + accountDto.getBalance());
			}else {
				System.out.println("๊ณ„์ขŒ๋ฒˆํ˜ธ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
			}
		}catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				JdbcUtil.close(rs);
				JdbcUtil.close(pstmt);
				JdbcUtil.close(conn);
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}
	
	public void showData() {	// 5. ์ „์ฒด์ถœ๋ ฅ
		conn = ConnectionProvider.getConnection();
		ArrayList<AccountDto> list = new ArrayList<AccountDto>(); //๋ฐ์ดํ„ฐ๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ๋ฉด์€ ArrayListํ™œ์šฉ
		
		try {
			sql = "select * from bank order by num asc";
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			
			while(rs.next()) {
				accountDto = new AccountDto();
				accountDto.setNum(rs.getInt("number"));
				accountDto.setId(rs.getString("id"));
				accountDto.setName(rs.getString("name"));
				accountDto.setBalance(rs.getLong("balance"));
				
				list.add(accountDto);
			}
			
			for(int i=0; i<list.size(); i++) {
				AccountDto dto = list.get(i);
				System.out.println(dto.getNum() + "\t" +dto.getId() +"\t"+ 
								dto.getName() +"\t"+dto.getBalance());
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JdbcUtil.close(rs);
			JdbcUtil.close(pstmt);
			JdbcUtil.close(conn);
		}
		
	}
	
	
}

 

AccountDto.java

package bank;

/**
 * @Author : ๊น€์†Œํ˜„
 * @Date   : 2020. 5. 26.
 * @Description : 
 */

public class AccountDto {
	private int num;		//์ž๋™์ฆ๊ฐ€๋ฒˆํ˜ธ
	private String id;		//๊ณ„์ขŒ๋ฒˆํ˜ธ
	private String name;	//์ด๋ฆ„
	private long balance;	//์ž”์•ก
	
	public AccountDto() {}
	
	public AccountDto(int num, String id, String name, long balance) {
		this.num = num;
		this.id = id;
		this.name = name;
		this.balance = balance;
	}
	
	public int getNum() {
		return num;
	}
	
	public void setNum(int num) {
		this.num = num;
	}
	
	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 long getBalance() {
		return balance;
	}
	
	public void setBalance(long balance) {
		this.balance = balance;
	}
	
	@Override
	public String toString() {
		return "AccountDto [num=" + num + ", id=" + id + ", name=" + name + ", balance=" + balance + "]";
	}
}

 

AccountView.java

package bank;

import java.util.Scanner;

import dbUtil.DriverProvider;

public class AccountView {
	public static void main(String[] args) {
		DriverProvider dp = new DriverProvider();
		dp.getDriver();
		
		AccountDao jbank = new AccountDao();
		Scanner sc = new Scanner(System.in);
		
		while(true) {
			printMenu();
			
			System.out.print("์„ ํƒ:");
			int choice = sc.nextInt();
			
			switch(choice) {
			case 1 :
				jbank.makeAccount();
				break;
			case 2 : 
				jbank.deposit();
				break;
			case 3 :
				jbank.withdraw();
				break;
			case 4 :
				jbank.inquire();
				break;
			case 5 : 
				jbank.showData();
				break;
			case 6 : 
				System.out.println("์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.");
				if(sc!=null) sc.close();
				System.exit(0); //ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ
			}
		}
	}
	
	public static void printMenu() {
		System.out.println("============Menu============");
		System.out.println("1. ๊ณ„์ขŒ๊ฐœ์„ค");
		System.out.println("2. ์ž…๊ธˆ");
		System.out.println("3. ์ถœ๊ธˆ");
		System.out.println("4. ์ž”์•ก์กฐํšŒ");
		System.out.println("5. ์ „์ฒด์ถœ๋ ฅ");
		System.out.println("6. ํ”„๋กœ๊ทธ๋žจ์ข…๋ฃŒ");
		System.out.println();
	}
}

 

ConnectionProvider.java

package dbUtil;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectionProvider {
	
	public static Connection getConnection() {
		Connection conn = null;
		try {
			String url = "jdbc:oracle:thin:@localhost:1521:xe";
			String id = "java";
			String pass = "1234";
			
			conn = DriverManager.getConnection(url, id, pass);
		}catch(SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
}

 

 

DriverProvider.java

package dbUtil;

public class DriverProvider {
	public void getDriver() {
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
}

 

JdbcUtil.java

package dbUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JdbcUtil {
	
	public static void close(Connection conn) {
		if(conn!=null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public static void close(PreparedStatement pstmt) {
		if(pstmt!=null) {
			try {
				pstmt.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void close(ResultSet rs) {
		if(rs!=null) {
			try {
				rs.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}

 

 

๊ฒฐ๊ณผ ํ™•์ธ

 

๋ฐ˜์‘ํ˜•
Comments