Advertisement
Guest User

Untitled

a guest
Apr 24th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.48 KB | None | 0 0
  1. package thales;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.EventQueue;
  5.  
  6. import javax.swing.JFrame;
  7. import javax.swing.JPanel;
  8. import javax.swing.border.EmptyBorder;
  9. import javax.swing.JTable;
  10. import javax.swing.JButton;
  11. import java.awt.event.ActionListener;
  12. import java.awt.event.ActionEvent;
  13. import javax.swing.table.DefaultTableModel;
  14. import javax.swing.JScrollPane;
  15. import java.sql.*;
  16. import java.util.ArrayList;
  17. import javax.swing.JTextField;
  18. import javax.swing.SwingConstants;
  19.  
  20. public class game extends JFrame {
  21.  
  22. private JPanel contentPane;
  23. private JTable table;
  24. private JTextField textField;
  25.  
  26. /**
  27. * Launch the application.
  28. */
  29. public static void main(String[] args) {
  30. EventQueue.invokeLater(new Runnable() {
  31. public void run() {
  32. try {
  33. game frame = new game();
  34. frame.setVisible(true);
  35. } catch (Exception e) {
  36. e.printStackTrace();
  37. }
  38. }
  39. });
  40. }
  41.  
  42. /**
  43. * Create the frame.
  44. */
  45. public game() {
  46. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  47. setBounds(100, 100, 414, 286);
  48. contentPane = new JPanel();
  49. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  50. setContentPane(contentPane);
  51. contentPane.setLayout(null);
  52.  
  53. JScrollPane scrollPane = new JScrollPane();
  54. scrollPane.setBounds(10, 28, 378, 154);
  55. contentPane.add(scrollPane);
  56.  
  57. table = new JTable();
  58. scrollPane.setViewportView(table);
  59. table.setModel(new DefaultTableModel(
  60. new Object[][] {
  61. {"0", "0", "0", "0", new Integer(0)},
  62. },
  63. new String[] {
  64. "id", "\u0422\u043E\u0432\u0430\u0440", "\u0426\u0435\u043D\u0430", "\u041E\u0441\u0442\u0430\u0442\u043E\u043A", "\u041E\u043F\u0435\u0440\u0430\u0446\u0438\u044F"
  65. }
  66. ) {
  67. Class[] columnTypes = new Class[] {
  68. Object.class, Object.class, Object.class, Object.class, Integer.class
  69. };
  70. public Class getColumnClass(int columnIndex) {
  71. return columnTypes[columnIndex];
  72. }
  73. });
  74. table.getColumnModel().getColumn(1).setPreferredWidth(268);
  75.  
  76. JButton btnNewButton = new JButton("test");
  77. btnNewButton.addActionListener(new ActionListener() {
  78. public void actionPerformed(ActionEvent arg0) {
  79. Connection connect;
  80. try {
  81. String driverName="com.mysql.jdbc.Driver";
  82. Class.forName(driverName);
  83. String serverName="localhost";
  84. String mybase="java2";
  85. String url_="jdbc:mysql://"+serverName+"/"+mybase;
  86. String username="root";
  87. String password="";
  88. connect=DriverManager.getConnection(url_,username,password);
  89.  
  90. String query="";
  91. Statement stmt=connect.createStatement();
  92.  
  93. DefaultTableModel model = (DefaultTableModel) table.getModel();
  94.  
  95. //выполнение операций
  96. ArrayList<Integer> list_id2=new ArrayList<Integer>();
  97. ArrayList<Integer> list_oper=new ArrayList<Integer>();
  98. ArrayList<Integer> list_price2=new ArrayList<Integer>();
  99.  
  100. for (int i = 0; i < model.getRowCount(); i++) {
  101. int oper=Integer.parseInt(model.getValueAt(i, 4).toString());
  102. int id2=Integer.parseInt(model.getValueAt(i, 0).toString());
  103. int price2=Integer.parseInt(model.getValueAt(i, 2).toString());
  104. if (oper!=0) {
  105. list_id2.add(id2);
  106. list_oper.add(oper);
  107. list_price2.add(price2);
  108. }
  109. }
  110.  
  111. for (int i = 0; i < list_id2.size(); i++) {
  112. query="INSERT operations (idgoods,val,sum)"
  113. + " values ("+list_id2.get(i)+","+
  114. list_oper.get(i)+","+
  115. (-1*list_price2.get(i)*list_oper.get(i))+")";
  116. stmt=connect.createStatement();
  117. stmt.executeUpdate(query);
  118. }
  119.  
  120.  
  121. //замена цен
  122. query="SELECT * FROM goods";
  123. stmt=connect.createStatement();
  124. ResultSet rs=stmt.executeQuery(query);
  125.  
  126. ArrayList<Integer> list_id=new ArrayList<Integer>();
  127. ArrayList<Integer> list_price=new ArrayList<Integer>();
  128.  
  129. while (rs.next()) {
  130. int p=rs.getInt("price");
  131. double d=Math.random()-0.5;
  132. p=(int)(p*(1+d));
  133. if (p<10) p=10;
  134. list_id.add(rs.getInt("id"));
  135. list_price.add(p);
  136. }
  137.  
  138. for (int i = 0; i < list_id.size(); i++) {
  139. query="UPDATE goods SET price="+list_price.get(i)+
  140. " WHERE id="+list_id.get(i);
  141. stmt=connect.createStatement();
  142. stmt.executeUpdate(query);
  143. }
  144.  
  145. //новое состояние
  146. query="SELECT id,name,price,SUM(ifnull(val,0)) as val FROM goods"
  147. + " LEFT JOIN operations ON id=idgoods"
  148. + " GROUP BY id,name,price";
  149. stmt=connect.createStatement();
  150. rs=stmt.executeQuery(query);
  151.  
  152. while (model.getRowCount()>0) {
  153. model.removeRow(0);
  154. }
  155.  
  156. while (rs.next()) {
  157. Object[] rowData=new Object[5];
  158. rowData[0]=rs.getString("id");
  159. rowData[1]=rs.getString("name");
  160. rowData[2]=rs.getString("price");
  161. rowData[3]=rs.getString("val");
  162. rowData[4]=0;
  163. model.addRow(rowData);
  164. }
  165.  
  166. query="SELECT SUM(ifnull(sum,0))+10000 as val FROM operations";
  167. stmt=connect.createStatement();
  168. rs=stmt.executeQuery(query);
  169. if (rs.next()) {
  170. textField.setText(rs.getString("val"));
  171. }
  172. connect.close();
  173. }
  174. catch(Exception ex) {}
  175.  
  176. }
  177. });
  178. btnNewButton.setBounds(10, 207, 152, 30);
  179. contentPane.add(btnNewButton);
  180.  
  181. textField = new JTextField();
  182. textField.setHorizontalAlignment(SwingConstants.RIGHT);
  183. textField.setBounds(10, 0, 86, 20);
  184. contentPane.add(textField);
  185. textField.setColumns(10);
  186. }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement