package selectmaxnum;

import java.io.*;
import java.sql.*;
import java.util.Date;
import javax.ejb.CreateException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import javax.ejb.CreateException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class SelectMaxNumBean implements SessionBean {

    private SessionContext ctx;

    public SelectMaxNumBean( ) {
    }
    public void loadDriver ( ) throws ClassNotFoundException, Exception {
    Class.forName ( "oracle.jdbc.driver.OracleDriver" ) .newInstance ( ) ;
    }
    public int getMaxNum ( ) throws SQLException {
    String url = "jdbc:oracle:thin:@129.254.75.152:1521:orcl816" ;
    Connection con ;
    Statement stmt ;
    con = DriverManager.getConnection ( url, "scott", "tiger" ) ;
    stmt = con.createStatement ( ) ;
    String CountQuery = "select max ( BNUM ) as rc from board " ;
    ResultSet rc = stmt.executeQuery ( CountQuery ) ;
    rc.next ( ) ;
    int num = rc.getInt ( "rc" ) + 1 ;
    stmt.close ( ) ;
    con.close ( ) ;
    return num ;
    }
    private String toHangul ( String str ) {
    if ( str == null ) return null;
    try {
    return new String ( str.getBytes ( "8859_1" ) , "KSC5601" ) ;
    }
    catch ( UnsupportedEncodingException e ) {
    }
    return null ;
    }
    private String hangulTo ( String str ) {
    if ( str == null ) return null;
    try {
    return new String ( str.getBytes ( "KSC5601" ) , "8859_1" ) ;
    }
    catch ( UnsupportedEncodingException e ) {
    }
    return null ;
    }

    public void log(String s) {
        System.out.println(s);
    }

    public void ejbActivate() {
        log("ejbActivate called");
    }

    public void ejbRemove() {
        log("ejbRemove called");
    }

    public void ejbPassivate() {
        log("ejbPassivate called");
    }

    public void setSessionContext(SessionContext ctx) {
        log("setSessionContext called");
        this.ctx = ctx;
    }

    public void ejbCreate() throws CreateException{
        log("ejbCreate called");
        try {
            InitialContext ic = new InitialContext();
        } catch (NamingException ne) {
            throw new CreateException("Failed to find environment value "+ne);
        }
    }

}
