Advertisement
DeveloperSergio

Part2

Nov 17th, 2016
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.32 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.sql.*;
  6.  
  7. /**
  8. * Created by Admin on 13.11.2016.
  9. */
  10. public class Window extends JFrame implements ActionListener {
  11.  
  12. String Code3 = "", Type3 = "", Quantity3 = "", Price3 = "", Year3 = "";
  13.  
  14. JPanel panel = new JPanel(new FlowLayout());
  15. JPanel panelForButtons = new JPanel(new FlowLayout()); //JPanel для кнопок выводит элементы в рядок
  16. JPanel panelStart = new JPanel();
  17. JPanel panelLabelTablo = new JPanel();
  18.  
  19. JLabel labelCode; // для вывода инфи из БД
  20. JLabel labelType; // для вывода инфи из БД
  21. JLabel labelQuantity;
  22. JLabel labelPrice;
  23. JLabel labelYear;
  24. JLabel labelToDo; // выводит все дейстия кнопок
  25.  
  26. JTextField field = new JTextField(16); // поле для ввода текаста
  27.  
  28. Container containerPane = getContentPane(); // контейнер в который будем добавлять кнопки, label-ы и тп
  29.  
  30. JButton start = new JButton("Start");
  31. JButton button1 = new JButton("Кнопка1");
  32. JButton button2 = new JButton("Кнопка2");
  33. JButton button3 = new JButton("Кнопка3");
  34. JButton button4 = new JButton("Кнопка4");
  35. JButton button5 = new JButton("Кнопка5");
  36. JButton button6 = new JButton("Очистить");
  37. JButton buttonUpCode = new JButton("Добавить в Code");//кнопка для обнавления поля Code таблици tbl1
  38.  
  39. Object[][] data = new Object[1][5]; // данные с БД
  40.  
  41. private static String tbl1Title = ""; //заголовок таблицы
  42.  
  43. //Обьект таблицы
  44. JTable jTablePeople;
  45.  
  46. // JDBC URL, username and password MYSQL server
  47. private static final String url = "jdbc:mysql://localhost:3306/carsfff";
  48. private static final String user = "root";
  49. private static final String password = "root";
  50.  
  51. // JDBC varibals for opening and managing connection
  52. private static Connection con;
  53. private static Statement stmt;
  54. private static ResultSet rs;
  55.  
  56.  
  57. public Window(){
  58. super("Запуск программы");
  59. setSize(400,100);
  60. setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  61. add(panelStart);
  62. panelStart.add( start );
  63. containerPane.add("Center", panelStart);
  64. setLocationRelativeTo(null);
  65. start.addActionListener(this);
  66. setVisible(true);
  67. }
  68.  
  69.  
  70.  
  71. public Window(String FieldCode, String FieldType, String FieldQuantity, String FieldPrice, String FieldYear,String Code, String Type, int Quantity, int Price, int Year){
  72. super( "Таблица " + "\""+Window.tbl1Title+"\"" + " Базы MYSQL from v0.01 to v1.36 " );
  73. setSize(700, 500); // шырина x высота
  74. setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  75. add(panel); //добавляем JPanel на JFrame
  76.  
  77.  
  78. this.labelCode = new JLabel("*******"); // поле для вывода инфы столбца CODE с таблицы
  79. this.labelType = new JLabel("*******");
  80. this.labelQuantity = new JLabel("*******");
  81. this.labelPrice = new JLabel("*******");
  82. this.labelYear = new JLabel("*******");
  83. this.labelToDo = new JLabel("-------");
  84.  
  85. panel.add(this.labelCode); //добавляем label на JPanel
  86. panel.add(this.labelType);
  87. panel.add(this.labelQuantity);
  88. panel.add(this.labelPrice);
  89. panel.add(this.labelYear);
  90.  
  91. panelLabelTablo.add(this.labelToDo); //добавляем label табло для вывода действый всех кнопок
  92.  
  93.  
  94. Code3 = Code; // для JLabel с БД
  95. Type3 = Type;
  96. Quantity3 = String.valueOf(Quantity);
  97. Price3 = String.valueOf(Price);
  98. Year3 = String.valueOf(Year);
  99.  
  100. panelForButtons.add( button1 ); // добавляем кнопки на JPanel
  101. panelForButtons.add( button2 );
  102. panelForButtons.add( button3 );
  103. panelForButtons.add( button4 );
  104. panelForButtons.add( button5 );
  105. panelForButtons.add( button6 );
  106.  
  107. panelForButtons.add(field); // добавил поле ввода на JPanel
  108. panelForButtons.add( buttonUpCode );
  109.  
  110. containerPane.add("North", panel); // добавляем в контейнер panel а с ней вместе Label а так же делаем выравнивание
  111. containerPane.add("Center", panelForButtons); //добавляем в контейнер групу кнопок(6) и выравниваем их по центру
  112. containerPane.add("South", panelLabelTablo);
  113. setLocationRelativeTo(null); // запуск окна JFrame по центру экрана
  114.  
  115.  
  116. //it is a Test
  117. // Заголовки таблици
  118. Object[] headers = {FieldCode, FieldType, FieldQuantity, FieldPrice, FieldYear};
  119.  
  120. // Массив для таблици
  121. data = new Object[][]{
  122. {Code, Type, Quantity, Price, Year}
  123. };
  124.  
  125.  
  126. jTablePeople = new JTable(data, headers);
  127. // добавляем панель прокрутки и включаем ее в состав таблици
  128. JScrollPane jScrollPane = new JScrollPane(jTablePeople);
  129. // Утанавливаем размери прокручиваемой области
  130. jTablePeople.setPreferredScrollableViewportSize(new Dimension(450, 550));
  131. // добавляем таблицу в контейнер
  132. containerPane.add("East", jScrollPane);
  133.  
  134. setVisible(true);
  135. /* Сушатели кнопок */
  136. button1.addActionListener( this );
  137. button2.addActionListener( this );
  138. button3.addActionListener( this );
  139. button4.addActionListener( this );
  140. button5.addActionListener( this );
  141. button6.addActionListener( this );
  142. buttonUpCode.addActionListener( this );
  143.  
  144. }
  145.  
  146. public void actionPerformed(ActionEvent event){ // обработчики событий
  147.  
  148. if (event.getSource() == button1){
  149. this.labelCode.setText("");
  150. this.labelCode.setText(Code3);
  151. // button1.setEnabled(false);
  152. this.labelToDo.setText(event.getActionCommand()); //выводим команду в поле
  153.  
  154. }
  155. else if (event.getSource() == button2){
  156. this.labelType.setText("");
  157. this.labelType.setText(Type3);
  158. this.labelToDo.setText(event.getActionCommand());
  159. }
  160. else if (event.getSource() == button3){
  161. this.labelQuantity.setText("");
  162. this.labelQuantity.setText(Quantity3);
  163. this.labelToDo.setText(event.getActionCommand());
  164. }
  165. else if (event.getSource() == button4){
  166. this.labelPrice.setText("");
  167. this.labelPrice.setText(Price3);
  168. this.labelToDo.setText(event.getActionCommand());
  169. }
  170. else if (event.getSource() == button5){
  171. this.labelYear.setText("");
  172. this.labelYear.setText(Year3);
  173. this.labelToDo.setText(event.getActionCommand());
  174. }
  175. else if (event.getSource() == button6){
  176. this.labelCode.setText("_");
  177. this.labelType.setText("_");
  178. this.labelQuantity.setText("_");
  179. this.labelPrice.setText("_");
  180. this.labelYear.setText("_");
  181. this.labelToDo.setText(event.getActionCommand());
  182. }
  183. else if (event.getSource() == start){
  184. ViewData();
  185. }
  186. else if (event.getSource() == buttonUpCode){
  187. labelToDo.setText(UpdateData());
  188. }
  189.  
  190.  
  191. }
  192.  
  193. // метод лоя просмотр данных с таблици tbl1
  194. Object ViewData(){
  195. String Code = "", Type = "", FieldCode = "", FieldType = "",
  196. FieldQuantity = "", FieldPrice = "", FieldYear = "";
  197. int Quantity = 0, Price = 0, Year = 0;
  198.  
  199.  
  200. String query = "select * from tbl1";
  201. String queryCode = "show fields from tbl1 where Field='CODE'";
  202. String queryType = "show fields from tbl1 where Field='TYPE'";
  203. String queryQuantity = "show fields from tbl1 where Field='QUANTITY'";
  204. String queryPrice = "show fields from tbl1 where Field='PRICE'";
  205. String queryYear = "show fields from tbl1 where Field='YEAR'";
  206. String queryTitleTable = "show tables";
  207.  
  208. try {
  209. con = DriverManager.getConnection(url, user, password);
  210.  
  211. stmt = con.createStatement();
  212.  
  213. rs = stmt.executeQuery(query);
  214.  
  215.  
  216. while (rs.next()){
  217. Code = rs.getString(1);
  218. Type = rs.getString(2);
  219. Quantity = rs.getInt(3);
  220. Price = rs.getInt(4);
  221. Year = rs.getInt(5);
  222.  
  223. }
  224.  
  225. rs = stmt.executeQuery(queryCode);
  226.  
  227. while (rs.next()){
  228. FieldCode = rs.getString(1);
  229. }
  230.  
  231. rs = stmt.executeQuery(queryType);
  232. while (rs.next()){
  233. FieldType = rs.getString(1);
  234. }
  235.  
  236. rs = stmt.executeQuery(queryQuantity);
  237. while (rs.next()){
  238. FieldQuantity = rs.getString(1);
  239. }
  240. rs = stmt.executeQuery(queryPrice);
  241. while (rs.next()){
  242. FieldPrice = rs.getString(1);
  243. }
  244. rs = stmt.executeQuery(queryYear);
  245. while (rs.next()){
  246. FieldYear = rs.getString(1);
  247. }
  248.  
  249. rs = stmt.executeQuery(queryTitleTable);
  250. while (rs.next()){
  251. Window.tbl1Title = rs.getString(1);
  252. }
  253.  
  254. }catch (SQLException sqlEx){
  255. sqlEx.printStackTrace();
  256. }finally {
  257. try{con.close();} catch (SQLException se){}
  258. try{stmt.close();} catch (SQLException se){}
  259. try{rs.close();} catch (SQLException se){}
  260. }
  261.  
  262. Window obWindow = new Window(FieldCode,FieldType, FieldQuantity, FieldPrice, FieldYear ,Code, Type, Quantity, Price, Year);
  263.  
  264. return obWindow;
  265. }
  266.  
  267. // обновление значений в таблице tbl1
  268. String UpdateData(){
  269.  
  270. String queryUpdate = "update tbl1 set CODE=".concat("'" + field.getText() + "'");// запрос для получения текста
  271. // с JTextField
  272. try {
  273. con = DriverManager.getConnection(url, user, password);
  274.  
  275. stmt = con.createStatement();
  276.  
  277. stmt.executeUpdate(queryUpdate); // обновляем в поле CODE
  278.  
  279.  
  280. }catch (SQLException sqlEx){
  281. sqlEx.printStackTrace();
  282. }finally {
  283. try{con.close();} catch (SQLException se){}
  284. try{stmt.close();} catch (SQLException se){}
  285. try{rs.close();} catch (SQLException se){}
  286. }
  287. return "Запись добавлена в поле \" CODE \"";
  288. }
  289.  
  290. public static void main(String[] args){
  291. Window obLittle = new Window();
  292.  
  293. }
  294.  
  295.  
  296. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement