Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package Pilka;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Point;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- /**
- *
- * @author Kamil
- */
- public class Baza {
- private static final String _DRIVER =("com.mysql.baza.Driver");
- private static final String _DB_URL = "baza:mysql://localhost:3306/generatoryaplikacji";
- private static final String _USER = "root";
- private static final String _PASS = "";
- private static Connection conn = null;
- private static Statement state = null;
- private Baza()
- {
- }
- static
- {
- try
- {
- // Register Driver
- Class.forName(_DRIVER);
- // Open Connection
- conn = DriverManager.getConnection(_DB_URL, _USER, _PASS);
- state = conn.createStatement();
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- }
- public static void zapisDoBazy(Frame f)
- {
- String sqlCommand = "";
- for(Pilka pilka : f.panelPilka().Pilki)
- {
- sqlCommand = "INSERT INTO bazapilki VALUES(" +
- pilka.index + ", " +
- pilka.kolor.getRed() + ", " +
- pilka.kolor.getGreen() + ", " +
- pilka.kolor.getBlue() + ", " +
- pilka.lokalizacja.x + ", " +
- pilka.lokalizacja.y + ", " +
- pilka.rozmiar.width + ", " +
- pilka.rozmiar.height + ", " +
- pilka.predkosc.x + ", " +
- pilka.predkosc.y +
- ");";
- try
- {
- state.execute(sqlCommand);
- }
- catch(SQLException e)
- {
- e.printStackTrace();
- return;
- }
- }
- }
- public static void wczytajZBazy(Frame f)
- {
- ResultSet rs = null;
- try
- {
- rs = state.executeQuery("SELECT * FROM bazapilki;");
- while(rs.next())
- {
- int index = rs.getInt(1);
- Color kolor = new Color(rs.getInt(2), rs.getInt(3), rs.getInt(4));
- Point lokalizacja = new Point(rs.getInt(5), rs.getInt(6));
- Dimension dim = new Dimension(rs.getInt(7), rs.getInt(8));
- Point predkosc = new Point(rs.getInt(9), rs.getInt(10));
- Pilka wczytajPilke = Pilka.createBall(index, kolor, lokalizacja, dim, predkosc);
- f.panelPilka().Pilki.add(wczytajPilke);
- f.mf.labelCountValue.setText(String.valueOf(f.panelPilka.Pilki.rozmiar()));
- }
- }
- catch(SQLException e)
- {
- e.printStackTrace();
- }
- }
- public static void wyczyscBaze(Frame frameTable)
- {
- String sqlCommand = "DELETE FROM bazapilki;";
- try
- {
- state.execute(sqlCommand);
- }
- catch(SQLException e)
- {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement