- 데이터 확인용 JSP/미완/spring/jndi추가
- 日常茶飯事
- 2009. 4. 15. 14:32
<%@page contentType="text/html; charset=UTF-8" %> <%@page import="java.sql.*"%> <% String cmd = request.getParameter("cmd"); String query = empty(request.getParameter("query")); %> <html> <head><title></title> </head> <body> <form method="post"> <input type="hidden" name="cmd" value="execute"> <textarea name="query" rows="" cols=""><%= query %></textarea> <input onclick="this.form.submit();" type="button"> <% if("execute".equals(cmd)){ Connection con = getConnection(); PreparedStatement ps = null; ResultSet rs = null; try{ ps = con.prepareStatement(query); boolean queryType = ps.execute(); if(queryType){ ResultSetMetaData md = ps.getMetaData(); int columnCount = md.getColumnCount(); out.println("<table border="1"><tr>"); for(int i = 1 ; i <= columnCount ; i++){ out.println("<td>" + md.getColumnName(i) + "</td>"); //getColumnLabel()...sql server는 columnName 과 동일함. } out.println("</tr>"); rs = ps.getResultSet(); while(rs.next()){ out.println("<tr>"); for(int j = 1 ; j <= columnCount ; j++){ out.println("<td>" + rs.getObject(j) + "</td>"); } out.println("</tr>"); } out.println("</table>"); }else{ //ps.getUpdateCount() //select 에도 이게 되나? sql server 는 select 인 경우 -1임. } }catch(Exception e){ //이거 출력하는거 하나 만들어둬. %> <%= e %> <% }finally{ close(con, ps, rs); } } %> <%! Connection getConnection(){ //이 메서드의 구현이 바뀌겠지 return project.DBConnectionManager.getInstance(); } String empty(String s){ return s == null ? "" : s; } void close(Connection con, Statement st, ResultSet rs){ if(rs != null){ try{ rs.close(); }catch(Exception ignored){ } } if(st != null){ try{ st.close(); }catch(Exception ignored){ } } if(con != null){ try{ con.close(); }catch(Exception ignored){ } } } %>
UPDATE/DELETE/INSERT 구문은 affectedRows 를 넘겨받아서 일치하는 경우만 commit 하도록 하자.
SELECT 시 로우개수를 제한할 것.
Recent comment