기록해! 정리해!

JSP - HRD 싱글톤 본문

JAVA/JSP

JSP - HRD 싱글톤

zsuling 2022. 7. 22. 18:00

1. 싱글톤 데이터베이스 생성

package hrd;

import java.sql.*;

public class DBConnection {
	
	Connection con = null;
	PreparedStatement stmt =null;
	ResultSet rs = null;
	
	String url = "jdbc:oracle:thin:@//localhost:1521/xe";
	String userId = "system";
	String pass = "1234";

	//싱글톤 패턴으로 객체생성 
	private  static DBConnection db=new DBConnection();
	
	private DBConnection(){}
	
	 public static DBConnection getInstance() {  
		  return  db;
	 }
	 
	 
	 public  Connection getConnection() {
			try {
				Class.forName("oracle.jdbc.OracleDriver");
		    	con = DriverManager.getConnection(url, userId, pass);
			} catch (Exception e) {
				e.printStackTrace();
			}
		 
		return con; 
	 }
	 
	public void close1(PreparedStatement stmt, Connection con ) {
		if(stmt !=null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
			if(con != null) {
				try {
					con.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
	}
	
	public void close2(ResultSet rs, PreparedStatement stmt, Connection con ) {
		if(rs !=null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(stmt !=null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
			if(con != null) {
				try {
					con.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		
	}
}

2. HrdDaoImpl에 연결하기

package hrd;

public interface HrdDao {

	int insert(TableVo vo);
	
}
package hrd;

import java.sql.*;

public class HrdDaoImpl implements HrdDao{
	
	DBConnection dbconn = null;
	Connection conn =null;
	PreparedStatement pstmt = null;
	
	public HrdDaoImpl() {
		 dbconn = DBConnection.getInstance();
	
	}
	
	@Override
	public int insert(TableVo vo) {
	 
	  try {
		  conn = dbconn.getConnection(); //오라클 커넥션 연결
		  String SQL = ""; 
		  pstmt = conn.prepareStatement(SQL);
		  pstmt.executeUpdate();
		  
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally {
		dbconn.close1(pstmt, conn);
	}
	return 0;
		
	}

	
	

	
}

 

2. 오라클 테이블 추가 (커밋 필수) -- lib 에 오라클 넣기

Create table T0722 (
 sno varchar2(10)  not  null primary key ,
 sname nvarchar2(20),
 title nvarchar2(50),
 cnt int
) ;

 

3. Vo 생성  - 게터세터/to String

private String sno;
private String sname;
private String title;
private int cnt;

 

4. Hrd들 작성하기

5. HrdDaoImpl 에 쿼리문 작성하기

6. HrdController

7.index.jsp 에 폼 작성하기 / success.jsp / error.jsp 생성

<div align=center>
<br><br><br>
<h1>저장성공</h1>
</div>

 

 

 

'JAVA > JSP' 카테고리의 다른 글

JSP 교재 (2)  (0) 2022.08.03
JSP 교재  (0) 2022.08.02
JSP - 학사관리프로그램(2) 학번/include/서치(검색창)/조회수  (0) 2022.07.21
JSP - 학사관리프로그램  (0) 2022.07.20
서블릿(3)  (0) 2022.07.19
Comments