package changeitem;

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;

public class ChangeItemBean implements SessionBean {

    private SessionContext ctx;

    public ChangeItemBean( ) {
    }
    public void loadDriver ( ) throws ClassNotFoundException, Exception {
    Class.forName ( "oracle.jdbc.driver.OracleDriver" ) .newInstance ( ) ;
    }
    public BoardData change ( String num, String password ) throws SQLException {
    String query = "select BNUM, BDATE, BNAME, BEMAIL, BTITLE, BCONTENT,BPASSWORD from board where BNUM = " +num.trim() + " and BPASSWORD = '" + password + "'";
    BoardData data = null ;
    String url = "jdbc:oracle:thin:@localhost:1521:etri" ;
    Connection con ;
    Statement stmt ;
    con = DriverManager.getConnection ( url, "scott", "tiger" ) ;
    stmt = con.createStatement ( ) ;
    ResultSet rs = stmt.executeQuery ( query ) ;
    rs.next ( ) ;
    Date bdate = rs.getDate ( "BDATE" ) ;
    String btitle = rs.getString ( "BTITLE" ) ;
    String bname = rs.getString ( "BNAME" ) ;
    String bemail = rs.getString ( "BEMAIL" ) ;
    String bcontent = rs.getString ( "BCONTENT" ) ;
    String bpassword = rs.getString ( "BPASSWORD" ) ;
    data = new BoardData ( 0, bdate, btitle, bname, bemail, bpassword, bcontent ) ;
    stmt.close ( ) ;
    con.close ( ) ;
    return data ;
    }

    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);
        }
    }

}
