2014년 9월 1일 월요일

Insert 예제[자바JSP/Servlet/JDBC/MiPlatform/XPlatform/교육/JSP/서블릿강좌/Spring교육잘하는곳/자바,JSP/spring/교육추천/JSP/JDBC실무교육]

================= 
Insert 예제 
================= 

/* JDBC Insert 예제 */ 
import java.io.*; 
import java.sql.*; 
import javax.servlet.*; 
import javax.servlet.http.*; 

public class Insert extends HttpServlet { 

  public void doGet(HttpServletRequest req, HttpServletResponse res) 
                              throws ServletException, IOException { 
      res.setContentType("text/plain; charset=euc-kr"); 
      PrintWriter out = res.getWriter();  
  
      out.println("<html><head><title>JDBC Insert예제</title></head><body>"); 
  out.println("<form name=myForm action=/servlet/Insert method=post>"); 
      out.println("이름 : <input type=text name=name><br>"); 
  out.println("전화번호 : <input type=text name=phone><br>"); 
  out.println("성별    :남 <input type=radio name=sex value='M'>"); 

      out.println("        여 <input type=radio name=sex value='F'><br>"); 
      out.println("나이 : <input type=text name=age size=3>"); 
  out.println("<input type=submit value='저장'"); 
  out.println("</form></body></html>"); 
  } 

  public void doPost(HttpServletRequest req, HttpServletResponse res) 
                              throws ServletException, IOException { 
    Connection con = null; 
    Statement stmt = null; 
    ResultSet rs = null; 
String szName = null; 
String szPhone = null; 
String szSex = null; 
int iNum = 0; 
int iAge = 0; 
String Query = null; 

      res.setContentType("text/plain; charset=euc-kr"); 
    PrintWriter out = res.getWriter();  

    try { 
      // 오라클 드라이버를 Load 한다 
      Class.forName("oracle.jdbc.driver.OracleDriver"); 
        
      // 데이타 베이스에 접속을 한다. 
      con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ahah", "scott", "tiger"); 
      con.setAutoCommit(false); 

      //Form의 파라미터를 받음 
      if (req.getParameter("name") != null) szName = req.getParameter("name"); 
      if (req.getParameter("phone") != null) szPhone = req.getParameter("phone");              
      if (req.getParameter("sex") != null) szSex = req.getParameter("sex"); 
      if (req.getParameter("age") != null) iAge = Integer.parseInt(req.getParameter("age“)); 


      // 순번값을 가져옴 
      stmt = con.createStatement(); 
      rs = stmt.executeQuery("SELECT max(num)+1 FROM employees "); 
  rs.next(); 
      iNum = rs.getInt(1); 
  rs.close(); 

      //입력 Query 생성 
  Query = "insert into employees (num, name, phone, sex, age) values (" + iNum + ",'"+han(szName)+"','"+szPhone+"','"  
          +szSex+"',"+iAge+")"; 

      stmt.executeUpdate(Query); 
  con.commit(); 
  stmt.close(); 
  con.close(); 

      res.sendRedirect("/servlet/Insert"); 
    }  


  catch(ClassNotFoundException e) {  
      out.println("Couldn't load database driver: " + e.getMessage()); 
    } 
    catch(SQLException e) {  
      out.println("SQLException caught: " + e.getMessage()); 
    } 
    finally { 
      // 언제나 데이타 베이스 연결을 종료한다. 
      try {        if (con != null) con.close(); } 
catch (SQLException ignored) { } 
    } 
  } 
  public static String han(String Unicodestr) throws UnsupportedEncodingException {    
      if( Unicodestr == null)    
        return null;    
      return new String(Unicodestr.getBytes("8859_1"),"KSC5601");    
  }      
} 



 기업100%환급/오라클/자바/스프링/안드로이드/닷넷C#/jQUERY개발… 오라클자바…12-271678
 [채용예정교육]오라클자바개발잘하는신입뽑기2개월과정,교육전취…오라클자바…12-111195
53 [평일주간]100%환급6건,안드로이드,자바,C#,스프링3.2,SQL,힌트/… 오라클자바…03-15957
52 [주말주간]C#, ASP.NET마스터 오라클자바…01-311115
51 [평일,기업100%환급]SQL기초에서 Schema Object까지 오라클자바…01-31874
50 [평일야간]HTML5, CSS3,Ajax, jQuery마스터과정 오라클자바…01-31808
49 [평일주간,평일야간,주말]Spring,MyBatis,Hibernate개발자과정 오라클자바…01-191093
48 [평일주간,평일야간,주말]안드로이드개발자과정 오라클자바…01-11985
47 [평일야간,주말주간]JAVA,Network&WEB&Framework 오라클자바…01-031479
46 기업100%환급/오라클/자바/스프링/안드로이드/닷넷C#/jQUERY개발… 오라클자바…12-271678
45 [평일야간,주말]자바기초에서JSP,Ajax,jQuery,Spring3.2,MyBatis… 오라클자바…12-191222
44 웹퍼블리싱 마스터(HTML5,CSS3,jQUERY,AJAX,JavaScript) 오라클자바…12-141216
43 [채용예정교육]오라클자바개발잘하는신입뽑기2개월과정,교육전취… 오라클자바…12-111195
42 [평일,기업100%환급]자바기초에서 JDBC, Servlet/JSP까지 오라클자바…12-09966
41 [평일야간]닷넷(C#,Network,ADO.NET,ASP.NET)마스터 오라클자바…12-011138
40 [기업100%환급]C#4.0,WinForm,ADO.NET프로그래밍(평일주간(단기)… 오라클자바…12-011372
39 [평일야간,주말,주말야간]SQL기초에서실무까지(SQL기초,PLSQL,힌… 오라클자바…12-01856

댓글 없음:

댓글 쓰기