Advertisement
Guest User

Untitled

a guest
Jun 7th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.98 KB | None | 0 0
  1. import javax.swing.*;
  2. import javax.swing.event.ListSelectionEvent;
  3. import javax.swing.event.ListSelectionListener;
  4. import java.awt.*;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.sql.*;
  8.  
  9. /**
  10. * Created by svgood on 07.06.17.
  11. */
  12. public class Main {
  13.  
  14. public static Connection connection;
  15. public static Statement stmt = null;
  16. public static ResultSet rs = null;
  17. static Boolean found = false;
  18. static int cur_item = 0;
  19.  
  20. public static void main (String [] args){
  21. String url = "jdbc:mysql://localhost:3306/med"; //Я хуй знает, надо ли туту что-то менять
  22. String username = "root";
  23. String password = "Svgood22";
  24.  
  25.  
  26. try {
  27. Class.forName("com.mysql.jdbc.Driver").newInstance();
  28. } catch (Exception ex) {
  29. ex.printStackTrace();
  30. }
  31. try {
  32. connection = DriverManager.getConnection(url, username, password);
  33. System.out.println("Connected");
  34. } catch (SQLException e){
  35. System.out.println("SQLException: " + e.getMessage());
  36. System.out.println("SQLState: " + e.getSQLState());
  37. System.out.println("VendorError: " + e.getErrorCode());
  38. }
  39.  
  40. JFrame frame;
  41. JPanel panel;
  42. JList list ;
  43. JTextField field1 ;
  44. JTextField field2 ;
  45. JButton btn_find ;
  46. JButton btn_last ;
  47. JButton btn_next ;
  48. JTextField f1 = new JTextField(10);
  49. JTextField f2 = new JTextField(10);
  50. JTextField f3 = new JTextField(10);
  51. JTextField f4 = new JTextField(10);
  52. JTextField f5 = new JTextField(10);
  53.  
  54.  
  55. DefaultListModel model = new DefaultListModel();
  56. for (int i = 0; i < 3; i++)
  57. model.addElement("Растение номер " + (i+1));
  58.  
  59. frame = new JFrame("DataBase");
  60. panel = new JPanel();
  61. list = new JList(model);
  62. field1 = new JTextField(15);
  63. field2 = new JTextField(15);
  64. btn_find = new JButton("Найти");
  65. btn_last = new JButton("<<");
  66. btn_next = new JButton(">>");
  67.  
  68. panel.setLayout(new GridBagLayout());
  69. GridBagConstraints c = new GridBagConstraints();
  70. Insets insets = new Insets(10,10,10,10);
  71. c.insets = insets;
  72. c.gridx = 0;
  73. c.gridy = 0;
  74. c.gridheight = 3;
  75. c.gridwidth = 3;
  76. c.ipadx = 300;
  77. c.ipady = 200;
  78. panel.add(list, c);
  79. c.gridwidth = 2;
  80. c.ipadx = 0;
  81. c.ipady = 0;
  82. c.gridheight = 1;
  83. c.gridx = 3;
  84. panel.add(field1, c);
  85. c.gridy = 1;
  86. panel.add(field2, c);
  87. c.gridy = 2;
  88. panel.add(btn_find,c);
  89. c.gridx = 0;
  90. c.gridy = 3;
  91. c.gridwidth = 1;
  92. panel.add(f1, c);
  93. c.gridx = 1;
  94. panel.add(f2, c);
  95. c.gridx = 2;
  96. panel.add(f3, c);
  97. c.gridx = 3;
  98. panel.add(f4, c);
  99. c.gridx = 4;
  100. panel.add(f5, c);
  101. c.gridwidth = 3;
  102. c.anchor = GridBagConstraints.EAST;
  103. c.gridy = 4;
  104. c.gridx = 0;
  105. panel.add(btn_last, c);
  106. c.anchor = GridBagConstraints.WEST;
  107. c.gridx = 3;
  108. panel.add(btn_next, c);
  109.  
  110. btn_last.setVisible(false);
  111. btn_next.setVisible(false);
  112. f1.setVisible(false);
  113. f2.setVisible(false);
  114. f3.setVisible(false);
  115. f4.setVisible(false);
  116. f5.setVisible(false);
  117.  
  118. list.addListSelectionListener(new ListSelectionListener() {
  119. @Override
  120. public void valueChanged(ListSelectionEvent e) {
  121. try {
  122. String text;
  123. String exp;
  124. stmt = connection.createStatement();
  125. rs = stmt.executeQuery("SELECT PlantName, PlantQuantity FROM Plant WHERE PID = " + (list.getSelectedIndex() + 1));
  126. while (rs.next()) {
  127. text = rs.getString("PlantName");
  128. exp = rs.getString("PlantQuantity");
  129. field1.setText(text);
  130. field2.setText(exp);
  131. }
  132.  
  133. } catch (SQLException ex) {
  134. System.out.println("SQLException: " + ex.getMessage());
  135. System.out.println("SQLState: " + ex.getSQLState());
  136. System.out.println("VendorError: " + ex.getErrorCode());
  137. } finally {
  138. if (rs != null) {
  139. try {
  140. rs.close();
  141. } catch (SQLException sqlEx) {
  142. } // ignore
  143.  
  144. rs = null;
  145. }
  146.  
  147. if (stmt != null) {
  148. try {
  149. stmt.close();
  150. } catch (SQLException sqlEx) {
  151. } // ignore
  152.  
  153. stmt = null;
  154. }
  155. }
  156. }
  157. });
  158.  
  159. btn_find.addActionListener(new ActionListener() {
  160. @Override
  161. public void actionPerformed(ActionEvent e) {
  162. JFrame frame1 = new JFrame("Поиск");
  163. JPanel panel1 = new JPanel();
  164. panel1.setLayout(new GridBagLayout());
  165. frame1.setMinimumSize(new Dimension(300, 200));
  166. frame1.setResizable(false);
  167. JLabel label = new JLabel("Введите количество растений");
  168. JButton btn_ok = new JButton("Искать");
  169. JTextField txtf_category = new JTextField(10);
  170. GridBagConstraints c1 = new GridBagConstraints();
  171. Insets insets1 = new Insets(10,10,10,10);
  172. c1.insets = insets1;
  173. c1.gridx = 0;
  174. c1.gridy = 0;
  175. panel1.add(label, c1);
  176. c1.gridy = 1;
  177. panel1.add(txtf_category, c1);
  178. c1.gridy = 2;
  179. panel1.add(btn_ok, c1);
  180. frame1.add(panel1);
  181. frame1.pack();
  182. frame1.setVisible(true);
  183. btn_ok.addActionListener(new ActionListener() {
  184. @Override
  185. public void actionPerformed(ActionEvent e) {
  186. if (!txtf_category.equals("")){
  187. try {
  188. String text;
  189. String exp;
  190. stmt = connection.createStatement();
  191. rs = null;
  192. rs = stmt.executeQuery("SELECT * FROM Plant WHERE PlantQuantity =" + txtf_category.getText());
  193. if (rs.next()){
  194. found = true;
  195. f1.setText(rs.getString("PID"));
  196. f2.setText(rs.getString("PlantName"));
  197. f3.setText(rs.getString("PlantType"));
  198. f4.setText(rs.getString("PlantPrice"));
  199. f5.setText(rs.getString("PlantQuantity"));
  200. cur_item = Integer.parseInt(f1.getText());
  201. }
  202. else {
  203. txtf_category.setText("Отсутсвует");
  204. }
  205.  
  206. }
  207. catch (SQLException ex){
  208. System.out.println("XYI");
  209. System.out.println("SQLException: " + ex.getMessage());
  210. System.out.println("SQLState: " + ex.getSQLState());
  211. System.out.println("VendorError: " + ex.getErrorCode());
  212. }
  213. finally {
  214. if (rs != null) {
  215. try {
  216. rs.close();
  217. } catch (SQLException sqlEx) { } // ignore
  218.  
  219. rs = null;
  220. }
  221.  
  222. if (stmt != null) {
  223. try {
  224. stmt.close();
  225. } catch (SQLException sqlEx) { } // ignore
  226.  
  227. stmt = null;
  228. }
  229. }
  230. if (found) {
  231. btn_next.setVisible(true);
  232. btn_last.setVisible(true);
  233. f1.setVisible(true);
  234. f2.setVisible(true);
  235. f3.setVisible(true);
  236. f4.setVisible(true);
  237. f5.setVisible(true);
  238. }
  239. }
  240. }
  241. });
  242. }
  243. });
  244.  
  245. btn_next.addActionListener(new ActionListener() {
  246. @Override
  247. public void actionPerformed(ActionEvent e) {
  248. if (cur_item < 3) {
  249. try {
  250. cur_item+= 1;
  251. stmt = connection.createStatement();
  252. rs = stmt.executeQuery("SELECT * FROM Plant WHERE PID = " + cur_item);
  253. if (rs.next()) {
  254. f1.setText(rs.getString("PID"));
  255. f2.setText(rs.getString("PlantName"));
  256. f3.setText(rs.getString("PlantType"));
  257. f4.setText(rs.getString("PlantPrice"));
  258. f5.setText(rs.getString("PlantQuantity"));
  259. }
  260. } catch (SQLException ex) {
  261. System.out.println("SQLException: " + ex.getMessage());
  262. System.out.println("SQLState: " + ex.getSQLState());
  263. System.out.println("VendorError: " + ex.getErrorCode());
  264. } finally {
  265. if (rs != null) {
  266. try {
  267. rs.close();
  268. } catch (SQLException sqlEx) {
  269. } // ignore
  270.  
  271. rs = null;
  272. }
  273.  
  274. if (stmt != null) {
  275. try {
  276. stmt.close();
  277. } catch (SQLException sqlEx) {
  278. } // ignore
  279.  
  280. stmt = null;
  281. }
  282. }
  283. }
  284. }
  285. });
  286.  
  287. btn_last.addActionListener(new ActionListener() {
  288. @Override
  289. public void actionPerformed(ActionEvent e) {
  290. if (cur_item > 1) {
  291. try {
  292. cur_item -= 1;
  293. stmt = connection.createStatement();
  294. rs = stmt.executeQuery("SELECT * FROM Plant WHERE PID = " + cur_item);
  295. if (rs.next()) {
  296. f1.setText(rs.getString("PID"));
  297. f2.setText(rs.getString("PlantName"));
  298. f3.setText(rs.getString("PlantType"));
  299. f4.setText(rs.getString("PlantPrice"));
  300. f5.setText(rs.getString("PlantQuantity"));
  301. }
  302. } catch (SQLException ex) {
  303. System.out.println("SQLException: " + ex.getMessage());
  304. System.out.println("SQLState: " + ex.getSQLState());
  305. System.out.println("VendorError: " + ex.getErrorCode());
  306. } finally {
  307. if (rs != null) {
  308. try {
  309. rs.close();
  310. } catch (SQLException sqlEx) {
  311. } // ignore
  312.  
  313. rs = null;
  314. }
  315.  
  316. if (stmt != null) {
  317. try {
  318. stmt.close();
  319. } catch (SQLException sqlEx) {
  320. } // ignore
  321.  
  322. stmt = null;
  323. }
  324. }
  325. }
  326. }
  327. });
  328.  
  329. frame.setMinimumSize(new Dimension(800, 600));
  330. frame.setResizable(false);
  331. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  332. frame.add(panel);
  333. frame.pack();
  334. frame.setVisible(true);
  335.  
  336. }
  337.  
  338.  
  339. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement