Advertisement
Guest User

Untitled

a guest
Apr 24th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 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.  
  18. public class game extends JFrame {
  19.  
  20. private JPanel contentPane;
  21. private JTable table;
  22.  
  23. /**
  24. * Launch the application.
  25. */
  26. public static void main(String[] args) {
  27. EventQueue.invokeLater(new Runnable() {
  28. public void run() {
  29. try {
  30. game frame = new game();
  31. frame.setVisible(true);
  32. } catch (Exception e) {
  33. e.printStackTrace();
  34. }
  35. }
  36. });
  37. }
  38.  
  39. /**
  40. * Create the frame.
  41. */
  42. public game() {
  43. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  44. setBounds(100, 100, 364, 265);
  45. contentPane = new JPanel();
  46. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  47. setContentPane(contentPane);
  48. contentPane.setLayout(null);
  49.  
  50. JScrollPane scrollPane = new JScrollPane();
  51. scrollPane.setBounds(10, 11, 325, 154);
  52. contentPane.add(scrollPane);
  53.  
  54. table = new JTable();
  55. scrollPane.setViewportView(table);
  56. table.setModel(new DefaultTableModel(
  57. new Object[][] {
  58. {null, null},
  59. },
  60. new String[] {
  61. "\u0422\u043E\u0432\u0430\u0440", "\u0426\u0435\u043D\u0430"
  62. }
  63. ));
  64. table.getColumnModel().getColumn(0).setPreferredWidth(268);
  65.  
  66. JButton btnNewButton = new JButton("test");
  67. btnNewButton.addActionListener(new ActionListener() {
  68. public void actionPerformed(ActionEvent arg0) {
  69. Connection connect;
  70. try {
  71. String driverName="com.mysql.jdbc.Driver";
  72. Class.forName(driverName);
  73. String serverName="localhost";
  74. String mybase="java2";
  75. String url_="jdbc:mysql://"+serverName+"/"+mybase;
  76. String username="root";
  77. String password="";
  78. connect=DriverManager.getConnection(url_,username,password);
  79.  
  80. String query="SELECT * FROM goods";
  81. Statement stmt=connect.createStatement();
  82. ResultSet rs=stmt.executeQuery(query);
  83.  
  84. ArrayList<Integer> list_id=new ArrayList<Integer>();
  85. ArrayList<Integer> list_price=new ArrayList<Integer>();
  86.  
  87. while (rs.next()) {
  88. int p=rs.getInt("price");
  89. double d=Math.random()-0.5;
  90. p=(int)(p*(1+d));
  91. if (p<1) p=1;
  92. list_id.add(rs.getInt("id"));
  93. list_price.add(p);
  94. }
  95.  
  96. for (int i = 0; i < list_id.size(); i++) {
  97. query="UPDATE goods SET price="+list_price.get(i)+
  98. " WHERE id="+list_id.get(i);
  99. stmt=connect.createStatement();
  100. stmt.executeUpdate(query);
  101. }
  102.  
  103.  
  104. query="SELECT * FROM goods";
  105. stmt=connect.createStatement();
  106. rs=stmt.executeQuery(query);
  107.  
  108. DefaultTableModel model = (DefaultTableModel) table.getModel();
  109. while (model.getRowCount()>0) {
  110. model.removeRow(0);
  111. }
  112. while (rs.next()) {
  113. Object[] rowData=new Object[2];
  114. rowData[0]=rs.getString("name");
  115. rowData[1]=rs.getString("price");
  116. model.addRow(rowData);
  117. }
  118. connect.close();
  119. }
  120. catch(Exception ex) {}
  121.  
  122. }
  123. });
  124. btnNewButton.setBounds(10, 186, 152, 30);
  125. contentPane.add(btnNewButton);
  126. }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement