์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- ์๊ณ ๋ฆฌ์ฆ
- ๋ฐฑ์ค
- data communication
- ubuntu
- ํ์ด์ฌ ์ฑ๋ฆฐ์ง
- ์ฐ๋ถํฌ
- Reversing
- ๋ฐฑ์ค ๋ฌ์คํธ
- ์ด์์ฒด์
- ์ค๋ผํดDB
- ๋ฐ์ดํฐ๋ฒ ์ด์ค
- Python challenge
- ํ์ด์ฌ
- Database
- java
- Rust
- ๋ฌ์คํธ ์์
- Operating System
- C
- ๋ฌ์คํธ ํ๋ก๊ทธ๋๋ฐ ๊ณต์ ๊ฐ์ด๋
- ์๋ฐ ๊ฐ๋
- ์๋ฐ ๊ธฐ์ด
- OS
- ๋ฌ์คํธ
- ํ์ด์ฌ ์ฒผ๋ฆฐ์ง
- ๋ฐ์ดํฐ ํต์
- ์๋ฐ
- Python
- ํ์ด์ฌ ์๊ณ ๋ฆฌ์ฆ
- ์ค๋ผํด
Archives
- Today
- Total
IT’s Portfolio
[Java] Lecture - Day 15 ๋ณธ๋ฌธ
728x90
๋ฐ์ํ
๐ฅ Basic Java - Day 15
๐๐ป [Chapter 16] JDBC ํ๋ก๊ทธ๋๋ฐ
๋ฐ์ดํฐ๋ฒ ์ด์ค
- ์ฌ๋ฌ ์์ฉ ์์คํ ๋ค์ ํตํฉ๋ ์ ๋ณด๋ค์ ์ ์ฅํ์ฌ ์ด์ํ ์ ์๋ ๊ณต์ฉ ๋ฐ์ดํฐ๋ค์ ์งํฉ
- ๋๊ท๋ชจ์ ๋ฐ์ดํฐ๋ฅผ ํจ์จ์ ์ผ๋ก ์ ์ฅ, ๊ฒ์, ๊ฐฑ์ ํ ์ ์๋๋ก ๋ฐ์ดํฐ๋ฅผ ๊ณ ๋๋ก ์กฐ์งํํ์ฌ ์ ์ฅ
DBMS
- DataBase Management System
- ๋ค์์ ์ฌ์ฉ์๋ค์ด ๋์์ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ์ฌ์ฉํ ์ ์๋๋ก ๊ด๋ฆฌ
- Oracle, SQL Server, MySQL, DB2, MariaDB, ...
- ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ข
๋ฅ
- ๊ด๊ณํ ๋ฐ์ดํฐ๋ฒ ์ด์ค
- ๋ฐ์ดํฐ๋ค์ด ๋ค์์ ํ ์ด๋ธ๋ก ๊ตฌ์ฑ
- ํ ์ด๋ธ์ ๊ฐ ํ์ ํ๋์ ๋ ์ฝ๋
- ๊ฐ ํ ์ด๋ธ์ ํค(key)์ ๊ฐ(value)์ ๊ด๊ณ๋ก ํํ
- ํค๋ ํ ์ด๋ธ์ ์ด ์ด๋ฆ
- ๊ฐ์ฒด ์งํฅ ๋ฐ์ดํฐ๋ฒ ์ด์ค
- ์ ๋ณด๋ฅผ ๊ฐ์ฒด์ ํํ๋ก ํํํ๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค
- ๊ฐ์ฒด ๋ชจ๋ธ์ ๊ทธ๋๋ก ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์ฉํ๋ฏ๋ก ์์ฉ ํ๋ก๊ทธ๋จ์ ๊ฐ์ฒด ๋ชจ๋ธ๊ณผ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๋ชจ๋ธ์ด ์๋ก ๋ถํฉํจ
- ๊ด๊ณํ ๋ฐ์ดํฐ๋ฒ ์ด์ค
SQL
- Structured Query Language
- ๊ด๊ณํ DBMS์์ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์คํค๋ง ์์ฑ, ์๋ฃ์ ๊ฒ์๏น๊ด๋ฆฌ๏น์์ , ๋ฐ์ดํฐ๋ฒ ์ด์ค ๊ฐ์ฒด ์ ๊ทผ ๊ด๋ฆฌ ๋ฑ์ ์ํด ๊ณ ์ํ ์ธ์ด
JDBC
- Java DataBase Connectivity
- ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ๋ค๋ฃจ๊ธฐ ์ํ Java API
- ๋ค์ํ DBMS์ ๋ํด ์ผ๊ด๋ ์๋ฐ API๋ฅผ ์ ๊ณตํ์ฌ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ, ๊ฒ์, ์์ , ๊ด๋ฆฌ ๋ฑ์ ํ ์ ์๊ฒ ํจ
- JDBC ๋๋ผ์ด๋ฒ ๋งค๋์
- ์๋ฐ API์์ ์ง์ํ๋ฉฐ DBMS์ ์ ๊ทผํ ์ ์๋ JDBC ๋๋ผ์ด๋ฒ ๋ก๋
- JDBC ๋๋ผ์ด๋ฒ
- DBMS๋ง๋ค ๊ณ ์ ํ JDBC ๋๋ผ์ด๋ฒ๋ฅผ ์ ๊ณต
- JDBC ๋๋ผ์ด๋ฒ์ DBMS๋ ๊ฐ ํ์ฌ์ ์ ์ฉ ํ๋กํ ์ฝ๋ก ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฒ๋ฆฌ
- MySQL Safe updates ๋ชจ๋
- ์ค์๋ก ๋ฐ์ดํฐ๋ฅผ update, delete ํ๋ ๊ฒ์ ๋ฐฉ์งํ๊ธฐ ์ํด ์ค์
- Safe updates ๋ชจ๋ ํด์ : [Edit - Preferences]
- ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฌ์ฐ๊ฒฐ
JDBC ํ๋ก๊ทธ๋๋ฐ
- JDBC API๋ฅผ ์ด์ฉํ์ฌ ๋ฐ์ดํฐ์ ์ถ๊ฐ, ์ญ์ , ์์ , ๊ฒ์ ๋ฑ์ ํ ์ ์๋ ์๋ฐ ์์ฉ ํ๋ก๊ทธ๋จ์ ์์ฑํ๋ ๊ฒ
- JDBC API(java.sql package)
- DriverManager: ๋๋ผ์ด๋ฒ๋ฅผ ๋ก๋ํ๊ณ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ฐ๊ฒฐ
- Connection: ํน์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ์ฐ๊ฒฐ
- Statement: SQL๋ฌธ์ ์คํํด ์์ฑ๋ ๊ฒฐ๊ณผ ๋ฐํ
- PreparedStatement: ์ฌ์ ์ ์ปดํ์ผ๋ SQL๋ฌธ์ ์คํ
- ResultSet: SQL์ Select๋ฌธ์ ์คํ ๊ฒฐ๊ณผ๋ฅผ ์ป์ด์ฌ ๋ ์ฌ์ฉ
JDBC ํ๋ก๊ทธ๋๋ฐ ์์
- JDBC ๋๋ผ์ด๋ฒ๋ฅผ ๋ก๋ฉํ์ฌ ์ธ์คํด์ค ์์ฑ
Class.forName("driver_name");
- DBMS์ ๋ํ ์ฐ๊ฒฐ ์์ฑ
Connection conn = DriverManager.getConnection("url", "account", "pw");
- Statement ์์ฑ
Statement stmt = conn.createStatement();
- ์ง์๋ฌธ ์คํ: Select๋ฌธ์ผ ๋, ResultSet์ผ๋ก ๊ฒฐ๊ณผ๋ฅผ ๋ฐ์
ResultSet rs = stmt.executeQuery("select_query");
- ResultSet ํด์ง
rs.close();
- Statement ํด์ง
stmt.close();
- ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ ํด์ง
conn.close();
๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ
- Class.forName("driver_name")
- ์ง์ JDBC ๋๋ผ์ด๋ฒ๋ฅผ ๋ฐํ์์ ๋ฉ๋ชจ๋ฆฌ์ ์ ์ฌ
- MySQL JDBC ๋๋ผ์ด๋ฒ ์ด๋ฆ
- version 8.0 ์ด์: com.mysql.cj.jdbc.Driver
- version 5.x ์ดํ: com.mysql.jdbc.Driver
- ClassNotFoundException ์ค๋ฅ์ ๋ํ ์ฒ๋ฆฌ๊ฐ ํ์ํจ
- DriverManagement.getConnection("url", "account", "pw")
- DriverManager: JDBC ๋๋ผ์ด๋ฒ๋ฅผ ๊ด๋ฆฌํ๋ ํด๋์ค
- url ํ์
jdbc:mysql://[hostname][:port]/dbname[?param1=value1][¶m2=value2]...
- serverTimezone=UTC
- useUnicode=true
- characterEncoding=euc-kr
// DB ์ฐ๊ฒฐ์ ํ์ํ ์ ๋ณด๋ค ์์ ์ฒ๋ฆฌ
public class DBConstant {
public static final String DRIVER_NAME = "com.mysql.cj.jdbc.Driver";
public static String URL = "jdbc:mysql://localhost:3306/sampledb";
public static String ID = "root";
public static String PWD = "root";
}
import java.sql.*;
public class DBConnectExam01 {
public static void main(String[] args) {
Connection conn = null;
try {
Class.forName(DBConstant.DRIVER_NAME); // MySQL ๋๋ผ์ด๋ฒ ๋ก๋
conn = DriverManager.getConnection(DBConstant.URL, DBConstant.ID, DBConstant.PWD); // JDBC ์ฐ๊ฒฐ
System.out.println("DB Connection is Done...");
} catch(ClassNotFoundException | SQLException e) {
System.out.println(e.toString());
} finally {
if(conn != null) {
try {
conn.close();
} catch(SQLException e) {
e.printStackTrace();
}
}
}
}
}
SQL ์คํ
- Connection ๊ฐ์ฒด๋ฅผ ์ด์ฉํด์ SQL๋ฌธ์ ์คํํ๊ณ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํ๋ฐ์ ์ ์๋ ๊ฐ์ฒด
- Statement
- ๋จ์ํ sql ๋ฌธ์ฅ์ ๋ณด๋ผ ๋ ์ฌ์ฉ
- ์ฑ๋ฅ๊ณผ ํจ์จ์ฑ์ด ๋ฎ์
Statement stmt = conn.createStatement();
- PreparedStatement
- ๋ฐ๋ณต๋๋ sql ๋ฌธ์ฅ ์ํ์ ํจ์จ์
- ๋ฏธ๋ฆฌ ์ปดํ์ผ๋ sql๋ฌธ์ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ Statement์ ๋นํด ์คํ ์๋๊ฐ ๋น ๋ฆ
- sql ํ์ ๋ฏธ๋ฆฌ ์ ํด๋๊ณ , ๋์ค์ ๊ฐ์ ์ง์ ํ๋ ๋ฐฉ์
PreparedStatement pstmt = conn.prepareStatement(SQL);
- Statement
- Statement ๊ฐ์ฒด์์ ์ ๊ณตํ๋ ๋ฉ์๋๋ก ์ง์๋ฌธ ์คํ
- ResultSet executeQuery(SQL)
- DB์ ๊ตฌ์กฐ์ ํ ์ด๋ธ์ ๋ด์ฉ์ ์ํฅ์ ๋ฏธ์น์ง ์๋ ์ง์์ ์ ํฉ
- select๋ฌธ
ResultSet rs = stmt.executeQuery("select_query");
- int executeUpdate(SQL)
- DB์ ๊ตฌ์กฐ์ ํ ์ด๋ธ์ ๋ด์ฉ์ ์ํฅ์ ๋ฏธ์น๋ ์ง์์ ์ ํฉ
- create, drop, insert, delete, update๋ฌธ
- create, drop์ ๋ฐํ๊ฐ: 0
- insert, delete, update์ ๋ฐํ๊ฐ: ์ง์๋ฌธ ์คํ์ผ๋ก ์ํฅ์ ๋ฐ์ ํ์ ์
int rowCount = stmt.executeUpdate("query");
- ResultSet executeQuery(SQL)
- ResultSet
- ํ
์ด๋ธ ํํ์ ๊ฒฐ๊ณผ๋ฅผ ์ถ์ํํ ์ธํฐํ์ด์ค
- BOF(Begin Of File, Before the first Row)
- ํ
์ด๋ธ ๊ฐ:
rs.next()
๋ก ํ ์ด๋ - EOF(End Of File, After the last Row)
- ํ์ฌ ๋ฐ์ดํฐ์ ํ(๋ ์ฝ๋ ์์น)์ ๊ฐ๋ฆฌํค๋ ์ปค์๋ฅผ ๊ด๋ฆฌ
- ์ด๊ธฐ ๊ฐ์ ์ฒซ ๋ฒ์งธ ํ ์ด์ ์ ๊ฐ๋ฆฌํด
- ์ ๊ณต ๋ฉ์๋
boolean first()
: ์ปค์ ์ฒซ ๋ฒ์งธ ํ์ผ๋ก ์ด๋boolean last()
: ์ปค์ ๋ง์ง๋ง ํ์ผ๋ก ์ด๋boolean next()
: ์ปค์ ๋ค์ ํ์ผ๋ก ์ด๋boolean previous()
: ์ปค์ ์ด์ ํ์ผ๋ก ์ด๋boolean absolute(int row)
: ์ปค์ ์ง์ ๋ ํ row๋ก ์ด๋boolean isFirst()
: ์ฒซ ๋ฒ์งธ ํ์ด๋ฉด true ๋ฐํboolean isLast()
: ๋ง์ง๋ง ํ์ด๋ฉด true ๋ฐํvoid close()
: ๊ฐ์ฒด์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ JDBC ๋ฆฌ์์ค ์ฆ์ ๋ฐํXxx getXxx(String columnLable)
: ํ์ฌ ํ์์ ์ง์ ๋ ์ด ์ด๋ฆ์ ํด๋นํ๋ ๋ฐ์ดํฐ ๋ฐํXxx getXxx(int columnIndex)
: ํ์ฌ ํ์์ ์ง์ ๋ ์ด ์ธ๋ฑ์ค์ ํด๋นํ๋ ๋ฐ์ดํฐ ๋ฐํ
- ํ
์ด๋ธ ํํ์ ๊ฒฐ๊ณผ๋ฅผ ์ถ์ํํ ์ธํฐํ์ด์ค
import java.sql.*;
public class DBConnectExam02 {
public static void ResultSetPrint(ResultSet rs) throws SQLException {
while(rs.next()) {
System.out.print(rs.getString("id"));
System.out.print("\t" + rs.getString("name"));
System.out.println("\t" + rs.getString("dept"));
}
}
public static void main(String[] args) {
Connection conn = null;
ResultSet rs = null;
Statement stmt = null;
try {
Class.forName(DBConstant.DRIVER_NAME);
conn = DriverManager.getConnection(DBConstant.URL, DBConstant.ID, DBConstant.PWD);
System.out.println("DB Connection is Done...");
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from student");
ResultSetPrint(rs);
System.out.println();
rs = stmt.executeQuery("select * from student where name='์ด๊ธฐ์'");
ResultSetPrint(rs);
} catch(ClassNotFoundException | SQLException e) {
System.out.println(e.toString());
} finally {
if(rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(stmt != null) {
try {
stmt.close();
} catch(SQLException e) {
e.printStackTrace();
}
}
if(conn != null) {
try {
conn.close();
} catch(SQLException e) {
System.out.println(e.toString());
e.printStackTrace();
}
}
}
}
}
- Statement๋ฅผ ์ฌ์ฉํ ๋ฐ์ดํฐ ๊ฒ์
import java.sql.*;
public class DBConnectExam03 {
public static void ObjectClose(Connection conn, Statement stmt, ResultSet rs) {
if(rs != null) {
try {
rs.close();
} catch(SQLException e) {
e.printStackTrace();
}
}
if(stmt != null) {
try {
stmt.close();
} catch(SQLException e) {
e.printStackTrace();
}
}
if(conn != null) {
try {
conn.close();
} catch(SQLException e) {
e.printStackTrace();
}
}
}
public static void ResultSetPrint(ResultSet rs) throws SQLException {
while(rs.next()) {
System.out.print(rs.getString("id"));
System.out.print("\t" + rs.getString("name"));
System.out.println("\t" + rs.getString("dept"));
}
}
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName(DBConstant.DRIVER_NAME);
conn = DriverManager.getConnection(DBConstant.URL, DBConstant.ID, DBConstant.PWD);
System.out.println("DB Connection is Done...");
stmt = conn.createStatement();
// insert
int rows = stmt.executeUpdate("insert into student (id, name, dept) " +
"values('0893012', '์๋ฌด๊ฐ', '์ปดํจํฐ๊ณตํ');");
if(rows>0) {
System.out.println("insert success");
rs = stmt.executeQuery("select * from student");
ResultSetPrint(rs);
} else System.out.println("insert fail");
// update
rows = stmt.executeUpdate("update student set id='0189011' where name='์๋ฌด๊ฐ'");
if(rows>0) {
System.out.println("update success");
rs = stmt.executeQuery("select * from student");
ResultSetPrint(rs);
} else System.out.println("update fail");
// delete
rows = stmt.executeUpdate("delete from student where name='์๋ฌด๊ฐ'");
if(rows>0) {
System.out.println("delete success");
rs = stmt.executeQuery("select * from student");
ResultSetPrint(rs);
} else System.out.println("delete fail");
} catch(ClassNotFoundException | SQLException e) {
System.out.println(e.toString());
e.printStackTrace();
} finally {
ObjectClose(conn, stmt, rs);
}
}
}
- Statement๋ฅผ ์ฌ์ฉํ ๋ฐ์ดํฐ ๋ณ๊ฒฝ
import java.sql.*;
import java.util.Scanner;
public class DBConnectExam04 {
public static void ObjectClose(Connection conn, PreparedStatement pstmt, ResultSet rs) {
if(rs != null) {
try {
rs.close();
} catch(SQLException e) {
e.printStackTrace();
}
}
if(pstmt != null) {
try {
pstmt.close();
} catch(SQLException e) {
e.printStackTrace();
}
}
if(conn != null) {
try {
conn.close();
} catch(SQLException e) {
e.printStackTrace();
}
}
}
public static void ResultSetPrint(ResultSet rs) throws SQLException {
while(rs.next()) {
System.out.print(rs.getString("id"));
System.out.print("\t" + rs.getString("name"));
System.out.println("\t" + rs.getString("dept"));
}
}
public static void main(String[] args) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
Scanner sc = new Scanner(System.in);
try {
Class.forName(DBConstant.DRIVER_NAME);
conn = DriverManager.getConnection(DBConstant.URL, DBConstant.ID, DBConstant.PWD);
String sql = "select * from student where name=?";
System.out.print("Search name: ");
String name = sc.next();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
rs = pstmt.executeQuery();
ResultSetPrint(rs);
} catch(ClassNotFoundException | SQLException e) {
System.out.println(e.toString());
e.printStackTrace();
} finally {
ObjectClose(conn, pstmt, rs);
sc.close();
}
}
}
- PreparedStatement๋ฅผ ์ฌ์ฉํ ๋ฐ์ดํฐ ๊ฒ์
import java.sql.*;
import java.util.Scanner;
public class DBConnectExam05 {
public static void ObjectClose(Connection conn, PreparedStatement pstmt, ResultSet rs) {
if(rs != null) {
try {
rs.close();
} catch(SQLException e) {
e.printStackTrace();
}
}
if(pstmt != null) {
try {
pstmt.close();
} catch(SQLException e) {
e.printStackTrace();
}
}
if(conn != null) {
try {
conn.close();
} catch(SQLException e) {
e.printStackTrace();
}
}
}
public static void ResultSetPrint(ResultSet rs) throws SQLException {
while(rs.next()) {
System.out.print(rs.getString("id"));
System.out.print("\t" + rs.getString("name"));
System.out.println("\t" + rs.getString("dept"));
}
}
public static void main(String[] args) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
Class.forName(DBConstant.DRIVER_NAME);
conn = DriverManager.getConnection(DBConstant.URL, DBConstant.ID, DBConstant.PWD);
String[] info = {"์ด์ฑ๊ณ", "0474012", "๊ตญ๋ฌธ๊ณผ"};
String sql = "insert into student(name, id, dept) " +
"values(?, ?, ?)";
pstmt = conn.prepareStatement(sql);
for(int i=0; i<info.length; i++) {
pstmt.setString(i+1, info[i]);
}
int rows = pstmt.executeUpdate();
if(rows>0) {
System.out.println("insert success");
rs = pstmt.executeQuery("select * from student");
ResultSetPrint(rs);
} else System.out.println("insert fail");
} catch(ClassNotFoundException | SQLException e) {
System.out.println(e.toString());
e.printStackTrace();
} finally {
ObjectClose(conn, pstmt, rs);
}
}
}
- PreparedStatement๋ฅผ ์ฌ์ฉํ ๋ฐ์ดํฐ ๋ณ๊ฒฝ
728x90
๋ฐ์ํ
'Development Study > Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] Lecture - Day 17 (1) | 2024.06.06 |
---|---|
[Java] Lecture - Day 16 (1) | 2024.05.17 |
[Java] Lecture - Day 14 (0) | 2024.04.05 |
[Java] Lecture - Day 13 (1) | 2024.03.30 |
[Java] Lecture - Day 12 (0) | 2024.03.22 |
Comments