//查询操作jdbc.properties package testjdbc; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class JDBCProperties { public static void main(String[] args) throws IOException { jdbc_Properties("jdbc.properties"); } public static void jdbc_Properties(String PropertiesName) throws IOException{ Properties pro = new Properties();//属性集合对象 // URL url = Thread.currentThread().getContextClassLoader().getResource("prop.properties");//获取项目中文件的路径 InputStream path =Thread.currentThread().getContextClassLoader().getResourceAsStream(PropertiesName);//获取路径并转换成流 // FileInputStream fis = new FileInputStream("属性文件创建在电脑上"); try { // pro.load(fis);//将属性文件流装载到Properties对象中 pro.load(path); // fis.close(); System.out.println(pro.getProperty("jdbc.default_db.driver")); System.out.println(pro.getProperty("jdbc.default_db.url")); System.out.println(pro.getProperty("jdbc.default_db.username")); System.out.println(pro.getProperty("jdbc.default_db.password")); System.out.println(pro.getProperty("jdbc.sql2000_db.driver")); System.out.println(pro.getProperty("jdbc.sql2000_db.url2")); System.out.println(pro.getProperty("jdbc.sql2000_db.username")); System.out.println(pro.getProperty("jdbc.sql2000_db.password")); /* pro.setProperty("shuzi", "1111");//往属性文件插值 System.out.println(pro.getProperty("shuzi"));*/ } catch (IOException e) { e.printStackTrace(); System.out.println(pro.getProperty("读取文件jdbc.properties异常")); }finally{ path.close(); } } } ========================================================================================= //用定时器定时执行main方法 package synthesizeTest; import java.io.IOException; import java.sql.SQLException; import java.util.Timer; import ThreadTime.MyTask; public class synthesize{ public static void main(String[] args) throws IOException, SQLException{ //定时器 Timer t = new Timer(); // 定义任务 MyTask myTask = new MyTask(); // 设置任务的执行,5秒后开始,每5秒重复调用一次 t.schedule(myTask, 0, 5000); /* * MySQL的JDBC URL编写方式:jdbc:mysql://主机名称:连接端口/数据库的名称?参数=值 * 避免中文乱码要指定useUnicode和characterEncoding * 执行数据库操作之前要在数据库管理系统上创建一个数据库,名字自己定, * 下面语句之前就要先创建javademo数据库 * */ /*Connection conn = null; String sql; String url = "jdbc:mysql://localhost:3306/javademo?user=root&password=zhxush&useUnicode=true&characterEncoding=UTF8"; try { // 之所以要使用下面这条语句,是因为要使用MySQL的驱动,所以我们要把它驱动起来, // 可以通过Class.forName把它加载进去,也可以通过初始化来驱动起来,下面三种形式都可以 Class.forName("com.mysql.jdbc.Driver");// 动态加载mysql驱动 // com.mysql.jdbc.Driver driver = new com.mysql.jdbc.Driver(); // new com.mysql.jdbc.Driver(); System.out.println("成功加载MySQL驱动程序"); // 一个Connection代表一个数据库连接 conn = DriverManager.getConnection(url); // Statement里面带有很多方法,比如executeUpdate可以实现插入,更新和删除等 Statement stmt = conn.createStatement(); sql = "create table student(id char(100),name varchar(20),age varchar(20),sex varchar(20),primary key(id))"; int result = stmt.executeUpdate(sql);// executeUpdate语句会返回一个受影响的行数,如果返回-1就没有成功 if (result != -1) { System.out.println("创建数据表成功"); sql = "insert into student(id,name,age,) values("+ +","+ +","+ +","+ +")"; result = stmt.executeUpdate(sql); sql = "insert into student(NO,name) values('2012002','周小俊')"; result = stmt.executeUpdate(sql); sql = "select * from student"; ResultSet rs = stmt.executeQuery(sql);// executeQuery会返回结果的集合,否则返回空值 System.out.println("id\t\t\t\t\t\t姓名\t\t年龄\t\t性别"); while (rs.next()) { System.out.println(rs.getString(1) + "\t" + rs.getString(2)+"\t"+rs.getString(3)+"\t"+rs.getString(4));// 入如果返回的是int类型可以用getInt() } } } catch (SQLException e) { System.out.println("MySQL操作错误"); e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { conn.close(); }*/ /*//存放照片的文件路径 File file = new File("D:/test/"); //存放照片的文件 File [] files = file.listFiles(); InputStream fis = null; byte[] imageByteArray = null; //把图片写入字节流写入photo.txt FileWriter fileWriter=new FileWriter("D:/test/photo.txt"); BASE64Encoder encoder = new BASE64Encoder(); for (int i = 0; i < files.length; i++){ //File file1 = files[i]; try { fis = new FileInputStream(files[i]); imageByteArray= IOUtils.toByteArray(fis); encoder.encode(imageByteArray); //System.out.println("字节流:"+encoder); //当文件是图片文件时将图片转换成64位字节流,然后读入photo.txt String fileName = files[i].getName(); if(fileName.trim().toLowerCase().endsWith(".png")||fileName.trim().toLowerCase().endsWith(".jpg")||fileName.trim().toLowerCase().endsWith(".jpeg")) { //System.out.println("是图片"+i+"文件"+fileName); fileWriter.write(String.valueOf(encoder)+" "); //解码,看是否能获取图片 BASE64Decoder decoder = new BASE64Decoder(); byte[] bytes = decoder.decodeBuffer(String.valueOf(encoder)); byteImage(bytes,path); } //System.out.println("字节流:"+fileName); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ fis.close(); imageByteArray.clone(); } } fileWriter.flush(); fileWriter.close(); */ } //byte数组到图片 /* public static void byteImage(byte[] data,String path) throws IOException{ if(data.length<3||path.equals("")) return; //FileWriter fileWriter= new FileWriter(path); try{ //FileImageOutputStream imageOutput = new FileImageOutputStream(fileWriter); OutputStream os = new FileOutputStream(String.valueOf(path)); os.write(data);//将字节一个个写入文件 //imageOutput.write(data, 0, data.length); //imageOutput.close(); os.close(); //System.out.println("地址:" + path); } catch(Exception ex) { System.out.println("Exception:" + ex); ex.printStackTrace(); } }*/ } ------------------------------------------------------- // 该处是吧图片转为64位字节符(还存在着问题) package synthesizeTest; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import ThreadTime.MyTask; public class test { public static void main(String[] args) { test myTask = new test(); try { //myTask.readPhoto1(); myTask.writePhoto(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void readPhoto () throws IOException{ InputStream inputStream = new FileInputStream("D:/test/补充任务.jpg"); OutputStream outputStream=new FileOutputStream("D:/test/补充任务1.jpg"); BASE64Encoder encoder = new BASE64Encoder(); byte[] imageByteArray = new byte[inputStream.available()]; inputStream.read(imageByteArray); //encoder.encode(imageByteArray); BASE64Decoder decoder = new BASE64Decoder(); byte[] byteTxt = decoder.decodeBuffer(encoder.encode(imageByteArray)); outputStream.write(byteTxt); inputStream.close(); outputStream.close(); } public void readPhoto1() throws IOException{ InputStream inputStream = new FileInputStream("D:/test/补充任务.jpg"); OutputStream outputStream=new FileOutputStream("D:/test/photo.txt"); BASE64Encoder encoder = new BASE64Encoder(); byte[] imageByteArray = new byte[inputStream.available()]; inputStream.read(imageByteArray); encoder.encode(imageByteArray); outputStream.write(imageByteArray); inputStream.close(); outputStream.close(); } public void writePhoto () throws IOException{ OutputStream outputStream = new FileOutputStream("D:/test/补充任务1.jpg"); InputStream inputStream=new FileInputStream("D:/test/photo.txt"); byte[] imageByteArray = new byte[inputStream.available()]; inputStream.read(imageByteArray); String s = imageByteArray.toString(); BASE64Decoder decoder = new BASE64Decoder(); byte[] byteTxt = decoder.decodeBuffer(s); for (int i=0;i<byteTxt.length;++i){ if(byteTxt[i]<0){ byteTxt[i]+=256; } } outputStream.write(imageByteArray); inputStream.close(); outputStream.flush(); outputStream.close(); } } -------------------------------------------------------------------- //用ireport写成模板用java生成PDF package PDFTest; import java.io.File; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.Map; import com.tecsun.framework.unitl.Report; public class CreatePDF { public static void main(String[] args){ createPDF("D:/test/content.jasper","D:/test/"); } public static void createPDF(String modelFile,String tempDir){ // String modelFile = null;//模板文件路径 //String tempDir = null;//生成pdf文件目录 File tempDirFile = new File(tempDir); if (!tempDirFile.exists()) { tempDirFile.mkdirs(); } CreatePdfVO createPdfVO = new CreatePdfVO(); Map<String, String> reportData = new HashMap<String, String>(); /*reportData.put("id", CreatePDF.isEmptyStr(createPdfVO.getName())?"":createPdfVO.getName()); reportData.put("name", CreatePDF.isEmptyStr(createPdfVO.getName())?"":createPdfVO.getName()); reportData.put("age", CreatePDF.isEmptyStr(createPdfVO.getName())?"":createPdfVO.getName()); reportData.put("sex", CreatePDF.isEmptyStr(createPdfVO.getName())?"":createPdfVO.getName());*/ reportData.put("id", CreatePDF.isEmptyStr(createPdfVO.getId())?"":"1"); reportData.put("name", CreatePDF.isEmptyStr(createPdfVO.getName())?"":"张三"); reportData.put("age", CreatePDF.isEmptyStr(createPdfVO.getAge())?"":"20"); reportData.put("sex", CreatePDF.isEmptyStr(createPdfVO.getSex())?"":"女"); //添加数据 //String pdfPath = tempDir +"content"+new SimpleDateFormat("yyyyMMddHHmmsss").format(new Date()) + ".pdf"; String pdfPath = tempDir +"content.pdf"; boolean is; try { is= Report.createPDFReport(modelFile, pdfPath, reportData); System.out.println("生成PDF成功!"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("生成PDF异常!"); } } public static boolean isEmptyStr(Object str){ return str == null || str.toString().trim().length() < 1 ? true : false; } public static String objectToString(Object str){ if (null == str){ return null; }else{ return str.toString(); } } } ---------------------------------------------------------- //实体类 package PDFTest; public class CreatePdfVO { private String id; private String name; private String age; private String sex; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getName() { return name; } public void setName(String name) { this.name = name; } } ================================================ //请看下一篇文章 ================================================