Advertisement
Guest User

Untitled

a guest
Nov 21st, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.48 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package Pilka;
  7. import java.awt.Color;
  8. import java.awt.Dimension;
  9. import java.awt.Point;
  10. import java.sql.Connection;
  11. import java.sql.DriverManager;
  12. import java.sql.ResultSet;
  13. import java.sql.SQLException;
  14. import java.sql.Statement;
  15. /**
  16.  *
  17.  * @author Kamil
  18.  */
  19. public class Baza {
  20.    
  21.     private static final String _DRIVER =("com.mysql.baza.Driver");
  22. private static final String _DB_URL = "baza:mysql://localhost:3306/generatoryaplikacji";
  23. private static final String _USER = "root";
  24. private static final String _PASS = "";
  25.  
  26. private static Connection conn = null;
  27. private static Statement state = null;
  28.  
  29. private Baza()
  30. {
  31. }
  32.    static
  33. {
  34. try
  35. {
  36. // Register Driver
  37. Class.forName(_DRIVER);
  38.  
  39. // Open Connection
  40. conn = DriverManager.getConnection(_DB_URL, _USER, _PASS);
  41. state = conn.createStatement();
  42. }
  43. catch(Exception e)
  44. {
  45. e.printStackTrace();
  46. }
  47. }  
  48.  public static void zapisDoBazy(Frame f)
  49. {
  50. String sqlCommand = "";
  51. for(Pilka pilka : f.panelPilka().Pilki)  
  52. {
  53. sqlCommand = "INSERT INTO bazapilki VALUES(" +
  54. pilka.index + ", " +
  55. pilka.kolor.getRed() + ", " +
  56. pilka.kolor.getGreen() + ", " +
  57. pilka.kolor.getBlue() + ", " +
  58. pilka.lokalizacja.x + ", " +
  59. pilka.lokalizacja.y + ", " +
  60. pilka.rozmiar.width + ", " +
  61. pilka.rozmiar.height + ", " +
  62. pilka.predkosc.x + ", " +
  63. pilka.predkosc.y +
  64. ");";
  65. try
  66. {
  67. state.execute(sqlCommand);
  68. }
  69. catch(SQLException e)
  70. {
  71. e.printStackTrace();
  72. return;
  73. }
  74. }
  75. }  
  76.   public static void wczytajZBazy(Frame f)
  77. {
  78. ResultSet rs = null;
  79. try
  80. {
  81. rs = state.executeQuery("SELECT * FROM bazapilki;");
  82. while(rs.next())
  83. {
  84. int index = rs.getInt(1);
  85. Color kolor = new Color(rs.getInt(2), rs.getInt(3), rs.getInt(4));
  86. Point lokalizacja = new Point(rs.getInt(5), rs.getInt(6));
  87. Dimension dim = new Dimension(rs.getInt(7), rs.getInt(8));
  88. Point predkosc = new Point(rs.getInt(9), rs.getInt(10));
  89. Pilka wczytajPilke = Pilka.createBall(index, kolor, lokalizacja, dim, predkosc);
  90. f.panelPilka().Pilki.add(wczytajPilke);
  91. f.mf.labelCountValue.setText(String.valueOf(f.panelPilka.Pilki.rozmiar()));
  92. }
  93. }
  94. catch(SQLException e)
  95. {
  96. e.printStackTrace();
  97. }
  98. }
  99.  
  100. public static void wyczyscBaze(Frame frameTable)
  101. {
  102. String sqlCommand = "DELETE FROM bazapilki;";
  103. try
  104. {
  105. state.execute(sqlCommand);
  106. }
  107. catch(SQLException e)
  108. {
  109. e.printStackTrace();
  110. }
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement