[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

 

  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ