定时执行sql生成图片保存到一个指定的目录下,动态传入时间参数
需要一个动态从oracle数据库读取sql,sql放在xml文件中,现在生成图片的已经实现了,怎么才能定时启动这个,把图片的读取的是这个时间的5天前的数据
public class ImageGeneratorUtil{
private final static Logger log = Logger.getLogger(ImageGeneratorUtil.class);
private static final long serialVersionUID = -3938318741402322164L;
private static BufferedImage image;
public static BufferedImage genaralImage(String columnamessql, String columsql) {
try {
String columnNames[] =ImageDataUtil.dataColumnames(columnamessql);
String[][] rowData =ImageDataUtil.dataColum(columsql);
DefaultTableModel model = new DefaultTableModel(rowData, columnNames) {
public boolean isCellEditable(int row, int column) {
return false;
}
};
DefaultTableCellRenderer tcr = new DefaultTableCellRenderer() {
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
if(row == 2 || row == 8)
{
setBackground(Color.gray);
setHorizontalAlignment(JLabel.CENTER);
}
else if (value != null && value.toString().trim().length() != 0 && column != 0) {
//setBackground(Color.red);
setHorizontalAlignment(JLabel.RIGHT);
} else {
setBackground(Color.white);
setHorizontalAlignment(JLabel.LEFT);
}
return super.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, column);
}
};
//Jtable大小
int table_width = 400;
int table_hight = 380;
//图片生成后再压缩后大小
int image_width = 480;
int image_hight = 480;
JTable table = new JTable(model);
table.setPreferredSize(new Dimension(table_width, table_hight));
//设置列表现器------------------------//
for (int i = 0; i < columnNames.length; i++) {
table.getColumn(columnNames[i]).setCellRenderer(tcr);
}
int width = 180;
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
TableColumn firsetColumn = table.getColumnModel().getColumn(0);
firsetColumn.setPreferredWidth(width);
firsetColumn.setMinWidth(width);
firsetColumn.setMinWidth(width);
JFrame f = new JFrame();
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setAutoscrolls(false);
f.getContentPane().add(scrollPane);
f.setSize(table_width, table_hight);
f.pack();
//f.setVisible(true);
image = createImage(table);
if (f != null) {
f.dispose();
}
//对图片进行压缩处理,输出到指定目录
ImageGeneratorUtil.reduceImg(image
, "d:/test.png"
, image_width, image_hight);
} catch (Exception ex) {
ex.printStackTrace();
}
return image;
}
public static BufferedImage createImage(JTable table) {
//get the table header component
JTableHeader tableHeaderComp = table.getTableHeader();
int totalWidth = table.getPreferredSize().width;
int totalHeight = table.getPreferredSize().height;
BufferedImage tableImage = new BufferedImage(totalWidth, totalHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g2D = (Graphics2D) tableImage.createGraphics();
g2D.setColor(Color.WHITE);
g2D.fillRect(0, 0, totalWidth, totalHeight);
g2D.translate(0, 0);
tableHeaderComp.paint(g2D);
g2D.translate(0, tableHeaderComp.getPreferredSize().height);
table.paint(g2D);
g2D.dispose();
//return the table image
return tableImage;
}
//压缩图片
public static void reduceImg(BufferedImage bufferedImage, String imgdist, int widthdist,
int heightdist) {
try {
BufferedImage tag = new BufferedImage(widthdist,
heightdist, BufferedImage.TYPE_INT_RGB);
/*
* Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的
* 优先级比速度高 生成的图片质量比较好 但速度慢
*/
tag.getGraphics().drawImage(
bufferedImage.getScaledInstance(widthdist, heightdist,
Image.SCALE_SMOOTH), 0, 0, null);
ImageIO.write(tag, "png", new File(imgdist));
} catch (IOException e) {
log.error("reduceImg error :" + e.toString());
}
}
}
,
[解决办法]
定时器可以用以下几种方式
1)java.util.Timer&TImerTask
2) ScheduledExecutorService
3) quartz
[解决办法]
定时器
public class test extends TimerTask { @Override public void run() { System.out.println("asdsdasd"); Timer timer = new Timer() ; timer.schedule(new test(), new Date(System.currentTimeMillis()+2000)) ; } public static void main(String[] args) throws Exception { Timer timer = new Timer() ; timer.schedule(new test(), new Date(System.currentTimeMillis()+10000)) ;}