jsp jdbc 사용 예

5482 단어 sqljspmysqljdbc공고

<%@ page contentType="text/xml;charset=gbk"%><%@ page import="java.io.*" %><%@ page import="java.util.*" %><%@ page import="java.util.regex.*" %><%@ page import="java.sql.*" %><%
        PrintWriter pw = response.getWriter();
        pw.write("<?xml version=\"1.0\" encoding=\"gbk\"?>");
        String id_start = request.getParameter("id_start");
        if (id_start == null) {
            pw.write("<crawled_news/>");
            return;
        }
        String count = request.getParameter("count");
        if (count == null) {
            pw.write("<crawled_news/>");
            return;
        }
        System.err.println(id_start);
        System.err.println(count);
        Pattern p = Pattern.compile("^\\d+$");
        if (!(p.matcher(id_start).find() && p.matcher(count).find())) {
            pw.write("<crawled_news/>");
            return;
        }
       
        String query = String.format("select * from qidi where id>=%s order by id limit %s", id_start, count);
        System.err.println(query);
        System.err.println("conn!=null "+conn!=null);
        try{
            if(!conn.isValid(10)){
                conn=getConnection();
            }
        }catch(Exception e){
                e.printStackTrace();
        }
        if(conn==null)conn=getConnection();
        pw.write("<crawled_news>");
        if (conn != null) {
            try {
                ResultSet rs = conn.prepareStatement(query).executeQuery();
                if (rs == null) {
                    return;
                }
                while (rs.next()) {
                    StringBuilder sb = new StringBuilder();
                    sb.append("<item>");
                    sb.append("<id>");
                    String id = rs.getString("id");
                    sb.append(id == null?"":id);
                    sb.append("</id>");

                    sb.append("<title><![CDATA[");
                    String title = rs.getString("title");
                    sb.append(title==null?"":title);
                    sb.append("]]></title>");

                    sb.append("<category><![CDATA[");
                    String cat = rs.getString("category");
                    cat= (cat==null?"":cat);
                    if(cat.indexOf("  ")>=0) cat="post";
                    if(cat.indexOf("  ")>=0) cat="news";
                    if(cat.indexOf("    ")>=0) cat="project";
                    sb.append(cat);
                    sb.append("]]></category>");

                    String content = rs.getString("content");
                    sb.append("<content><![CDATA[");
                    sb.append(content==null?"":content);
                    sb.append("]]></content>");

                    String source = rs.getString("source");
                    sb.append("<source><![CDATA[");
                    sb.append(source==null?"":source);
                    sb.append("]]></source>");

                    String pubdate = rs.getString("pubdate");
                    sb.append("<pubdate><![CDATA[");
                    pubdate = (pubdate==null?"":pubdate);
                    String[] ss = pubdate.split("-");
                    if(ss.length==3){
                        pubdate = ss[0]+" "+ss[1]+" "+ss[2]+" ";
                     }
                    sb.append(pubdate==null?"":pubdate);
                    sb.append("]]></pubdate>");

                    sb.append("</item>");
                    pw.write(sb.toString());
                   
                    pw.flush();
                }
                
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        pw.write("</crawled_news>");
        pw.flush();
%>

<%!    public static Connection conn = getConnection();

    public static Connection getConnection() {
        Connection con = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://sp4:3306/etc?user=search&password=psw&characterEncoding=gbk&autoReconnect=true";
            con = DriverManager.getConnection(url);
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        return con;
    }
%>



좋은 웹페이지 즐겨찾기