2019.07.04

 

  각 jsp 파일 안에 DB 연결을 위한 코딩을 따로 java 파일에서 관리하고 원할 때 호출

1
2
3
4
5
6
7
8
9
10
11
12
13
<%
    Connection conn = null;
    PreparedStatement pstmt = null;
 
    // 01 드라이버 로딩
    Class.forName("com.mysql.jdbc.Driver");    
    
    // 02 DB연결(Connection)
    String jdbcDriver = "jdbc:mysql://localhost:3306/DB명?" + "useUnicode=true&characterEncoding=euckr";
    String dbUser = "DB아이디";
    String dbPass = "DB비밀번호";
    conn = DriverManager.getConnection(jdbcDriver, dbUser, dbPass); 
%> 
 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// DriverDB.java
 
package kr.or.ksmart.driverdb;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
 
public class DriverDB {
 
    // 01 드라이버 로딩과 02 DB연결 후 Connection객체 주소값을 리턴하는 메서드 선언
    public Connection driverDbcon() throws ClassNotFoundException, SQLException { 
        Class.forName("com.mysql.jdbc.Driver");
        String jdbcDriver = "jdbc:mysql://localhost:3306/DB명?" + "useUnicode=true&characterEncoding=euckr";
        String dbUser = "DB아이디";
        String dbPass = "DB비밀번호";  
        Connection conn = DriverManager.getConnection(jdbcDriver, dbUser, dbPass);
    
        return conn;
    }
}
 
 

 

1
2
3
4
5
6
7
8
9
10
11
12
<!-- DriverDB_call.jsp -->
 
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%>
<%@ page import="kr.or.ksmart.driverdb.DriverDB"%>
<%@ page import="java.sql.Connectio"%>
 
<!DOCTYPE html>
<%
    Connection conn = null;
    DriverDB db = new DriverDB();
    conn = db.driverDbcon();
%>
 
 

 

 메서드 호출하면 끝

+ Recent posts