[Day1] JSP/JDBC 1 [10/26]
1. JDBC
ใฑ. Java DataBase Connectivity
ใด. ์๋ฐ์ DB ์ฐ๋ ๋ฐ ์์ (CRUD) ํ๋ ์๋ฐ ํ์ค [์ธํฐํ์ด์ค]
ใท. DB ์ฐ๋ ๊ธฐ์
2. ๊ทธ๋ฆผ ์ค๋ช
3&4.
3. JDBC Driver๋ฅผ ๊ด๋ฆฌํ๋ ๊ด๋ฆฌ์ : JDBC DriverManager
4. JDBC Driver 4๊ฐ์ง ์ข ๋ฅ
ใฑ. Type1 - ODBC Driver ๋ธ๋ฆฟ์ง ์ฌ์ฉ ์ฐ๋ X
ใด. Type2 - Native Library C/C++ ์ธ์ด ์ฌ์ฉํด์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ฐ๋ X
ใท. Type3 - ์๋ฒ ๋ฏธ๋ค์จ์ด X
ใน. Type4 - [Thin] ๋๋ผ์ด๋ฒ, ์์ ์๋ฐ ๋ง๋ค์ด์ก๊ณ , O
DBMS ์ง์ ์ฐ๊ฒฐ, ํ์ฌ ๊ฐ์ฅ ๋ง์ด ์ฌ์ฉ๋๋ค.
5. DBMS(์ค๋ผํด) ์ฐ๊ฒฐ( Connection)ํ๋ ์์ (์๊ธฐ)
1) Class.forName() ๋ฉ์๋๋ฅผ ์ฌ์ฉํด์ JDBC ๋๋ผ์ด๋ฒ ๋ก๋ฉ // JDBC๋ฅผ ๋ฉ๋ชจ๋ฆฌ์์ ์ฌ๋ฆฐ๋ค๋ ์๋ฏธ
2) DriverManager ํด๋์ค ์ getConnection() ๋ฉ์๋๋ฅผ ์ฌ์ฉํด์ Connection ๊ฐ์ฒด ์์ฑ
3) CRUD ํ์ํ ์์
4) ์ฐ๊ฒฐ์ข ๋ฃ Connection ๊ฐ์ฒด ๋ซ๊ธฐ(Close)
์ค์ต
public static void main(String[] args) {
// 1. JDBC ๋๋ผ์ด๋ฒ ๋ก๋ฉ - Class.forName()
// oracle.jdbc.driver = ํจํค์ง
// OracleDriver = ๋๋ผ์ด๋ฒ
String className = "oracle.jdbc.driver.OracleDriver";
try {
// java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
Class.forName(className);
// 2. Connection ๊ฐ์ฒด ์์ฑ - DriverManager.getConnection()
// ์ฐ๊ฒฐ ๋ฌธ์์ด( connection string ) = url, user, password
String url = "jdbc:oracle:thin:@localhost:1521:xe";
String user = "scott";
String password = "tiger";
Connection conn = DriverManager.getConnection(url, user, password);
// 3. CRUD
System.out.println( conn );
System.out.println( conn.isClosed() ); // false
// 4. ์ฐ๊ฒฐ ์ข
๋ฃ - Connection.close()
if( !conn.isClosed()) conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println(" END ");
} // main
์ค์ต2
๋ช ๋ น์ ์ํํ๋ ๊ฐ์ฒด
ใฑ. Statement : ๊ธฐ๋ณธ๊ฐ์ฒด , createStatement() ๋ฉ์๋๋ก ์ป์ด์จ๋ค.
ใด. PerparedStatement : ? ๋ฐ์ธ๋ฉ ๋ณ์ *** // ์ข์์ฝ๋ฉ!!
ใท. CallableStatement : ์ ์ฅ ํ๋ก์์ ์ฌ์ฉ
Statement stmt = conn.createStatement();
10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON |
String sql = "SELECT * "
+ " FROM dept";
ResultSet rs = stmt.executeQuery(sql); // SELECT ๊ฒฐ๊ณผ
//stmt.executeUpdate(sql); // INSERT, UPDATE, DELETE
int deptno;
String dname, loc;
b = rs.next();
if( b ) { // ๊ฐ์ด ์๋์ง ํ์ธ
deptno = rs.getInt("deptno");
dname = rs.getString("dname");
loc = rs.getString("loc");
System.out.printf("%d\t%s\t%s\n", deptno, dname, loc );
}
while( rs.next() ) {
deptno = rs.getInt("deptno");
dname = rs.getString("dname");
loc = rs.getString("loc");
System.out.printf("%d\t%s\t%s\n", deptno, dname, loc );
}
์ค์ต3
Class.forName(className);
Connection conn = DriverManager.getConnection(url, user, password);
String sql = "SELECT * FROM emp";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
int empno;
String ename;
String job;
// java.sql.Date hiredate; ์ด๊ฑฐ ์ถ์ฒ! ๋๋ค ๋๊ธดํจ
String hiredate;
int mgr;
double sal;
double comm;
int deptno;
while( rs.next() ) {
empno = rs.getInt("empno");
ename = rs.getString("ename");
job = rs.getString("job");
hiredate = rs.getString("hiredate");
mgr = rs.getInt("mgr");
sal = rs.getDouble("sal");
comm = rs.getDouble("comm");
deptno = rs.getInt("deptno");
System.out.printf("%d, %s, %s, %s, %d, %.2f, %.2f, %d\n"
, empno, ename, job,hiredate,mgr, sal, comm, deptno);
} // while
์ค์ต4 : ์ ๋ ฅ๋ฐ์์ ์กฐ๊ฑด์ ๋ก ์ฃผ๊ธฐ
System.out.print("> ๋ถ์ ๋ฒํธ๋ฅผ ์
๋ ฅํ์ธ์ ? ");
int pdeptno;
Scanner scanner = new Scanner(System.in);
pdeptno = scanner.nextInt();
try {
Class.forName(className);
Connection conn = DriverManager.getConnection(url, user, password);
//String sql = "SELECT * "
String sql = "SELECT empno, ename, job, TO_CHAR( hiredate, 'yyyy-mm-dd' ) hiredate, mgr, sal, comm, deptno "
+ " FROM emp"
+ " WHERE deptno = " + pdeptno
+ " ORDER BY hiredate ASC";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
int empno;
String ename;
String job;
// java.sql.Date hiredate;
String hiredate;
int mgr;
double sal;
double comm;
int deptno;
if( rs.next() ) {
do {
empno = rs.getInt("empno");
ename = rs.getString("ename");
job = rs.getString("job");
hiredate = rs.getString("hiredate");
mgr = rs.getInt("mgr");
sal = rs.getDouble("sal");
comm = rs.getDouble("comm");
deptno = rs.getInt("deptno");
System.out.printf("%d, %s, %s, %s, %d, %.2f, %.2f, %d\n"
, empno, ename, job
// ,hiredate.substring(0, 11)
, hiredate
,mgr, sal, comm, deptno);
} while ( rs.next() );
}else {
System.out.printf("%d๋ฒ ๋ฒ ์ฌ์์ ์กด์ฌํ์ง ์์ต๋๋ค.\n", pdeptno);
} // if
์ถ๋ ฅ๋ ์ง ์์
Date hiredate;
int mgr;
double sal;
double comm;
int deptno;
if( rs.next() ) {
do {
//rs.getXXX()
hiredate = rs.getDate("hiredate");
System.out.printf("%d, %s, %s, %s, %d, %.2f, %.2f, %d\n"
, empno, ename, job
// ,hiredate.substring(0, 11)
, hiredate
,mgr, sal, comm, deptno);
DBconnํ์ฉ
package com.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
// ์ฑ๊ธํค ์ ์ธ - ๋ณต์ต
public class DBConn {
// new DBConn() ๊ฐ์ฒด ์์ฑ X
private DBConn() {}
private static Connection connection = null;
// ๋ก์ปฌDB , scott, tiger
public static Connection getConnection() {
if( connection == null ) {
// 1. + 2.
// 1.
String className = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:xe"; // dbname:port:SID
String user = "scott";
String password = "tiger";
try {
Class.forName(className);
connection = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} // try
} // if
return connection;
}
// ์ค๋ฒ๋ผ์ด๋ฉ==์ฌ์ ์ํจ์/[์ค๋ฒ๋ก๋ฉ==์ค๋ณตํจ์]
public static Connection getConnection(String url, String user, String password ) {
if( connection == null ) {
String className = "oracle.jdbc.driver.OracleDriver";
try {
Class.forName(className);
connection = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} // try
} // if
return connection;
}
public static void close() {
try {
if( connection != null && !connection.isClosed()) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
// ********
connection = null;
}
} // class
System.out.print("> ๋ถ์ ๋ฒํธ๋ฅผ ์
๋ ฅํ์ธ์ ? ");
int pdeptno;
Scanner scanner = new Scanner(System.in);
pdeptno = scanner.nextInt();
try {
Connection conn = DBConn.getConnection();
String sql = "SELECT empno, ename, job, hiredate, mgr, sal, comm, deptno "
+ " FROM emp"
+ " WHERE deptno = " + pdeptno
+ " ORDER BY hiredate ASC";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
int empno;
String ename;
String job;
Date hiredate;
int mgr;
double sal;
double comm;
int deptno;
if( rs.next() ) {
do {
// rs.getXXX()
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");
System.out.printf("%d, %s, %s, %s, %d, %.2f, %.2f, %d\n"
, empno, ename, job
// ,hiredate.substring(0, 11)
, hiredate
,mgr, sal, comm, deptno);
} while ( rs.next() );
}else {
System.out.printf("%d๋ฒ ๋ฒ ์ฌ์์ ์กด์ฌํ์ง ์์ต๋๋ค.\n", pdeptno);
} // if
// 4.
rs.close();
stmt.close();
// conn.close();
DBConn.close();
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("END");
} // main
Connection conn = DBConn.getConnection();
DBConn.close();
// conn.close(); X -> connection = null;์ ์ค์ผํ๊ธฐ๋๋ฌธ์
Properties p = new Properties();
p.setProperty("className", "oracle.jdbc.driver.OracleDriver");
p.setProperty("url", "jdbc:oracle:thin:@localhost:1521:xe");
p.setProperty("user", "scott");
p.setProperty("password", "tiger");
String comments = "> JDBC Connection String <";
// System.getProperty("user.dir");
String fileName = ".\\src\\com\\util\\ConnectionString.properties";
FileWriter writer;
try {
writer = new FileWriter(fileName);
p.store(writer, comments);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("END");
String className , url , user , password;
Properties p = new Properties();
String fileName = ".\\src\\com\\util\\ConnectionString.properties";
Reader reader;
try {
reader = new FileReader(fileName);
p.load(reader);
className = p.getProperty("className");
url = p.getProperty("url");
user = p.getProperty("user");
password = p.getProperty("password");
System.out.println( className );
System.out.println( url );
System.out.println( user );
System.out.println( password );
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
DTO (data transfer object, ๋ฐ์ดํฐ ์ ์ก ๊ฐ์ฒด)
-domain dto-
package domain;
public class DeptDTO {
// field
private int deptno;
private String dname;
private String loc;
// constructor
public DeptDTO() {}
public DeptDTO(int deptno, String dname, String loc) {
super();
this.deptno = deptno;
this.dname = dname;
this.loc = loc;
}
// getter, setter
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
@Override
public String toString() {
// return "[deptno=" + deptno + ", dname=" + dname + ", loc=" + loc + "]";
return String.format("%d\t%s\t%s", deptno, dname, loc );
}
} // class
-javaํ์ผ-
package days01;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import com.util.DBConn;
import domain.DeptDTO;
/**
* @author k≡n¡k
* @date 2022. 10. 26. - ์คํ 4:17:37
* @subject JDBC ์์
1 ์ผ์ฐจ
* @content dept ํ
์ด๋ธ ๋ถ์ ์ ๋ณด๋ฅผ ์ฝ์ด์์ DTO ๊ฐ์ฒด ์ ์ฅ -> ArrayList ์ ์ฅ -> ์ถ๋ ฅ
* - ๋ฐ์ดํฐ ์ ์ก ๊ฐ์ฒด([D]ata [t]ransfer [o]bject)
* - M - dto - V - dto - C
* - DAO ( Data Access Object) : DB i,u,d,s ์ค์ ๊ฐ์ฒด // db์ฐ๋๊ฐ์ฒด
*/
public class Ex07 {
public static void main(String[] args) {
// 1. ๋ถ์์ ๋ณด๋ฅผ ์ ์ฅํ DTO ์ ์ธ - domain ํจํค์ง
// 2. DAO - persistence ํจํค์ง
ArrayList<DeptDTO> deptList = null;
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String sql = " SELECT deptno, dname, loc "
+ " FROM dept";
conn = DBConn.getConnection();
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
if ( rs.next() ) {
deptList = new ArrayList<DeptDTO>();
do {
/*
* rs.getInt("deptno");
* rs.getString("dname");
* rs.getString("loc");
*/
int deptno = rs.getInt( 1 );
String dname = rs.getString( 2 );
String loc = rs.getString( 3 );
/*
DeptDTO dto = new DeptDTO();
dto.setDeptno(deptno);
dto.setDname(dname);
dto.setLoc(loc);
deptList.add(dto);
*/
//์์ฑ์
deptList.add( new DeptDTO(deptno, dname, loc) );
} while ( rs.next() );
} else {
System.out.println("๋ถ์๊ฐ ์กด์ฌํ์ง ์์ต๋๋ค.");
} // if
// ๋ถ์ ์ ๋ณด๋ฅผ ์ถ๋ ฅํ๋ ๋ฉ์๋
// ๋งค๊ฐ๋ณ์ : ArrayList
printDept( deptList );
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
stmt.close();
rs.close();
DBConn.close();
} catch (SQLException e) {
e.printStackTrace();
}
} // try
System.out.println( "END" );
} // main
private static void printDept(ArrayList<DeptDTO> deptList) {
Iterator<DeptDTO> ir = deptList.iterator();
while (ir.hasNext()) {
DeptDTO dto = ir.next();
System.out.println( dto );
} // while
} // printDept
} // class
'๐จโ๐ป 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 |
[Day2] JSP/JDBC 2 - ๋์ ์ฟผ๋ฆฌ, PreparedStatement (0) | 2022.10.31 |
์ต๊ทผ๋๊ธ