=================
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-27 | 1678 | ||
[채용예정교육]오라클자바개발잘하는신입뽑기2개월과정,교육전취… | 12-11 | 1195 | ||
53 | [평일주간]100%환급6건,안드로이드,자바,C#,스프링3.2,SQL,힌트/… | 03-15 | 957 | |
52 | [주말주간]C#, ASP.NET마스터 | 01-31 | 1115 | |
51 | [평일,기업100%환급]SQL기초에서 Schema Object까지 | 01-31 | 874 | |
50 | [평일야간]HTML5, CSS3,Ajax, jQuery마스터과정 | 01-31 | 808 | |
49 | [평일주간,평일야간,주말]Spring,MyBatis,Hibernate개발자과정 | 01-19 | 1093 | |
48 | [평일주간,평일야간,주말]안드로이드개발자과정 | 01-11 | 985 | |
47 | [평일야간,주말주간]JAVA,Network&WEB&Framework | 01-03 | 1479 | |
46 | 기업100%환급/오라클/자바/스프링/안드로이드/닷넷C#/jQUERY개발… | 12-27 | 1678 | |
45 | [평일야간,주말]자바기초에서JSP,Ajax,jQuery,Spring3.2,MyBatis… | 12-19 | 1222 | |
44 | 웹퍼블리싱 마스터(HTML5,CSS3,jQUERY,AJAX,JavaScript) | 12-14 | 1216 | |
43 | [채용예정교육]오라클자바개발잘하는신입뽑기2개월과정,교육전취… | 12-11 | 1195 | |
42 | [평일,기업100%환급]자바기초에서 JDBC, Servlet/JSP까지 | 12-09 | 966 | |
41 | [평일야간]닷넷(C#,Network,ADO.NET,ASP.NET)마스터 | 12-01 | 1138 | |
40 | [기업100%환급]C#4.0,WinForm,ADO.NET프로그래밍(평일주간(단기)… | 12-01 | 1372 | |
39 | [평일야간,주말,주말야간]SQL기초에서실무까지(SQL기초,PLSQL,힌… | 12-01 | 856 |
댓글 없음:
댓글 쓰기