[Day2] JSP/JDBC 2 [10/27]
--๋ณต์ต๋ฌธ์ --
1. ๋์ ์ฟผ๋ฆฌ๋ฅผ ์ฌ์ฉํ๋ ์ ์ฅ ํ๋ก์์ ์์ฑํ์ธ์. ( 5๋ถ )
ใฑ. ์ปค์๋ณ์ ์ฌ์ฉ
ใด. open ~ for ๋ฌธ ์ฌ์ฉ
ใท. ํ๋ก์์ ์์์ ์ถ๋ ฅ๊น์ง ํ์ธ์.
ใน. deptno ๋ฅผ ํ๋ผ๋ฏธํฐ๋ก ํด์ emp ํ ์ด๋ธ์ deptno, ename, hiredate ๋ฅผ ์ถ๋ ฅํ๋ ์ ์ฅ ํ๋ก์์ ์์ฑ.
CREATE OR REPLACE PROCEDURE up_emp
(
pdeptno emp.deptno%TYPE
)
IS
vsql varchar2(2000);
vempCursor SYS_REFCURSOR;
vdeptno emp.deptno%TYPE;
vename emp.ename%TYPE;
vhiredate emp.hiredate%TYPE;
BEGIN
vsql := 'SELECT deptno, ename, hiredate ';
vsql := vsql || 'FROM emp ';
vsql := vsql || 'WHERE deptno = :deptno ';
OPEN vempCursor FOR vsql USING pdeptno;
LOOP
FETCH vempCursor INTO vdeptno, vename, vhiredate;
EXIT WHEN vempCursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE( vdeptno || ', ' || vename || ', ' || vhiredate );
END LOOP;
CLOSE vempCursor;
--EXCEPTION
END;
EXEC up_emp(20);
2-1. JDBC ์ค๋ช ํ์ธ์.
- J D B C
- ์๋ฐ ํ์ค ์ธํฐํ์ด์ค : ์ถ์๋ฉ์๋, ์์ + s๋ฉ, d๋ฉ
- ์๋ฐ + DB ์ฐ๋ ๊ธฐ์
2-2. JDBC Driver ์ค๋ช ํ์ธ์.
- DBMS(์ค/M) ์ฐ๋ํ๊ธฐ ์ํด JDBC๋ฅผ ๊ตฌํํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ(๋๋ผ์ด๋ฒ)
- Oralce : JDBC Driver ๋ฒค๋(ํ์ฌ) ๋ค์ด ojdbc6.jar == > Oracle 11g xe
2-3. JDBC Driver [์ข ๋ฅ] ์ค๋ช ํ์ธ์.
- Type1 ODBC ๋ธ๋ฆฟ์ง X
- Type2 Native API C/C++ X
- Type3 ๋ฏธ๋ค์จ์ด ์๋ฒ X
- Type4 ์์ ์๋ฐ , ๊ฐ์ฅ ๋ง์ด ์ฌ์ฉํ๋ค. thin
3. JDBC๋ฅผ ์ฌ์ฉํด์ DB์ ์ฐ๊ฒฐ( Connect ) ํ๋ ์์์ ๋ํด ์ค๋ช ํ์ธ์.
1) JDBC ๋๋ผ์ด๋ฒ ๋ก๋ฉ : Class.forName()
2) Connecion ๊ฐ์ฒด ์ป์ด์ค๊ธฐ : DriverManager.getConnection ()
3) ํ์ํ ์กฐ์ ( CRUD )
4) Close
4. ์์ ์์์ ๋ง์ถฐ์ ์ค๋ผํด์๋ฒ์ scott/tiger ๋ก DB์ ์ฐ๊ฒฐํ๋ ์ ์ฒด ์ฝ๋ฉ์ main() ๋ฉ์๋์ ํ์๊ณ , ์ฐ๊ฒฐ๋ Connection ๊ฐ์ฒด ์ ๋ณด๋ฅผ ์ถ๋ ฅํ๊ณ Connection ๊ฐ์ฒด ์ฐ๊ฒฐ ์ํ๋ฅผ true, false๋ก ์ถ๋ ฅํ๋ ์ฝ๋ฉ์ ํ์ธ์.
import java.sql.Connection;
main(){
Connection conn = null;
String className = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:xe"; // SID xe
String user = "scott";
String password = "tiger";
Class.forName(className);
conn = DriverManager.getConnection (url, user, password);
Syso( conn );
Syso( conn.is์ํ );
conn.close();
ClassNotFoundException
SQLException
}
5. ๋ถ์๋ฒํธ๋ฅผ ์ ๋ ฅ๋ฐ์์ ํด๋น ๋ถ์์ ๋ชจ๋ ์ฌ์ ์ ๋ณด๋ฅผ ์ถ๋ ฅ
( ์กฐ๊ฑด :
ใฑ. ๋ชจ๋ ์ปฌ๋ผ ์ถ๋ ฅ
ใด. ์ด๋ฆ ์์ผ๋ก ์ค๋ฆ์ฐจ์์ ๋ ฌ
ใท. domain.EmpDTO ์ ์ธ
ใน. ArrayList ์ฌ์ฉํด์ EmpDTO ๊ฐ์ฒด ์ ์ฅ
ใ . printEmp() ๋ฉ์๋ ์ฌ์ฉํด์ ์ฌ์ ์ ๋ณด ์ถ๋ ฅ
ใ . ์ฌ์์ด ์์ ๊ฒฝ์ฐ "์ฌ์์ด ์กด์ฌํ์ง ์์ต๋๋ค."๋ผ๊ณ ์ถ๋ ฅ.
)
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("> ๋ถ์๋ฒํธ ์
๋ ฅ ? ");
int pdeptno = scanner.nextInt(); // 10~40
String sql = "SELECT * "
+ " FROM emp "
+ " WHERE deptno = " + pdeptno;
// 1 + 2.
Connection conn = DBConn.getConnection();
// 3.
Statement stmt = null;
ResultSet rs = null;
// ์ฌ์์ด ์กด์ฌํ์ง ์์ผ๋ฉด empList ๊ฐ์ฒด ์์ฑ X
ArrayList<EmpDTO> empList = null;
int empno;
String ename;
String job;
Date hiredate;
int mgr;
double sal;
double comm;
int deptno;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql); // select
// stmt.executeUpdate(sql); insert, update, delete
if( rs.next()) {
empList = new ArrayList<EmpDTO>();
do {
empno = rs.getInt("empno");
ename = rs.getString("ename");
job = rs.getString("job");
hiredate = rs.getDate("hiredate");
mgr = rs.getInt("mgr");
sal = rs.getDouble("sal");
comm = rs.getDouble("comm");
deptno = rs.getInt("deptno");
EmpDTO dto = new EmpDTO(empno, ename, job, hiredate, mgr, sal, comm, deptno);
empList.add(dto);
}while( rs.next() );
}
printEmp( empList ) ;
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
rs.close();
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
// 4.
DBConn.close();
}
System.out.println( "END");
} // main
private static void printEmp(ArrayList<EmpDTO> empList) {
try {
Iterator<EmpDTO> ir = empList.iterator();
// java.lang.[NullPointerException]
while (ir.hasNext()) {
EmpDTO dto = ir.next();
System.out.println( dto );
} // while
} catch (NullPointerException e) {
System.out.println("์ฌ์์ด ์กด์ฌํ์ง ์์ต๋๋ค.");
}catch (Exception e) {
e.printStackTrace();
}
} // printEmp
// ์ธ์คํด์ค ๋ณ์
// ํด๋์ค ๋ณ์ == ์ ์ ๋ณ์ == static ๋ณ์ == ๊ณต์ ๋ณ์
private static int selectedNumber;
private static Scanner scanner = new Scanner(System.in);
private static Connection conn ;
private static Statement stmt;
private static ResultSet rs ;
private static String [] menus = {
"๋ถ์ ์กฐํ"
, "๋ถ์ ์ถ๊ฐ"
, "๋ถ์ ์์ "
, "๋ถ์ ์ญ์ "
, "๋ถ์ ๊ฒ์"
, "์ข
๋ฃ"
};
public static void main(String[] args) {
// java.lang.NullPointerException
// EmpDTO dto = null;
// dto.setEname("ํ๊ธธ๋");
conn = DBConn.getConnection();
while( true ) {
๋ฉ๋ด์ถ๋ ฅ();
๋ฉ๋ด์ ํ();
๋ฉ๋ด์ฒ๋ฆฌ();
} // while
} // main
1) ๋ฉ๋ด์ถ๋ ฅ()
private static void ๋ฉ๋ด์ถ๋ ฅ() {
System.out.println("> ๋ฉ๋ด ์ถ๋ ฅ <");
for (int i = 0; i < menus.length; i++) {
System.out.printf("%d. %s\n", ( i+1), menus[i] );
} // for
}
2) ๋ฉ๋ด์ ํ()
private static void ๋ฉ๋ด์ ํ() {
System.out.print("> ๋ฉ๋ด ์ ํํ์ธ์ ? ");
selectedNumber = scanner.nextInt();
}
3) ๋ฉ๋ด์ฒ๋ฆฌ()
private static void ๋ฉ๋ด์ฒ๋ฆฌ() {
switch ( selectedNumber ) {
case 1: // ๋ถ์ ์กฐํ - dept ํ
์ด๋ธ์ ๋ชจ๋ ๋ถ์์ ๋ณด ์ถ๋ ฅ.
selectAllDept();
break;
case 2: // ๋ถ์ ์ถ๊ฐ
insertDept();
break;
case 3: // ๋ถ์ ์์
updateDept();
break;
case 4: // ๋ถ์ ์ญ์
deleteDept();
break;
case 5: // ๋ถ์ ๊ฒ์
searchDept();
break;
case 6: // ์ข
๋ฃ
exit();
break;
default:
System.out.println("\t\t[๊ฒฝ๊ณ ] ๋ฉ๋ด ์ ํ ์๋ชปํ๋ค( 1~6)!!!");
break;
} // switch
์ผ์์ ์ง();
} // ๋ฉ๋ด์ฒ๋ฆฌ
3-1) ๋ถ์ ์กฐํ
private static void selectAllDept() {
String sql = "SELECT * "
+ " FROM dept"
+ " ORDER BY deptno ASC";
ArrayList<DeptDTO> deptList = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
if( rs.next() ) {
deptList = new ArrayList<DeptDTO>();
do {
DeptDTO dto = new DeptDTO( rs.getInt("deptno") , rs.getString("dname"), rs.getString("loc"));
deptList.add(dto);
} while (rs.next());
} // if
printDept( deptList );
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
stmt.close();
rs.close();
// DBConn.close(); ํ๋ก๊ทธ๋จ ์ข
๋ฃํ ๋ ๊ทธ ๋ ํ ๋ฒ๋ง ๋ซ๊ธฐ.
} catch (SQLException e) {
e.printStackTrace();
}
} // try
}
3-2) ๋ถ์ ์ถ๊ฐ
private static char _continue; // 'y' 'n'
private static void insertDept() {
do {
// 1. ๋ถ์๋ฒํธ X , [๋ถ์๋ช
], [์ง์ญ๋ช
] ์
๋ ฅ
// seq_dept ์ํ์ค 50, 10
System.out.println("[๋ถ์ ์ ๋ณด ์
๋ ฅ]");
System.out.print("1. ๋ถ์๋ช
์
๋ ฅ ? ");
String pdname = scanner.next();
System.out.print("2. ์ง์ญ๋ช
์
๋ ฅ ? ");
String ploc = scanner.next();
// String sql = "INSERT INTO dept ( deptno, dname, loc ) VALUES ( seq_dept.nextval, '"+pdname+"', '"+ploc+"' )";
String sql = String.format("INSERT INTO dept ( deptno, dname, loc ) VALUES ( seq_dept.nextval, '%s', '%s' )" , pdname, ploc);
try {
// ์ปค๋ฐ์ ํ์ง ์์๋ ์๋ ์ปค๋ฐ๋๋๋ก ์ค์ ์ด ๋์ด์ ธ ์๋ค.
// conn.setAutoCommit(true);
stmt = conn.createStatement();
// rs = stmt.executeQuery(sql); // SELECT
int rowCount = stmt.executeUpdate(sql); // INSERT, UPDATE, DELETE
if( rowCount == 1) {
System.out.println("> 1๊ฐ ๋ถ์ ์ถ๊ฐ ์๋ฃ!!!");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
} // try
/*
DROP SEQUENCE seq_dept;
--
CREATE SEQUENCE seq_dept
INCREMENT BY 10
START WITH 50
MAXVALUE 90
NOCYCLE
NOCACHE;
*/
๊ณ์์ฌ๋ถํ์ธ();
} while ( Character.toUpperCase(_continue) == 'Y' );
}
private static void ๊ณ์์ฌ๋ถํ์ธ() {
System.out.print("> ๊ณ์ ํ ๊ฑฐ๋ ? ");
try {
_continue = (char)System.in.read();
System.in.skip( System.in.available() ); // 13, 10 ์ ๊ฑฐ
} catch (IOException e) {
e.printStackTrace();
}
}
3-3) ๋ถ์ ์์
// ๋ถ์ ์์
private static void updateDept() {
System.out.print("> ์์ ํ ๋ถ์๋ฒํธ(deptno) ์
๋ ฅ ? ");
int pdeptno = scanner.nextInt();
System.out.print("> ์์ ํ ๋ถ์๋ช
, ์ง์ญ๋ช
์
๋ ฅ ? ");
String pdname = scanner.next();
String ploc = scanner.next();
String sql = String.format(
" UPDATE dept "
+ " SET dname = '%s' , loc = '%s' "
+ " WHERE deptno = %d "
, pdname, ploc, pdeptno );
// 1. 2.
// 3. UPDATE ์ฒ๋ฆฌ
try {
stmt = conn.createStatement();
int rowCount = stmt.executeUpdate(sql); // UPDATE
if( rowCount == 1 ) {
System.out.println("> 1๊ฐ ๋ถ์ ์์ ์๋ฃ!!!");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
3-4) ๋ถ์ ์ญ์
// ๋ถ์ ์ญ์
private static void deleteDept() {
System.out.print("> ์ญ์ ํ ๋ถ์๋ฒํธ(deptno) ์
๋ ฅ ? ");
int pdeptno = scanner.nextInt();
String sql = String.format(
" DELETE FROM dept "
+ " WHERE deptno = %d "
, pdeptno );
try {
stmt = conn.createStatement();
int rowCount = stmt.executeUpdate(sql);
if( rowCount == 1 ) {
System.out.println("> 1๊ฐ ๋ถ์ ์ญ์ ์๋ฃ!!!");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
3-5) ๋ถ์ ๊ฒ์
[๊ธฐ๋ณธ]
// ๋ถ์ ๊ฒ์
private static void searchDept() {
System.out.print("> ๊ฒ์ ์กฐ๊ฑด ์
๋ ฅ ? "); // "dname" "loc"
String searchCondition = scanner.next();
System.out.print("> ๊ฒ์์ด ์
๋ ฅ ? ");
String searchWord = scanner.next();
// java.util.UnknownFormat[Conversion]Exception: Conversion = '''
// %s %d %c %f
// ์ฒซ ๋ฒ์งธ ๋ฐฉ๋ฒ
/*
String sql = String.format(
"SELECT * "
+ " FROM dept "
+ " WHERE %s LIKE '%%%s%%'"
, searchCondition, searchWord);
*/
String sql = String.format(
"SELECT * "
+ " FROM dept "
+ " WHERE %s LIKE '%s' "
, searchCondition, "%"+searchWord.toUpperCase()+"%");
// selectAllDept ๋ฉ์๋ ๋ณต์ฌ ๋ถ์ฌ๋ฃ๊ธฐ
ArrayList<DeptDTO> deptList = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
if( rs.next() ) {
deptList = new ArrayList<DeptDTO>();
do {
DeptDTO dto = new DeptDTO( rs.getInt("deptno") , rs.getString("dname"), rs.getString("loc"));
deptList.add(dto);
} while (rs.next());
} // if
printDept( deptList );
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
stmt.close();
rs.close();
// DBConn.close(); ํ๋ก๊ทธ๋จ ์ข
๋ฃํ ๋ ๊ทธ ๋ ํ ๋ฒ๋ง ๋ซ๊ธฐ.
} catch (SQLException e) {
e.printStackTrace();
}
} // try
}
private static void printDept(ArrayList<DeptDTO> deptList) {
System.out.println("---------------------------");
System.out.printf("%s\t%s\t%s\n", "DEPTNO", "DNAME", "LOC");
System.out.println("---------------------------");
try {
Iterator<DeptDTO> ir = deptList.iterator();
while (ir.hasNext()) {
DeptDTO dto = ir.next();
System.out.println( dto );
} // while
} catch (NullPointerException e) {
System.out.println(" ๊ฒ์ ๊ฒฐ๊ณผ๊ฐ ์์ต๋๋ค. ");
} catch (Exception e) {
// TODO: handle exception
}
}
[๋์ ์ฟผ๋ฆฌ]
private static void searchDept() {
// [ PL/SQL ๋์ ์ฟผ๋ฆฌ ] OPEN ~ FOR ๋ฌธ
System.out.println("[ ๊ฒ์ ์กฐ๊ฑด ] ");
System.out.println("1) ๋ถ์๋ช
");
System.out.println("2) ์ง์ญ๋ช
");
System.out.println("3) ๋ถ์๋ช
+ ์ง์ญ๋ช
");
System.out.print("> ๊ฒ์์กฐ๊ฑด ์ ํ ? "); // 1, 2, 3
int searchCondition = scanner.nextInt();
System.out.print("> ๊ฒ์์ด ์
๋ ฅ ? ");
String searchWord = scanner.next();
String sql = " SELECT * "
+ " FROM dept ";
switch ( searchCondition) {
case 1:
// PL/SQL์์๋ ์ปฌ๋ฌ๋ช
, ํ
์ด๋ธ๋ช
์ ๋ณ์ ์ฌ์ฉ X ( ์๊ธฐ )
sql += String.format(" WHERE dname LIKE '%s' ", "%"+ searchWord + "%");
break;
case 2:
sql += String.format(" WHERE loc LIKE '%%%s%%' ", searchWord );
break;
case 3:
sql += String.format(" WHERE dname LIKE '%1$s' OR loc LIKE '%1$s'", "%"+ searchWord + "%");
break;
}
sql += " ORDER BY deptno ASC";
// selectAllDept ๋ฉ์๋ ๋ณต์ฌ ๋ถ์ฌ๋ฃ๊ธฐ
ArrayList<DeptDTO> deptList = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
if( rs.next() ) {
deptList = new ArrayList<DeptDTO>();
do {
DeptDTO dto = new DeptDTO( rs.getInt("deptno") , rs.getString("dname"), rs.getString("loc"));
deptList.add(dto);
} while (rs.next());
} // if
printDept( deptList );
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
stmt.close();
rs.close();
// DBConn.close(); ํ๋ก๊ทธ๋จ ์ข
๋ฃํ ๋ ๊ทธ ๋ ํ ๋ฒ๋ง ๋ซ๊ธฐ.
} catch (SQLException e) {
e.printStackTrace();
}
} // try
}
[REGEXP_LIKE]
private static void searchDept() {
System.out.println("[ ๊ฒ์ ์กฐ๊ฑด ] ");
System.out.println("1) ๋ถ์๋ช
");
System.out.println("2) ์ง์ญ๋ช
");
System.out.println("3) ๋ถ์๋ช
+ ์ง์ญ๋ช
");
System.out.print("> ๊ฒ์์กฐ๊ฑด ์ ํ ? "); // 1, 2, 3
int searchCondition = scanner.nextInt();
System.out.print("> ๊ฒ์์ด ์
๋ ฅ ? ");
String searchWord = scanner.next();
String sql = " SELECT * "
+ " FROM dept ";
switch ( searchCondition) {
case 1:
// REGEXP_LIKE() ํจ์๋ก ์์
sql += String.format(" WHERE REGEXP_LIKE( dname, '%s', 'i' ) ", searchWord );
break;
case 2:
sql += String.format(" WHERE REGEXP_LIKE( loc, '%s', 'i' ) ", searchWord );
break;
case 3:
sql += String.format(" WHERE REGEXP_LIKE( dname, '%1$s', 'i' ) OR REGEXP_LIKE( loc, '%1$s', 'i' ) "
, searchWord );
break;
}
sql += " ORDER BY deptno ASC";
// selectAllDept ๋ฉ์๋ ๋ณต์ฌ ๋ถ์ฌ๋ฃ๊ธฐ
ArrayList<DeptDTO> deptList = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
if( rs.next() ) {
deptList = new ArrayList<DeptDTO>();
do {
DeptDTO dto = new DeptDTO( rs.getInt("deptno") , rs.getString("dname"), rs.getString("loc"));
deptList.add(dto);
} while (rs.next());
} // if
printDept( deptList );
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
stmt.close();
rs.close();
// DBConn.close(); ํ๋ก๊ทธ๋จ ์ข
๋ฃํ ๋ ๊ทธ ๋ ํ ๋ฒ๋ง ๋ซ๊ธฐ.
} catch (SQLException e) {
e.printStackTrace();
}
} // try
}
3-6) ์ข ๋ฃ
private static void exit() {
System.out.println("\t\t ํ๋ก๊ทธ๋จ ์ข
๋ฃ !!!");
DBConn.close();
System.exit(-1);
}
3-7) ์ผ์์ ์ง
private static void ์ผ์์ ์ง() {
System.out.println("\t\t ์ํฐ์น๋ฉด ๊ณ์ํฉ๋๋ค.");
try {
System.in.read();
System.in.skip( System.in.available() ); // 13, 10
} catch (IOException e) {
e.printStackTrace();
} // try
}
[์ค๋ผํด] ์ฟผ๋ฆฌ ์คํ ์์
1. ๊ตฌ๋ฌธ๋ถ์(Parsing)
- ์์ฑํ ์ฟผ๋ฆฌ๊ฐ ๋ฌธ๋ฒ์ ์ผ๋ก ํ๋ฆฌ์ง ์์์ง ํ์ธํ๋ ๋จ๊ณ. ํด๋น ์ฟผ๋ฆฌ๋ฅผ SQL ์๋ฒ๊ฐ ์ดํดํ ์ ์๋ ๋จ์๋ก ๋ถํดํ๋ ๊ณผ์ ์ด๋ค.
- ๋ง์ฝ ๊ตฌ๋ฌธ์ด ๋ถ์ ํ(๋ฌธ๋ฒ์ด ํ๋ฆฌ๋ค๋ฉด)ํ๋ค๋ฉด ์ด ๋จ๊ณ์์ ์ฒ๋ฆฌ๋ฅผ ์ค๋จํ๋ค. ๋ํ, ์ด ๋ฌธ์ฅ์ด ์ผ๊ด์ฒ๋ฆฌ(batch)์ ์ํด ์๋ค๋ฉด ์ผ๊ด ์ฒ๋ฆฌ ์ ์ฒด๋ฅผ ์ค๋จํ๋ค.
2. ์ต์ ํ(Optimination)
- ์ฟผ๋ฆฌ ์ฒ๋ฆฌ์์ ๋งค์ฐ ์ค์ํ ๋จ๊ณ๋ก ํต๊ณ๋ ์กฐ๊ฐ ์ ๋ณด ๋ฑ์ ๋ฐํ์ผ๋ก ์คํ๊ณํ(Execution Plan)์ ๋ง๋ค์ด ๋ธ๋ค.
1) ์ฟผ๋ฆฌ ๋ถ์: ๊ฒ์ ์ ํ์์ธ์ง ์กฐ์ธ ์กฐ๊ฑด์ธ์ง ํ๋จ.
2) ์ธ๋ฑ์ค ์ ํ: ๋ถํฌ ํต๊ณ ์ ๋ณด๋ฅผ ์ด์ฉํ์ฌ ์ธ๋ฑ์ค ๊ฒ์์ด๋ ํ ์ด๋ธ ์ค์บ ์ค์ ํ๋๋ฅผ ์ ํ. ์ฌ๋ฌ ์ธ๋ฑ์ค ์ค ๊ฐ์ฅ ํจ์จ์ ์ธ ์ธ๋ฑ์ค ํ๋๋ฅผ ์ ํ
3) ์กฐ์ธ ์ฒ๋ฆฌ: Join, Union, Group By, Order By ์ ์ ๊ฐ์ง๊ณ ์๋์ง ์ ์ ํ ์์ ์์๋ฅผ ์ ํ
์ดํ, ์ถ๋ ฅ ๊ฐ์ผ๋ก ์คํ๊ณํ(Excution Plan)์ ์ถ๋ ฅํ๋ค.
3. Row source Generator
- ์ตํฐ๋ง์ด์ ๊ณผ์ ์ ํตํด์ ๋ง๋ค์ด์ง ์คํ๊ณํ์ ์ค์ ์คํํ ์ ์๋ ์ฝ๋ ๋๋ ํ๋ก์์ ํํ๋ก ํฌ๋ฉงํ ํ๋ ์์ .
- Row-Source๋ ๋ ์ฝ๋ ์งํฉ์ ๋ฐ๋ณต ์ฒ๋ฆฌํ๋ฉด์ ์ฌ์ฉ์๊ฐ ์๊ตฌํ ์ต์ข ๊ฒฐ๊ณผ ์งํฉ์ ์ค์ ์ ์ผ๋ก ์์ฑํ๋๋ฐ ์ฌ์ฉ๋๋ ์ ์ด ๊ตฌ์กฐ๋ฅผ ๋งํ๋ค.
4. ์คํ(Execute) : prepared statement๋ฅผ ์ฐ๋ฉด 1~3 ํ์์๊ณ 4๋ง! -> ์ฑ๋ฅ ์ข์์ง
1) Statement stmt ;
2) [ PreparedStatement ; ] ***** ->1~3 ํ์์๊ณ 4๋ง! -> ์ฑ๋ฅ ์ข์์ง
private static int selectedNumber;
private static Scanner scanner = new Scanner(System.in);
private static Connection conn ;
private static PreparedStatement pstmt; // ์ ์ธ
private static ResultSet rs ;
<๋ณ๊ฒฝ๋ด์ฉ>
* ๋ถ์์กฐํ selectAllDept
private static void selectAllDept() {
String sql = "SELECT * "
+ " FROM dept"
+ " ORDER BY deptno ASC";
ArrayList<DeptDTO> deptList = null;
try {
pstmt = conn.prepareStatement(sql); // 1 ์์ (์ฌ๊ธฐ์ sql ๋จผ์ ๋ฑ๋กํด์ผํจ)
rs = pstmt.executeQuery(); // 2 ์์ (์ฌ๊ธฐ์ sql ๋ฑ๋กํ์์์)
if( rs.next() ) {
deptList = new ArrayList<DeptDTO>();
do {
DeptDTO dto = new DeptDTO( rs.getInt("deptno") , rs.getString("dname"), rs.getString("loc"));
deptList.add(dto);
} while (rs.next());
} // if
printDept( deptList );
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
pstmt.close(); // 3 ์์
rs.close();
// DBConn.close(); ํ๋ก๊ทธ๋จ ์ข
๋ฃํ ๋ ๊ทธ ๋ ํ ๋ฒ๋ง ๋ซ๊ธฐ.
} catch (SQLException e) {
e.printStackTrace();
}
} // try
}
*๋ถ์ ์ถ๊ฐ insertDept
private static char _continue; // 'y' 'n'
private static void insertDept() {
do {
System.out.println("[๋ถ์ ์ ๋ณด ์
๋ ฅ]");
System.out.print("1. ๋ถ์๋ช
์
๋ ฅ ? ");
String pdname = scanner.next();
System.out.print("2. ์ง์ญ๋ช
์
๋ ฅ ? ");
String ploc = scanner.next();
// PreparedStatement ๋ ? ๋ฐ์ธ๋ฉ ๋ณ์๋ฅผ ์ฌ์ฉํ๋ค.
// (๊ธฐ์ต) ๋ฌธ์,๋ ์ง '?' X
String sql = "INSERT INTO dept ( deptno, dname, loc ) VALUES ( seq_dept.nextval, ? , ? )" ; // 1 ์์
try {
pstmt = conn.prepareStatement(sql); // 2 ์์
// ? ? [IN], OUT, IN OUT ํ๋ผ๋ฏธํฐ ๊ฐ ์ค์
pstmt.setString(1, pdname);
pstmt.setString(2, ploc ) ;
int rowCount = pstmt.executeUpdate(); // 3 ์์
if( rowCount == 1) {
System.out.println("> 1๊ฐ ๋ถ์ ์ถ๊ฐ ์๋ฃ!!!");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
pstmt.close(); // 4 ์์
} catch (SQLException e) {
e.printStackTrace();
}
} // try
๊ณ์์ฌ๋ถํ์ธ();
} while ( Character.toUpperCase(_continue) == 'Y' );
}
* ๋ถ์ ์์ udateDept
private static void updateDept() {
System.out.print("> ์์ ํ ๋ถ์๋ฒํธ(deptno) ์
๋ ฅ ? ");
int pdeptno = scanner.nextInt();
System.out.print("> ์์ ํ ๋ถ์๋ช
, ์ง์ญ๋ช
์
๋ ฅ ? ");
String pdname = scanner.next();
String ploc = scanner.next();
String sql =
" UPDATE dept "
+ " SET dname = ? , loc = ? "
+ " WHERE deptno = ? ";
// 3. UPDATE ์ฒ๋ฆฌ
try {
pstmt = conn.prepareStatement(sql);
// ? , ? , ?
pstmt.setString(1, pdname);
pstmt.setString(2, ploc);
pstmt.setInt(3, pdeptno);
int rowCount = pstmt.executeUpdate();
if( rowCount == 1 ) {
System.out.println("> 1๊ฐ ๋ถ์ ์์ ์๋ฃ!!!");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
* ๋ถ์ ์ญ์ deleteDept
private static void deleteDept() {
System.out.print("> ์ญ์ ํ ๋ถ์๋ฒํธ(deptno) ์
๋ ฅ ? ");
int pdeptno = scanner.nextInt();
String sql = " DELETE FROM dept "
+ " WHERE deptno = ? "; // 1 ์์
try {
pstmt = conn.prepareStatement(sql);// 2 ์์
pstmt.setInt(1, pdeptno); // 3 ์ถ๊ฐ
int rowCount = pstmt.executeUpdate(); // 4 ์์
if( rowCount == 1 ) {
System.out.println("> 1๊ฐ ๋ถ์ ์ญ์ ์๋ฃ!!!");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
pstmt.close();// 5 ์์
} catch (SQLException e) {
e.printStackTrace();
}
}
}
* ๋ถ์ ๊ฒ์ searchDept
// ๋ถ์ ๊ฒ์
private static void searchDept() {
System.out.println("[ ๊ฒ์ ์กฐ๊ฑด ] ");
System.out.println("1) ๋ถ์๋ช
");
System.out.println("2) ์ง์ญ๋ช
");
System.out.println("3) ๋ถ์๋ช
+ ์ง์ญ๋ช
");
System.out.print("> ๊ฒ์์กฐ๊ฑด ์ ํ ? "); // 1, 2, 3
int searchCondition = scanner.nextInt();
System.out.print("> ๊ฒ์์ด ์
๋ ฅ ? ");
String searchWord = scanner.next();
String sql = " SELECT * "
+ " FROM dept ";
switch ( searchCondition) {
case 1:
// LIKE
// sql += " WHERE dname LIKE ? ;
// pstmt.setString(1, "%"+searchWord+"%");
sql += " WHERE REGEXP_LIKE( dname, ? , 'i' ) ";
break;
case 2:
sql += " WHERE REGEXP_LIKE( loc, ? , 'i' ) ";
break;
case 3:
sql += " WHERE REGEXP_LIKE( dname, ? , 'i' ) OR REGEXP_LIKE( loc, ?', 'i' ) ";
break;
}
sql += " ORDER BY deptno ASC";
// selectAllDept ๋ฉ์๋ ๋ณต์ฌ ๋ถ์ฌ๋ฃ๊ธฐ
ArrayList<DeptDTO> deptList = null;
try {
pstmt = conn.prepareStatement(sql);
// 1,2,3 ๊ฒ์ ?
pstmt.setString(1, searchWord);
// 3 ๊ฒ์ ? ?
if( searchCondition == 3) pstmt.setString(2, searchWord);
rs = pstmt.executeQuery();
if( rs.next() ) {
deptList = new ArrayList<DeptDTO>();
do {
DeptDTO dto = new DeptDTO( rs.getInt("deptno") , rs.getString("dname"), rs.getString("loc"));
deptList.add(dto);
} while (rs.next());
} // if
printDept( deptList );
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
pstmt.close();
rs.close();
// DBConn.close(); ํ๋ก๊ทธ๋จ ์ข
๋ฃํ ๋ ๊ทธ ๋ ํ ๋ฒ๋ง ๋ซ๊ธฐ.
} catch (SQLException e) {
e.printStackTrace();
}
} // try
}
'๐จโ๐ป Web Development > JDBC | JSP | Servlet' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Day1] JSP/Servlet 1 - ๊ฐ์ (0) | 2022.12.06 |
---|---|
[Day5] JSP/JDBC 5 - transaction ์ฒ๋ฆฌ, callable statement, reflection (0) | 2022.11.02 |
[Day4] JSP/JDBC 4 - Board (0) | 2022.11.01 |
[Day3] JSP/JDBC 3 - ์ฌ์ฉ์ ๋ณ๊ฒฝ, ์ฌ๋ฌ์ถ๋ ฅ๋ฐฉ์, linkedhashmap์ฌ์ฉ DTO ์ ์ฅ, MVC, JUnit (0) | 2022.10.31 |
[Day1] JSP/JDBC 1 - ๊ฐ์, jdbc driver, dto (0) | 2022.10.28 |
์ต๊ทผ๋๊ธ