package com.gaowj.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;

import org.apache.commons.io.IOUtils;

public class ConnectionFactoryNormal {

	public static Connection getConn(){
	    String user="sa";
 	    String pass="sa";
 	    String url="jdbc:sqlserver://192.168.0.242:1433;databaseName=haishushequ";
 	    Connection conn=null;
  	try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			conn=DriverManager.getConnection(url, user, pass);
		} catch (ClassNotFoundException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		return conn;
  }

/**
 * 将byte[]转换成String类型
 */
private static String bytesToString(ByteArrayInputStream inStream){
	String result = null;
	//ByteArrayInputStream inStream = new ByteArrayInputStream(array);   
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
    //byte[] data = new byte[array.length];  
    int count = -1;
    int tmpi = 0;
    try {
		while((tmpi = inStream.read()) != -1){
			outStream.write(tmpi);
		}  
		//data = null;  
		result = new String(outStream.toByteArray(),"utf-8");  
	} catch (IOException e) {
		e.printStackTrace();
	} finally {
		try {
			outStream.close();
			inStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	return result;
} 

public static void main(String[] args) {
	// TODO 自动生成方法存根
  Connection conn_s=ConnectionFactoryNormal.getConn();
//  Statement st_s=null;
  PreparedStatement ps_d=null;
//  Statement st_d=null;
  ResultSet rs_s=null;
//  ResultSet rs_d=null;
  String sql_s="select u.aid, u.cid, u.title, u.subhead, u.content, u.author, " +
  		"u.status, u.crtime,u.modtime, u.pubtime, u.endtime from jparticle u where 1=1 and AUTHOR=?";
  try {
	  ps_d=conn_s.prepareStatement(sql_s);
	  ps_d.setString(1, "");
	  rs_s = ps_d.executeQuery();
	  int i=0;
	  while(rs_s.next()){
		  System.out.println("-----------------");
		  String tt = IOUtils.toString(rs_s.getBinaryStream("content"), "UTF-8");
				  //bytesToString((ByteArrayInputStream) rs_s.getBinaryStream("content"));
//		  tt = tt.substring(tt.indexOf("artcontent"));
//		  tt = tt.replaceAll("[artcontent�]","");

		  List<String> list = new ArrayList<String>();
		  list.add(tt);
		  JSONArray jsonArray = new JSONArray();
		  
		  jsonArray.addAll(list);
		  String yy = jsonArray.toString();
//		  yy = yy.replaceAll("[artcontentu0010u0003u0000u0000u0000u0001u0000u0000u0000u0006u0005u0000u0000u0000�]","");
//		  yy = yy.replace("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\", "");
		  System.out.println(yy);  
		  i++;
		  System.out.println(i);
	  }
	  
	  rs_s.close();
		ps_d.close();
		conn_s.close();
	  
} catch (SQLException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
	
}
//    System.out.println("artcontent?".replaceAll("\\", ""));
//	String s = "artcontent";
//    Pattern pat = Pattern.compile("(?<=<div>).*(?=</div>)");
//    Matcher mat = pat.matcher(s);
//    while (mat.find()) {
//        System.out.println(s.replace(mat.group(), "I love xx"));
//    }
 catch (IOException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}

}

}
