SqlSeverTicket.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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/两票相关材料/zhongmy.png", "zhongmy");
  34. // String fileName = "wangbing.png";
  35. //
  36. // String filePath1 = "D:/document/两票相关材料/人员电子签名/";
  37. // String filePath2 = "D:/document/两票相关材料/";
  38. // batchThumb(filePath1, filePath2);
  39. // singleThumb(filePath1, filePath2, "zhongmy.png");
  40. } catch (Exception e) {
  41. e.printStackTrace();
  42. } finally {
  43. conn.close();
  44. }
  45. }
  46. // 获取人员信息
  47. public static Map<String, List> getLabor(Connection conn){
  48. Map<String, List> labors = new HashMap<>();
  49. try {
  50. PreparedStatement ps = conn.prepareStatement("select * from LABOR");
  51. ResultSet rs = ps.executeQuery();
  52. while(rs.next()){
  53. List<String> list = new ArrayList<>();
  54. String laborNum = rs.getString("LABORNUM");
  55. String laborName = rs.getString("LABORNAME");
  56. String deptNum = rs.getString("DEPTNUM");
  57. list.add(0, laborName);
  58. list.add(1, deptNum);
  59. labors.put(laborNum, list);
  60. }
  61. } catch (SQLException e) {
  62. e.printStackTrace();
  63. }
  64. return labors;
  65. }
  66. public static List<String> getLabors(Connection conn){
  67. List<String> list = new ArrayList<>();
  68. try {
  69. PreparedStatement ps = conn.prepareStatement("select * from SIGNPICTURE");
  70. ResultSet rs = ps.executeQuery();
  71. while(rs.next()){
  72. String laborNum = rs.getString("LABORNUM");
  73. list.add(laborNum);
  74. }
  75. } catch (SQLException e) {
  76. e.printStackTrace();
  77. }
  78. return list;
  79. }
  80. // signpicture表插入人员信息
  81. public static void setLabor(Connection conn, Map map) throws SQLException {
  82. List<String> lists = new ArrayList<>();
  83. try {
  84. PreparedStatement ps = conn.prepareStatement("select * from SIGNPICTURE");
  85. ResultSet rs = ps.executeQuery();
  86. while(rs.next()){
  87. String laborNum = rs.getString("LABORNUM");
  88. lists.add(laborNum);
  89. }
  90. } catch (SQLException e) {
  91. e.printStackTrace();
  92. }
  93. PreparedStatement ps = null;
  94. try {
  95. ps = conn.prepareStatement("Insert into SIGNPICTURE (LABORNUM,LABORNAME,DEPTNUM) values (?,?,?)");
  96. Iterator<Map.Entry<String, List>> entries = map.entrySet().iterator();
  97. while(entries.hasNext()){
  98. Map.Entry<String, List> entry = entries.next();
  99. String key = entry.getKey();
  100. if(!lists.contains(key))
  101. {
  102. List list = entry.getValue();
  103. ps.setString(1,key);
  104. ps.setString(2, String.valueOf(list.get(0)));
  105. ps.setString(3, String.valueOf(list.get(1)));
  106. // ps.setBinaryStream(3,fis,(int)file.length());
  107. ps.executeUpdate();
  108. }
  109. }
  110. ps.close();
  111. } catch (SQLException e) {
  112. e.printStackTrace();
  113. } finally {
  114. ps.close();
  115. }
  116. }
  117. /**
  118. * 写入图片
  119. * @param conn
  120. * @param filePath
  121. */
  122. public static void setImage(Connection conn, String filePath, String laborNum)
  123. {
  124. FileInputStream fis=null;
  125. File file = new File(filePath);
  126. try{
  127. fis = new FileInputStream(file);
  128. }catch(FileNotFoundException e){
  129. System.out.println("Not find file!");
  130. }
  131. try{
  132. // PreparedStatement ps = conn.prepareStatement("Insert into SIGNPICTURE (IMAGE) values (?)");
  133. PreparedStatement ps = conn.prepareStatement("update SIGNPICTURE set IMAGE = ? where LABORNUM = '"+laborNum+"'");
  134. ps.setBinaryStream(1,fis,(int)file.length());
  135. ps.executeUpdate();
  136. ps.close();
  137. fis.close();
  138. System.out.println("写进去了!");
  139. }catch(Exception e){
  140. System.out.println("fis cann't cloase!");
  141. }
  142. }
  143. // 图片签名处理
  144. private static void batchThumb(String filePath1, String filePath2){
  145. File file = new File(filePath1);
  146. File[] files = file.listFiles();
  147. if(files != null){
  148. for(int i = 0; i < files.length; i++){
  149. String fileName = files[i].getName();
  150. try {
  151. Thumbnails.of(filePath1 + fileName)
  152. .size(100, 40)
  153. .toFile(filePath2 + fileName);
  154. } catch (IOException e) {
  155. e.printStackTrace();
  156. } finally {
  157. continue;
  158. }
  159. }
  160. }
  161. }
  162. // 图片签名处理
  163. private static void singleThumb(String filePath1, String filePath2, String fileName) throws IOException {
  164. Thumbnails.of(filePath1 + fileName)
  165. .size(100, 40)
  166. .toFile(filePath2 + fileName);
  167. }
  168. }