๐ป
Java ํ๋ก๊ทธ๋๋ฐ - jdbc ์ฐ๊ฒฐ ๋ณธ๋ฌธ
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() ์ฌ์ฉ
์์ 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();
}
}
}
}
'KITRI > JAVA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Java ํ๋ก๊ทธ๋๋ฐ - ์ฑํ ํ๋ก๊ทธ๋จ (1) | 2020.05.25 |
---|---|
Java ํ๋ก๊ทธ๋๋ฐ - ๋คํธ์ํฌ ํต์ (0) | 2020.05.25 |
Java ํ๋ก๊ทธ๋๋ฐ - ์ค๋ ๋(Thread) (0) | 2020.05.20 |
Java ํ๋ก๊ทธ๋๋ฐ - byte ์ ์ถ๋ ฅ, char์ ์ถ๋ ฅ (0) | 2020.05.20 |
Java ํ๋ก๊ทธ๋๋ฐ - ์์ธ์ฒ๋ฆฌ(Exception) (0) | 2020.05.18 |