SqlSeverTicket.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package com.gyee;
  2. import net.coobird.thumbnailator.Thumbnails;
  3. import javax.imageio.ImageIO;
  4. import java.awt.geom.AffineTransform;
  5. import java.awt.image.AffineTransformOp;
  6. import java.awt.image.BufferedImage;
  7. import java.io.*;
  8. import java.sql.*;
  9. import java.util.*;
  10. /**
  11. * 图形开票 人员电子签名 数据库人员处理
  12. */
  13. public class SqlSeverTicket {
  14. public static void main(String []args) throws SQLException {
  15. String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
  16. //加载JDBC驱动
  17. String dbURL = "jdbc:sqlserver://10.155.32.2:1433;DatabaseName=fdeam";
  18. //连接服务器和数据库
  19. String userName = "sa"; //默认用户名
  20. String userPwd = "Gyee@321#!"; //密码
  21. Connection conn = null;
  22. try {
  23. // Class.forName(driverName);
  24. conn = DriverManager.getConnection(dbURL, userName, userPwd);
  25. //System.out.println("Connection Successful!");
  26. //如果连接成功 控制台输出Connection Successful!
  27. Map<String, List> labor = getLabor(conn);
  28. setLabor(conn, labor);
  29. // List<String> list = getLabors(conn);
  30. // for(int i = 0; i < list.size(); i++){
  31. // setImage(conn, "D:/document/两票相关材料/人员电子签名/电子签名thumb2/" + list.get(i).toLowerCase() +".png", list.get(i).toLowerCase());
  32. // }
  33. // setImage(conn, "D:/document/两票相关材料/zhanghy.png", "zhanghy");
  34. // String fileName = "wangbing.png";
  35. //
  36. String filePath1 = "D:/document/两票相关材料/人员电子签名/";
  37. String filePath2 = "D:/document/两票相关材料/";
  38. // batchThumb(filePath1, filePath2);
  39. // singleThumb(filePath1, filePath2, "chenl.png");
  40. setImage(conn, "D:/document/两票相关材料/chenl.png", "chenl");
  41. } catch (Exception e) {
  42. e.printStackTrace();
  43. } finally {
  44. conn.close();
  45. }
  46. }
  47. // 获取人员信息
  48. public static Map<String, List> getLabor(Connection conn){
  49. Map<String, List> labors = new HashMap<>();
  50. try {
  51. PreparedStatement ps = conn.prepareStatement("select * from LABOR");
  52. ResultSet rs = ps.executeQuery();
  53. while(rs.next()){
  54. List<String> list = new ArrayList<>();
  55. String laborNum = rs.getString("LABORNUM");
  56. String laborName = rs.getString("LABORNAME");
  57. String deptNum = rs.getString("DEPTNUM");
  58. list.add(0, laborName);
  59. list.add(1, deptNum);
  60. labors.put(laborNum, list);
  61. }
  62. } catch (SQLException e) {
  63. e.printStackTrace();
  64. }
  65. return labors;
  66. }
  67. public static List<String> getLabors(Connection conn){
  68. List<String> list = new ArrayList<>();
  69. try {
  70. PreparedStatement ps = conn.prepareStatement("select * from SIGNPICTURE");
  71. ResultSet rs = ps.executeQuery();
  72. while(rs.next()){
  73. String laborNum = rs.getString("LABORNUM");
  74. list.add(laborNum);
  75. }
  76. } catch (SQLException e) {
  77. e.printStackTrace();
  78. }
  79. return list;
  80. }
  81. // signpicture表插入人员信息
  82. public static void setLabor(Connection conn, Map map) throws SQLException {
  83. List<String> lists = new ArrayList<>();
  84. try {
  85. PreparedStatement ps = conn.prepareStatement("select * from SIGNPICTURE");
  86. ResultSet rs = ps.executeQuery();
  87. while(rs.next()){
  88. String laborNum = rs.getString("LABORNUM").toLowerCase();
  89. lists.add(laborNum);
  90. }
  91. } catch (SQLException e) {
  92. e.printStackTrace();
  93. }
  94. PreparedStatement ps = null;
  95. try {
  96. ps = conn.prepareStatement("Insert into SIGNPICTURE (LABORNUM,LABORNAME,DEPTNUM) values (?,?,?)");
  97. Iterator<Map.Entry<String, List>> entries = map.entrySet().iterator();
  98. while(entries.hasNext()){
  99. Map.Entry<String, List> entry = entries.next();
  100. String key = entry.getKey();
  101. if(!lists.contains(key.toLowerCase()))
  102. {
  103. List list = entry.getValue();
  104. ps.setString(1,key);
  105. ps.setString(2, String.valueOf(list.get(0)));
  106. ps.setString(3, String.valueOf(list.get(1)));
  107. // ps.setBinaryStream(3,fis,(int)file.length());
  108. ps.executeUpdate();
  109. }
  110. }
  111. ps.close();
  112. } catch (SQLException e) {
  113. e.printStackTrace();
  114. } finally {
  115. ps.close();
  116. }
  117. }
  118. /**
  119. * 写入图片
  120. * @param conn
  121. * @param filePath
  122. */
  123. public static void setImage(Connection conn, String filePath, String laborNum)
  124. {
  125. FileInputStream fis=null;
  126. File file = new File(filePath);
  127. try{
  128. fis = new FileInputStream(file);
  129. }catch(FileNotFoundException e){
  130. //System.out.println("Not find file!");
  131. }
  132. try{
  133. // PreparedStatement ps = conn.prepareStatement("Insert into SIGNPICTURE (IMAGE) values (?)");
  134. PreparedStatement ps = conn.prepareStatement("update SIGNPICTURE set IMAGE = ? where LABORNUM = '"+laborNum+"'");
  135. ps.setBinaryStream(1,fis,(int)file.length());
  136. ps.executeUpdate();
  137. ps.close();
  138. fis.close();
  139. //System.out.println("写进去了!");
  140. }catch(Exception e){
  141. //System.out.println("fis cann't cloase!");
  142. }
  143. }
  144. // 图片签名处理
  145. private static void batchThumb(String filePath1, String filePath2){
  146. File file = new File(filePath1);
  147. File[] files = file.listFiles();
  148. if(files != null){
  149. for(int i = 0; i < files.length; i++){
  150. String fileName = files[i].getName();
  151. try {
  152. Thumbnails.of(filePath1 + fileName)
  153. .size(100, 40)
  154. .toFile(filePath2 + fileName);
  155. } catch (IOException e) {
  156. e.printStackTrace();
  157. } finally {
  158. continue;
  159. }
  160. }
  161. }
  162. }
  163. // 图片签名处理
  164. private static void singleThumb(String filePath1, String filePath2, String fileName) throws IOException {
  165. Thumbnails.of(filePath1 + fileName)
  166. .size(100, 40)
  167. .toFile(filePath2 + fileName);
  168. }
  169. }