게시판 insert 모듈화..... 선생님..
⚡ 게시판 insert 모듈화.....
- qa_writeok2.java
@MultipartConfig( fileSizeThreshold = 1024 * 1024 * 2, maxFileSize = 1024 * 1024 * 10, maxRequestSize = 1024 * 1024* 100 ) public class qa_writeok2 extends HttpServlet { private static final long serialVersionUID = 1L; PrintWriter pw = null; ArrayList<String> al = null; ArrayList<ArrayList<String>> all = null; protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html;charset=utf-8"); req.setCharacterEncoding("utf-8"); String uid = req.getParameter("mid"); String uname = req.getParameter("qname"); String qa_title = req.getParameter("qcategory"); String qa_pass = req.getParameter("qpw"); String qa_subject = req.getParameter("qsubject"); String qa_text = req.getParameter("qcontent"); Part qa_file = req.getPart("mfile"); String mfile = qa_file.getSubmittedFileName(); String tables = "qa"; String[] cols = {"idx","uidx","uid","uname","qa_title","qa_pass","qa_subject","qa_text","qa_file","qa_date"}; this.al = new ArrayList<String>(Arrays.asList(cols)); this.all = new ArrayList<ArrayList<String>>(); all.add(this.al); if(mfile.equals("")) { this.al = new ArrayList<String>(); this.al.add("0"); this.al.add("2"); this.al.add(uid); this.al.add(uname); this.al.add(qa_title); this.al.add(qa_pass); this.al.add(qa_subject); this.al.add(qa_text); this.al.add(""); this.al.add("now()"); this.all.add(this.al); }else { String url = req.getServletContext().getRealPath("/qa_upload/"); qa_file.write(url+mfile); this.al = new ArrayList<String>(); this.al.add("0"); this.al.add("2"); this.al.add(uid); this.al.add(uname); this.al.add(qa_title); this.al.add(qa_pass); this.al.add(qa_subject); this.al.add(qa_text); this.al.add("./qa_upload/"+mfile); this.al.add("now()"); this.all.add(this.al); } sql_insert in = new sql_insert(); int result = in.insert_sql(this.all,tables); System.out.println(result); this.pw = res.getWriter(); if(result > 0) { this.pw.write("<script>" + "alert('정상적으로 문의 완료되었습니다.');" + "location.href = './top.jsp';" + "</script>"); }else { this.pw.write("<script>" + "alert('올바른 값이 전달되지 않았습니다.');" + "location.href = './top.jsp';" + "</script>"); } this.pw.close(); } }
- 모듈화할 파일 => database와 연결된 dbconfig파일과 extendspublic class sql_insert extends dbconfig{ Connection con = null; Statement st = null; public sql_insert(){ try { this.con = info(); System.out.println("database 접속!!"); }catch(Exception e) { System.out.println("database 접속오류!!"); } } public int insert_sql(ArrayList<ArrayList<String>> data,String tablename) { int return_no = 0; //실패- 기본 sql문법 오류 String sql = "insert into " + tablename + " "; int z=data.get(0).size(); int w=data.size(); int ww=0; String dda = ""; while(ww<w) { //2바퀴 if(ww==0) { sql += "("; dda = ""; }else { sql += " values ("; dda = "'"; } int zz=0; while(zz<z) { if(zz==z-1) { if(data.get(ww).get(zz) == "now()") { sql += data.get(ww).get(zz); }else { sql += dda + data.get(ww).get(zz) + dda; } }else { sql += dda + data.get(ww).get(zz) + dda + ","; } zz++; } sql += ")"; ww++; } System.out.println(sql); try { this.st = this.con.createStatement(); //try-catch 필수 return_no = this.st.executeUpdate(sql); }catch(Exception e) { System.out.println("SQL 문법오류!"); }finally { try { this.st.close(); this.con.close(); }catch(Exception e2){ e2.getMessage(); } } return return_no; } }
'CLASS > SERVLET' 카테고리의 다른 글
web.xml 선언을 하지 않고 사용 (0) | 2024.07.05 |
---|---|
로그인 - 자동저장 (0) | 2024.07.03 |
#8-1 / 게시판 (0) | 2024.06.21 |
#7-3 / 첨부파일 기능 + database 저장 (0) | 2024.06.19 |
#7-2 / 첨부파일 기능 - java + io (0) | 2024.06.19 |