달력

5

« 2024/5 »

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

http://suite.tistory.com/  fs  2007-01-11




사용자가 적은 프로그램을 만들때  Connection Pool ( DBConnectionManager.java ) 같은 걸 궂이 사용 할 필요 없다.

소스만 지저분 해진다.


그래서 간단하게 jsp 단에서 Statement.executeQuery , Statement.executeUpdate 함수를 쉽게 사용 할 수 있도록

 일명: DBClass를 만들어 보았다.




1. 사용법


test_db.jsp


<%@ page import="linuxmaster.*" %>

<%

        String RET_STR="";

        //디비 연결정보 파일(db.conf) 생성자로 아이피 계정 암호...등  
        DBClass sel_db=new DBClass("db.conf");
        RET_STR=sel_db.db_Init("MA_DB"); // 디비 선택하여 연결

        if( RET_STR !="SUCC" ){
                out.println(RET_STR);
                return;
        }


        String INSERT_SQL="insert into USERS(user_id,user_pwd,user_group) values('a1','a2','admin')";

        // StmtExeUpdate 라는 함수를 이용하여 INSERT,DELETE,UPDATE 를 수행

        RET_STR=sel_db.StmtExeUpdate(INSERT_SQL);

        if( RET_STR != "SUCC" ){
                out.println(RET_STR);
                return;
        }

       
        String SEL_SQL="select * from USERS";

        // StmtExeQry 라는 함수를 이용하여 SELECT 함수 수행

        RET_STR=sel_db.StmtExeQry(SEL_SQL);
        if( RET_STR != "SUCC" ){
                out.println(RET_STR);
                return;
        }
        while(sel_db.m_rs.next()){
                out.println(sel_db.m_rs.getString("user_id"));
                out.println("<br>");
        }


2. 소스 (DBClass)

   DBClass.java

     

/*
 * DBObject.java
 *
 * Created on 2007년 1월 9일 (금), 오전 11:54
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

/**
 *
 * @author fs*/


package linuxmaster;


import java.sql.*;
import java.io.InputStream;
import java.util.Properties;


public class DBClass{


        private Statement m_stmt=null;
        private Connection m_con=null;


        //default file "db.conf"
        private String m_conf_file="db.conf";

        public ResultSet m_rs=null;


        public DBClass(String p_conf_file){
                m_conf_file=p_conf_file;
        }


        public String db_Init(String p_DB_NAME){

                // read db.conf
                InputStream is = getClass().getResourceAsStream(m_conf_file);
                Properties dbProps = new Properties();
                try {
                        dbProps.load(is);
                }
                catch (Exception e) {
                        e.printStackTrace();
                        String print_temp="Can't read the db.conf file. Make sure db.conf is in the CLASSPATH";
                        System.out.println(print_temp);
                        return  print_temp;
                }
                //db connect setup
                String url="";
                try {
                        Class.forName(dbProps.getProperty("DB_DRIVERS")).newInstance();
                        //String print_temp="..[DBOject()] Driverloading succeed( \"" + dbProps.getProperty("DBMS_TYPE")+"\" ) ....[OK]";
                        //System.out.println(print_temp);

                        url = "jdbc:mysql://" +
                                dbProps.getProperty("DB_SERVER") +
                                ":" +
                                dbProps.getProperty("DB_PORT") +
                                "/" +
                                p_DB_NAME;


                        // connect db(p_DB_NAME)
                        m_con =  DriverManager.getConnection(url,dbProps.getProperty("DB_USER"),dbProps.getProperty("DB_PASSWORD"));

                        m_stmt = m_con.createStatement();

                }catch (Exception e) {
                        e.printStackTrace();

                        String print_temp="==[Exception - Class.forName]"+ e.toString() ;
                        System.out.println(print_temp);

                        print_temp="[Error]<b> CHECK URL:" +url+" user:"+dbProps.getProperty("DB_USER")+" pwd:"+dbProps.getProperty("DB_PASSWORD")+"</b><p>"+e.toString();
                        System.out.println(print_temp);
                        return print_temp;
                }
                return "SUCC";
        }

        public String StmtExeQry(String p_SQL){

                m_rs=null;
                try {
                        m_rs=m_stmt.executeQuery(p_SQL);
                }catch (SQLException e) {

                        e.printStackTrace();
                        int ret_code = e.getErrorCode();
                        String print_temp="..[SQLException-StmtExeQry()]"+ret_code+e.getMessage()+"<br><b> SQL: "+p_SQL+"</b>";
                        System.out.println(print_temp);
                        return  print_temp;

                }
                return "SUCC";

        }//end of StmtExeQry

        public String StmtExeUpdate(String p_SQL){

                try{
                        m_stmt.executeUpdate(p_SQL);

                }catch (SQLException e){

                        e.printStackTrace();
                        int ret_code = e.getErrorCode();
                        String print_temp="..[SQLException-StmtExeUpdate()]"+ret_code+e.getMessage()+"<br><b> SQL: "+p_SQL+"</b>";
                        System.out.println(print_temp);

                        return  print_temp;
                }
                return "SUCC";

        }//end of StmtExeUpdate

}//end of DBObject
 


3. 컴파일


#!/bin/sh

JAVAC_EXE=`which javac`

WEB_INF_DIR="../../WEB-INF"

CLASSES_DIR=$WEB_INF_DIR/classes
mkdir -p $CLASSES_DIR
#컴파일
echo $JAVAC_EXE -d $CLASSES_DIR DBClass.java 

$JAVAC_EXE -d $CLASSES_DIR DBClass.java 

CONFIG_FILE=$CLASSES_DIR/linuxmaster/db.conf

# 환경파일 생성
cat > $CONFIG_FILE << EOF0
DBMS_TYPE=mysql
DB_DRIVERS=org.gjt.mm.mysql.Driver
DB_SERVER=localhost
DB_PORT=3307
DB_USER=root
DB_PASSWORD=*****

EOF0

#mysql 드라이버 jar 복사 한번만
#LIB_DIR=$WEB_INF_DIR/lib
#mkdir -p $LIB_DIR;
#cp mysql-connector-java-3.1.12-bin.jar  $LIB_DIR


# 톰켓 재시작
cd ../../../../bin
shutdown.sh;startup.sh


* 스크랩시 덧글 센스

:
Posted by mastar