<%@ page contentType="text/xml;charset=gbk"%><%@ page import="org.apache.commons.dbcp.*" %><%@ 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 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);
conn = getConnection();
pw.write("<crawled_news>");
if (conn != null) {
try {
ResultSet rs = conn.prepareStatement(query).executeQuery();
if (rs == null) {
return;
}
while (rs.next()) {
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(conn != null){
try{
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
pw.write("</crawled_news>");
pw.flush();
%>
<%!
public static Connection conn = getConnection();
public static BasicDataSource bds = getDataSource();
public static BasicDataSource getDataSource() {
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql://sp4:3306/etc?user=user&password=psw&characterEncoding=gbk&autoReconnect=true");
return ds;
}
//关闭数据源
public static void shutdownDataSource(BasicDataSource ds) throws SQLException {
ds.close();
}
public static Connection getConnection() {
if(bds == null){
bds = getDataSource() ;
}
Connection con = null;
try{
con = bds.getConnection();
}catch(Exception e){
e.printStackTrace();
}
return con;
}
%>
jsp中使用jdbc和dbcp一例
最新推荐文章于 2021-01-21 08:41:43 发布