Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.45 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.EventQueue;
  3. import java.awt.Font;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.io.DataOutputStream;
  7. import java.io.File;
  8. import java.io.FileInputStream;
  9. import java.io.FileNotFoundException;
  10. import java.io.FileOutputStream;
  11. import java.io.InputStream;
  12. import java.io.OutputStream;
  13. import java.sql.Blob;
  14. import java.sql.Connection;
  15. import java.sql.DriverManager;
  16. import java.sql.PreparedStatement;
  17. import java.sql.ResultSet;
  18. import java.sql.SQLException;
  19. import java.sql.Statement;
  20.  
  21. import javax.swing.JButton;
  22. import javax.swing.JFrame;
  23. import javax.swing.JLabel;
  24. import javax.swing.JTextField;
  25. import javax.swing.JComboBox;
  26. import javax.swing.JFileChooser;
  27.  
  28. @SuppressWarnings("serial")
  29. public class Operator extends JFrame implements ActionListener{
  30.  
  31. private static Operator frame = new Operator();
  32. public JLabel lblOperator;
  33. private JTextField username;
  34. private String selectedTemplate;
  35. private JLabel doc3, doc4, doc5, lblError;
  36. private JLabel doc_name3, doc_name4, doc_name5;
  37. private JButton btnAdd3, btnAdd4, btnAdd5;
  38. private String path = "jdbc:mysql://localhost:8889/DocumentsWork";
  39. private Connection connection;
  40. private Statement statement;
  41. private JFileChooser openFile = new JFileChooser();
  42.  
  43. public static void main(String[] args) {
  44. EventQueue.invokeLater(new Runnable() {
  45. public void run() {
  46. try {
  47. frame.setVisible(true);
  48. } catch (Exception e) {
  49. e.printStackTrace();
  50. }
  51. }
  52. });
  53. }
  54.  
  55. public Operator()
  56. {
  57. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  58. setBounds(100, 100, 500, 300);
  59. getContentPane().setLayout(null);
  60.  
  61. JLabel operatorLabel = new JLabel("Оператор:");
  62. operatorLabel.setBounds(12, 10, 75, 30);
  63. getContentPane().add(operatorLabel);
  64.  
  65. lblOperator = new JLabel();
  66. lblOperator.setBounds(85, 10, 147, 30);
  67. getContentPane().add(lblOperator);
  68.  
  69. JLabel userLabel = new JLabel("Пользователь:");
  70. userLabel.setBounds(12,40, 100, 30);
  71. getContentPane().add(userLabel);
  72.  
  73. JLabel patternLabel = new JLabel("Шаблон:");
  74. patternLabel.setBounds(244, 9, 65, 30);
  75. getContentPane().add(patternLabel);
  76.  
  77. username = new JTextField();
  78. username.setBounds(105, 40, 127, 30);
  79. getContentPane().add(username);
  80.  
  81. JButton btnGetData = new JButton("Получить данные");
  82. btnGetData.addActionListener(new ActionListener() {
  83. public void actionPerformed(ActionEvent e) {
  84. GetData();
  85. }
  86. });
  87. btnGetData.setBounds(280, 41, 138, 31);
  88. getContentPane().add(btnGetData);
  89.  
  90. JLabel doc1 = new JLabel("Паспорт:");
  91. doc1.setFont(new Font("Lucida Grande", Font.BOLD, 13));
  92. doc1.setBounds(12, 90, 100, 30);
  93. getContentPane().add(doc1);
  94.  
  95. JLabel doc2 = new JLabel("Заявление:");
  96. doc2.setFont(new Font("Lucida Grande", Font.BOLD, 13));
  97. doc2.setBounds(12, 115, 100, 30);
  98. getContentPane().add(doc2);
  99.  
  100. doc3 = new JLabel("СНИЛС:");
  101. doc3.setFont(new Font("Lucida Grande", Font.BOLD, 13));
  102. doc3.setBounds(12, 140, 100, 30);
  103. getContentPane().add(doc3);
  104.  
  105. doc4 = new JLabel("Регистрация:");
  106. doc4.setFont(new Font("Lucida Grande", Font.BOLD, 13));
  107. doc4.setBounds(12, 165, 100, 30);
  108. getContentPane().add(doc4);
  109.  
  110. doc5 = new JLabel("Полис:");
  111. doc5.setFont(new Font("Lucida Grande", Font.BOLD, 13));
  112. doc5.setBounds(12, 190, 100, 30);
  113. getContentPane().add(doc5);
  114.  
  115. JLabel doc_name1 = new JLabel("Документ отсутствует");
  116. doc_name1.setFont(new Font("Lucida Grande", Font.ITALIC, 13));
  117. doc_name1.setBounds(110, 90, 200, 30);
  118. getContentPane().add(doc_name1);
  119.  
  120. JLabel doc_name2 = new JLabel("Документ отсутствует");
  121. doc_name2.setFont(new Font("Lucida Grande", Font.ITALIC, 13));
  122. doc_name2.setBounds(110, 115, 200, 30);
  123. getContentPane().add(doc_name2);
  124.  
  125. doc_name3 = new JLabel("Документ отсутствует");
  126. doc_name3.setFont(new Font("Lucida Grande", Font.ITALIC, 13));
  127. doc_name3.setBounds(110, 140, 200, 30);
  128. getContentPane().add(doc_name3);
  129.  
  130. doc_name4 = new JLabel("Документ отсутствует");
  131. doc_name4.setFont(new Font("Lucida Grande", Font.ITALIC, 13));
  132. doc_name4.setBounds(110, 165, 200, 30);
  133. getContentPane().add(doc_name4);
  134.  
  135. doc_name5 = new JLabel("Документ отсутствует");
  136. doc_name5.setFont(new Font("Lucida Grande", Font.ITALIC, 13));
  137. doc_name5.setBounds(110, 190, 200, 30);
  138. getContentPane().add(doc_name5);
  139.  
  140. JButton btnAdd1 = new JButton("Загрузить");
  141. btnAdd1.setBounds(350, 90, 110, 25);
  142. btnAdd1.addActionListener(new ActionListener(){
  143. public void actionPerformed(ActionEvent e)
  144. {
  145. int res = openFile.showDialog(null, "Выберите файл: ");
  146. if (res == JFileChooser.APPROVE_OPTION)
  147. {
  148. doc_name1.setText(openFile.getSelectedFile().getName());
  149. try {
  150. InsertData("Passport", new FileInputStream(new File(openFile.getSelectedFile().getAbsolutePath())));
  151. } catch (FileNotFoundException e1) {
  152. e1.printStackTrace();
  153. }
  154. }
  155. };
  156. });
  157. getContentPane().add(btnAdd1);
  158.  
  159. JButton btnAdd2 = new JButton("Загрузить");
  160. btnAdd2.setBounds(350, 115, 110, 25);
  161. btnAdd2.addActionListener(new ActionListener(){
  162. public void actionPerformed(ActionEvent e)
  163. {
  164. int res = openFile.showDialog(null, "Выберите файл: ");
  165. if (res == JFileChooser.APPROVE_OPTION)
  166. {
  167. doc_name2.setText(openFile.getSelectedFile().getName());
  168. try {
  169. InsertData("Claim", new FileInputStream(new File(openFile.getSelectedFile().getAbsolutePath())));
  170. } catch (FileNotFoundException e1) {
  171. e1.printStackTrace();
  172. }
  173. }
  174. };
  175. });
  176. getContentPane().add(btnAdd2);
  177.  
  178. btnAdd3 = new JButton("Загрузить");
  179. btnAdd3.setBounds(350, 140, 110, 25);
  180. btnAdd3.addActionListener(new ActionListener(){
  181. public void actionPerformed(ActionEvent e) {
  182. int res = openFile.showDialog(null, "Выберите файл: ");
  183. if (res == JFileChooser.APPROVE_OPTION) {
  184. doc_name3.setText(openFile.getSelectedFile().getName());
  185. try {
  186. InsertData("SNILS", new FileInputStream(new File(openFile.getSelectedFile().getAbsolutePath())));
  187. } catch (FileNotFoundException e1) {
  188. e1.printStackTrace();
  189. }
  190. }
  191. };
  192. });
  193. getContentPane().add(btnAdd3);
  194.  
  195. btnAdd4 = new JButton("Загрузить");
  196. btnAdd4.setBounds(350, 165, 110, 25);
  197. btnAdd4.addActionListener(new ActionListener(){
  198. public void actionPerformed(ActionEvent e) {
  199. int res = openFile.showDialog(null, "Выберите файл: ");
  200. if (res == JFileChooser.APPROVE_OPTION) {
  201. doc_name4.setText(openFile.getSelectedFile().getName());
  202. try {
  203. InsertData("Registration", new FileInputStream(new File(openFile.getSelectedFile().getAbsolutePath())));
  204. } catch (FileNotFoundException e1) {
  205. e1.printStackTrace();
  206. }
  207. }
  208. };
  209. });
  210. getContentPane().add(btnAdd4);
  211.  
  212. btnAdd5 = new JButton("Загрузить");
  213. btnAdd5.setBounds(350, 190, 110, 25);
  214. btnAdd5.addActionListener(new ActionListener(){
  215. public void actionPerformed(ActionEvent e) {
  216. int res = openFile.showDialog(null, "Выберите файл: ");
  217. if (res == JFileChooser.APPROVE_OPTION) {
  218. doc_name5.setText(openFile.getSelectedFile().getName());
  219. try {
  220. InsertData("Policy", new FileInputStream(new File(openFile.getSelectedFile().getAbsolutePath())));
  221. } catch (FileNotFoundException e1) {
  222. e1.printStackTrace();
  223. }
  224. }
  225. };
  226. });
  227. getContentPane().add(btnAdd5);
  228.  
  229. String[] currentTemplate = { "Дарственная", "Бракосочетание", "Страхование жизни" };
  230. JComboBox comboBox = new JComboBox(currentTemplate);
  231. comboBox.setSelectedIndex(0);
  232. selectedTemplate = comboBox.getSelectedItem().toString();
  233. SetVisibility();
  234. comboBox.addActionListener(new ActionListener(){
  235. public void actionPerformed(ActionEvent e)
  236. {
  237. selectedTemplate = comboBox.getSelectedItem().toString();
  238. SetVisibility();
  239. }
  240. });
  241. comboBox.setBounds(322, 10, 138, 30);
  242. getContentPane().add(comboBox);
  243.  
  244. JButton btnExit = new JButton("Выход");
  245. btnExit.addActionListener(new ActionListener() {
  246. public void actionPerformed(ActionEvent e) {
  247. System.exit(0);
  248. }
  249. });
  250. btnExit.setBounds(408, 243, 86, 29);
  251. getContentPane().add(btnExit);
  252.  
  253. lblError = new JLabel("Пользователь отсутствует");
  254. lblError.setBounds(12, 73, 355, 16);
  255. lblError.setFont(new Font("Lucida Grande", Font.BOLD, 13));
  256. lblError.setForeground(Color.RED);
  257. lblError.setVisible(false);
  258. getContentPane().add(lblError);
  259. }
  260.  
  261. private void SetVisibility()
  262. {
  263. if (selectedTemplate == "Дарственная")
  264. {
  265. doc3.setVisible(false);
  266. doc4.setVisible(false);
  267. doc5.setVisible(false);
  268. doc_name3.setVisible(false);
  269. doc_name4.setVisible(false);
  270. doc_name5.setVisible(false);
  271. btnAdd3.setVisible(false);
  272. btnAdd4.setVisible(false);
  273. btnAdd5.setVisible(false);
  274. }
  275. else if (selectedTemplate == "Страхование жизни")
  276. {
  277. doc3.setVisible(true);
  278. doc4.setVisible(true);
  279. doc5.setVisible(true);
  280. doc_name3.setVisible(true);
  281. doc_name4.setVisible(true);
  282. doc_name5.setVisible(true);
  283. btnAdd3.setVisible(true);
  284. btnAdd4.setVisible(true);
  285. btnAdd5.setVisible(true);
  286. }
  287. else if (selectedTemplate == "Бракосочетание")
  288. {
  289. doc3.setVisible(true);
  290. doc4.setVisible(true);
  291. doc5.setVisible(false);
  292. doc_name3.setVisible(true);
  293. doc_name4.setVisible(true);
  294. doc_name5.setVisible(false);
  295. btnAdd3.setVisible(true);
  296. btnAdd4.setVisible(true);
  297. btnAdd5.setVisible(false);
  298. }
  299. }
  300.  
  301. public void GetData()
  302. {
  303. try
  304. {
  305. Class.forName("com.mysql.cj.jdbc.Driver");
  306. connection = DriverManager.getConnection(path, "admin", "");
  307. statement = connection.createStatement();
  308. String sql = "Select * from Users where Username='" + username.getText().toString() + "' and Type='User'";
  309. ResultSet rs = statement.executeQuery(sql);
  310. if (!rs.next())
  311. lblError.setVisible(true);
  312. else
  313. {
  314. lblError.setText("Текущий пользователь: " + username.getText().toString());
  315. lblError.setForeground(Color.GREEN);
  316. lblError.setVisible(true);
  317. }
  318. connection.close();
  319. }
  320. catch(Exception e)
  321. {
  322. System.out.print(e);
  323. }
  324. }
  325.  
  326. private void InsertData(String fileType, InputStream inputStream)
  327. {
  328. try
  329. {
  330. PreparedStatement st = null;
  331. Class.forName("com.mysql.cj.jdbc.Driver");
  332. connection = DriverManager.getConnection(path, "admin", "");
  333. statement = connection.createStatement();
  334. String str = "SELECT ID FROM Users WHERE Username ='" + username.getText().toString() + "'";
  335. ResultSet rs = statement.executeQuery(str);
  336. rs.next();
  337. String sql = "INSERT INTO AllFiles VALUES (NULL, '" + rs.getInt(1) + "','" + fileType + "',?,'" + 0 + "');";
  338. st = connection.prepareStatement(sql);
  339. st.setBinaryStream(1, inputStream);
  340. st.executeUpdate();
  341. connection.close();
  342. }
  343. catch(Exception e)
  344. {
  345. System.out.print(e);
  346. }
  347. }
  348.  
  349. @Override
  350. public void actionPerformed(ActionEvent e) {
  351. // TODO Auto-generated method stub
  352. }
  353. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement