1、读取含有Clob字段的数据
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import oracle.jdbc.rowset.OracleSerialClob;
import org.apache.commons.io.IOUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.SqlRowSetResultSetExtractor;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.jdbc.support.rowset.SqlRowSetMetaData;
public class QueryClob extends JdbcDaoSupport {
public QueryClob(JdbcTemplate jdbcTemplate) {
this.setJdbcTemplate(jdbcTemplate);
}
public List<Map<String, Object>> processClob(String sql) throws SQLException, IOException {
List<Map<String, Object>> objects = new ArrayList<Map<String, Object>>();
SqlRowSet rs = (SqlRowSet) this.getJdbcTemplate().query(sql,new SqlRowSetResultSetExtractor());
while (rs.next()) {
Map<String, Object> map = assembleMap(rs);
objects.add(map);
}
return objects;
}
private Map<String, Object> assembleMap(SqlRowSet rs) throws SQLException,IOException {
Map<String, Object> map = new HashMap<String, Object>();
SqlRowSetMetaData meta = rs.getMetaData();
String[] columnNames = meta.getColumnNames();
int i = 0;
for (String columnName : columnNames) {
Object object = rs.getObject(columnName);
if (object != null && meta.getColumnType(i + 1) == Types.CLOB) {
OracleSerialClob obj = (OracleSerialClob) object;
Reader is = obj.getCharacterStream();// 得到流
BufferedReader br = new BufferedReader(is);
String s = br.readLine();
StringBuffer sb = new StringBuffer();
while (s != null) {// 执行循环将字符串全部取出付值给StringBuffer由StringBuffer转成STRING
sb.append(s);
s = br.readLine();
}
map.put(columnName, sb.toString());
IOUtils.closeQuietly(br);
IOUtils.closeQuietly(is);
} else if (object != null&& meta.getColumnType(i + 1) == Types.NUMERIC) {
BigDecimal number = (BigDecimal) object;
map.put(columnName, number);
} else {
map.put(columnName, object);
}
++i;
}
return map;
}
}
2、去除html标签
public static String filterHtml(String input) {
if (input == null || input.trim().equals("")) {
return "";
}
// 去掉所有html元素,
String str = input.replaceAll("\\&[a-zA-Z]{1,10};", "").replaceAll("<[^>]*>", "");
str = str.replaceAll("[(/>)<]", "");
return str;
}
3、去除重复
public static void unique(Collection list ){
HashSet set = new HashSet(list);
list.clear();
list.addAll(set);
}
4、文件打包
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
public class ZipUtil {
private static String parentPath = "";
public static void zipFile(String parentPath, String[] fileNames) {
ZipUtil.parentPath = parentPath;
File[] files = new File[fileNames.length];
// 初始化文件
for (int i = 0; i < fileNames.length; i++)
files[i] = new File(parentPath + fileNames[i]);
// 压缩后的文件
File zipFile = files[0];
ZipOutputStream out = null;
try {
out = new ZipOutputStream(new FileOutputStream(zipFile));
out.setEncoding("gbk");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
for (int i = 1; i < files.length; i++) {
File file = files[i];
zipFile(file, out);
}
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 根据输入的文件与输出流对文件进行打包
*
* @param File
* @param org.apache.tools.zip.ZipOutputStream
*/
public static void zipFile(File inputFile, ZipOutputStream ouputStream) {
try {
if (inputFile.exists()) {
/**
* 如果是目录的话这里是不采取操作的, 至于目录的打包正在研究中
*/
if (inputFile.isFile()) {
FileInputStream IN = new FileInputStream(inputFile);
BufferedInputStream bins = new BufferedInputStream(IN, 1024);
ZipEntry entry = new ZipEntry(inputFile.getName());
ouputStream.putNextEntry(entry);
// 向压缩文件中输出数据
int nNumber;
byte[] buffer = new byte[1024];
while ((nNumber = bins.read(buffer)) != -1) {
ouputStream.write(buffer, 0, nNumber);
}
// 关闭创建的流对象
bins.close();
IN.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
5、时钟的java实现
import java.awt.BasicStroke;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TimeDemo extends JFrame{
private JPanel pan;
private double r1=160,r2=165,rm1=158,rm2=167,hr=100,mr=130,sr=167;
public TimeDemo(){
super("Time demo");
this.setBounds(200,200,410,430);
getMinLab();
pan=new MyPanel();
this.add(pan);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyThread th=new MyThread();
th.setDaemon(true);
th.start();
}
public static void main(String[] args) {
new TimeDemo();
}
class MyPanel extends JPanel{
public void paint(Graphics g){
g.clearRect(0,0,this.getWidth(),this.getHeight());
Graphics2D g2=(Graphics2D)g;
BasicStroke bs=null;
// String num[]={"1","2","3","4","5","6","7","8","9","10","11","12"};
// 打开抗锯齿
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
Line2D line[]=getMinLab();
for(int i=0;i<line.length;i++){
bs=new BasicStroke(2,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
g2.setStroke(bs);
if(i%5==0){
bs=new BasicStroke(4,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
g2.setStroke(bs);
}
g2.draw(line[i]);
}
//计算时针
Date date=new Date();
int s=date.getSeconds();
double m=date.getMinutes()+s/60.0;
double h=date.getHours()+m/60.0;
System.out.println(h);
double hy=Math.sin(Math.PI/6*(h%12-3))*hr+200;
double hx=Math.cos(Math.PI/6*(h%12-3))*hr+200;
bs=new BasicStroke(6,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
g2.setStroke(bs);
Line2D.Double hline=new Line2D.Double(200,200,hx,hy);
g2.draw(hline);
// 计算分针
double my=Math.sin(Math.PI/30*(m-15))*mr+200;
double mx=Math.cos(Math.PI/30*(m-15))*mr+200;
bs=new BasicStroke(4,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
g2.setStroke(bs);
Line2D.Double mline=new Line2D.Double(200,200,mx,my);
g2.draw(mline);
// 计算秒针
double sy=Math.sin(Math.PI/30*(s-15))*sr+200;
double sx=Math.cos(Math.PI/30*(s-15))*sr+200;
bs=new BasicStroke(2,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
g2.setStroke(bs);
Line2D.Double sline=new Line2D.Double(200,200,sx,sy);
g2.draw(sline);
//画表盘
Ellipse2D.Double el=new Ellipse2D.Double(10,10,380,380);
g2.draw(el);
}
}
public Line2D[] getMinLab(){
Line2D[] lineM=new Line2D[60];
for(int i=0;i<lineM.length;i++){
double x1,y1,x2,y2;
double b1 = Math.sin(Math.PI / 30 * i);
double b2 = Math.cos(Math.PI / 30 * i);
if (i % 5 != 0) {
y1 = b1 * r1 + 200;
y2 = b1 * r2 + 200;
x1 = b2 * r1 + 200;
x2 = b2 * r2 + 200;
} else {
y1 = b1 * rm1 + 200;
y2 = b1 * rm2 + 200;
x1 = b2 * rm1 + 200;
x2 = b2 * rm2 + 200;
}
lineM[i]=new Line2D.Double(x1,y1,x2,y2);
// System.out.println(i+"-------("+x1+","+y1+"),("+x2+","+y2+")");
}
return lineM;
}
class MyThread extends Thread{
public void run(){
while(true){
pan.repaint();
try {
this.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
// public void refresh(){
// pan.repaint();
// this.validate();
// }
}