Advertisement
Guest User

Untitled

a guest
Jun 7th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.94 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 PlantType = " + 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("SQLException: " + ex.getMessage());
  209. System.out.println("SQLState: " + ex.getSQLState());
  210. System.out.println("VendorError: " + ex.getErrorCode());
  211. }
  212. finally {
  213. if (rs != null) {
  214. try {
  215. rs.close();
  216. } catch (SQLException sqlEx) { } // ignore
  217.  
  218. rs = null;
  219. }
  220.  
  221. if (stmt != null) {
  222. try {
  223. stmt.close();
  224. } catch (SQLException sqlEx) { } // ignore
  225.  
  226. stmt = null;
  227. }
  228. }
  229. if (found) {
  230. btn_next.setVisible(true);
  231. btn_last.setVisible(true);
  232. f1.setVisible(true);
  233. f2.setVisible(true);
  234. f3.setVisible(true);
  235. f4.setVisible(true);
  236. f5.setVisible(true);
  237. }
  238. }
  239. }
  240. });
  241. }
  242. });
  243.  
  244. btn_next.addActionListener(new ActionListener() {
  245. @Override
  246. public void actionPerformed(ActionEvent e) {
  247. if (cur_item < 3) {
  248. try {
  249. cur_item+= 1;
  250. stmt = connection.createStatement();
  251. rs = stmt.executeQuery("SELECT * FROM Plant WHERE PID = " + cur_item);
  252. if (rs.next()) {
  253. f1.setText(rs.getString("PID"));
  254. f2.setText(rs.getString("PlantName"));
  255. f3.setText(rs.getString("PlantType"));
  256. f4.setText(rs.getString("PlantPrice"));
  257. f5.setText(rs.getString("PlantQuantity"));
  258. }
  259. } catch (SQLException ex) {
  260. System.out.println("SQLException: " + ex.getMessage());
  261. System.out.println("SQLState: " + ex.getSQLState());
  262. System.out.println("VendorError: " + ex.getErrorCode());
  263. } finally {
  264. if (rs != null) {
  265. try {
  266. rs.close();
  267. } catch (SQLException sqlEx) {
  268. } // ignore
  269.  
  270. rs = null;
  271. }
  272.  
  273. if (stmt != null) {
  274. try {
  275. stmt.close();
  276. } catch (SQLException sqlEx) {
  277. } // ignore
  278.  
  279. stmt = null;
  280. }
  281. }
  282. }
  283. }
  284. });
  285.  
  286. btn_last.addActionListener(new ActionListener() {
  287. @Override
  288. public void actionPerformed(ActionEvent e) {
  289. if (cur_item > 1) {
  290. try {
  291. cur_item -= 1;
  292. stmt = connection.createStatement();
  293. rs = stmt.executeQuery("SELECT * FROM Plant WHERE PID = " + cur_item);
  294. if (rs.next()) {
  295. f1.setText(rs.getString("PID"));
  296. f2.setText(rs.getString("PlantName"));
  297. f3.setText(rs.getString("PlantType"));
  298. f4.setText(rs.getString("PlantPrice"));
  299. f5.setText(rs.getString("PlantQuantity"));
  300. }
  301. } catch (SQLException ex) {
  302. System.out.println("SQLException: " + ex.getMessage());
  303. System.out.println("SQLState: " + ex.getSQLState());
  304. System.out.println("VendorError: " + ex.getErrorCode());
  305. } finally {
  306. if (rs != null) {
  307. try {
  308. rs.close();
  309. } catch (SQLException sqlEx) {
  310. } // ignore
  311.  
  312. rs = null;
  313. }
  314.  
  315. if (stmt != null) {
  316. try {
  317. stmt.close();
  318. } catch (SQLException sqlEx) {
  319. } // ignore
  320.  
  321. stmt = null;
  322. }
  323. }
  324. }
  325. }
  326. });
  327.  
  328. frame.setMinimumSize(new Dimension(800, 600));
  329. frame.setResizable(false);
  330. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  331. frame.add(panel);
  332. frame.pack();
  333. frame.setVisible(true);
  334.  
  335. }
  336.  
  337. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement