Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package DbUtil;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- public class dbconnection {
- private static final String username = "username";
- private static final String password = "password";
- private static final String OONN = "jdbc:mysql://localhost/login";
- private static final String SCONN = "jdbc:sqlite:stany.sqlite";
- public static Connection getConnection()throws SQLException{
- try{
- System.out.println("Tutaj wchodze dbconneciton");
- Class.forName("org.sqlite.JDBC");
- return DriverManager.getConnection(SCONN);
- }catch (ClassNotFoundException ex){
- System.out.println("Tutaj wchodze dbconneciton catch");
- ex.printStackTrace();
- }
- return null;
- }
- }
- package loginapp;
- import javafx.application.Application;
- import javafx.fxml.FXMLLoader;
- import javafx.scene.Parent;
- import javafx.scene.Scene;
- import javafx.stage.Stage;
- public class LoginApp extends Application {
- public void start(Stage stage) throws Exception{
- Parent root = (Parent) FXMLLoader.load(getClass().getResource("login.fxml"));
- Scene scene = new Scene(root);
- stage.setScene(scene);
- stage.setTitle("EmotionalGraph");
- stage.show();
- }
- public static void main(String []args ){
- launch(args);
- }
- }
- package loginapp;
- import User.UserContoller;
- import javafx.event.ActionEvent;
- import javafx.fxml.FXML;
- import javafx.fxml.FXMLLoader;
- import javafx.fxml.Initializable;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.control.Label;
- import javafx.scene.control.PasswordField;
- import javafx.scene.control.TextField;
- import javafx.scene.layout.Pane;
- import javafx.stage.Stage;
- import java.io.IOException;
- import java.net.URL;
- import java.util.ResourceBundle;
- public class LoginController implements Initializable {
- LoginModel loginModel = new LoginModel();
- @FXML
- private Label dbstatus;
- @FXML
- private TextField username;
- @FXML
- private PasswordField password;
- @FXML
- private Button loginButton;
- @FXML
- private Label loginStatus;
- public void initialize(URL url, ResourceBundle rb){
- if(this.loginModel.isDataBaseConnected()){
- this.dbstatus.setText("Connected");
- }else{
- this.dbstatus.setText("Not Connected");
- }
- }
- @FXML
- public void login(ActionEvent event){
- try{
- if(this.loginModel.isLogin(this.username.getText(),this.password.getText())) {
- Stage stage = (Stage) this.loginButton.getScene().getWindow();
- userLogin();
- stage.close();
- }else {
- loginStatus.setText("Wrong Username or Password");
- }
- }catch(Exception localException){
- }
- }
- public void userLogin(){
- try {
- Stage userStage = new Stage();
- FXMLLoader loader = new FXMLLoader();
- Pane root = (Pane)loader.load(getClass().getResource("/User/UserFXML.fxml").openStream());
- UserContoller userContoller = (UserContoller)loader.getController();
- Scene scene = new Scene(root);
- userStage.setScene(scene);
- userStage.setTitle("Emotions");
- userStage.setResizable(false);
- userStage.show();
- }catch (IOException ex){
- ex.printStackTrace();
- }
- }
- }
- package loginapp;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- import java.sql.ResultSet;
- import DbUtil.dbconnection;
- public class LoginModel {
- Connection connection;
- public LoginModel(){
- try{
- this.connection = dbconnection.getConnection();
- }catch(SQLException ex){
- ex.printStackTrace();
- }
- if(this.connection==null){
- System.exit(1);
- }
- }
- public boolean isDataBaseConnected(){
- return this.connection != null;
- }
- public boolean isLogin(String username,String password)throws Exception{
- PreparedStatement pr = null;
- ResultSet rs = null;
- String sql= "SELECT * FROM login where username = ? and password = ?";
- try {
- pr = this.connection.prepareStatement(sql);
- pr.setString(1, username);
- pr.setString(2, password);
- rs = pr.executeQuery();
- boolean boll1;
- if (rs.next()) {
- return true;
- }
- return false;
- }catch(SQLException ex){
- System.out.println(ex);
- return false;
- }
- finally {
- pr.close();
- rs.close();
- }
- }
- }
- package User;
- import javafx.beans.property.SimpleStringProperty;
- import javafx.beans.property.StringProperty;
- public class EmoData {
- private final StringProperty stan;
- private final StringProperty czas;
- public EmoData(String Stan, String Czas) {
- this.stan = new SimpleStringProperty(Stan);
- this.czas = new SimpleStringProperty(Czas);
- }
- public String getCzas() {
- return czas.get();
- }
- public StringProperty czasProperty() {
- return this.czas;
- }
- public void setCzas(String czas) {
- this.czas.set(czas);
- }
- public String getStan() {
- return stan.get();
- }
- public StringProperty stanProperty() {
- return this.stan;
- }
- public void setStan(String stan) {
- this.stan.set(stan);
- }
- }
- package User;
- import DbUtil.dbconnection;
- import javafx.collections.FXCollections;
- import javafx.collections.ObservableList;
- import javafx.fxml.FXML;
- import javafx.fxml.Initializable;
- import javafx.scene.control.Button;
- import javafx.scene.control.TableColumn;
- import javafx.scene.control.TableView;
- import java.awt.event.ActionEvent;
- import java.net.URL;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ResourceBundle;
- public abstract class UserContoller implements Initializable {
- @FXML
- private Button badbut;
- @FXML
- private Button natbut;
- @FXML
- private Button goodbut;
- @FXML
- private TableView<EmoData> emotable;
- @FXML
- private TableColumn<EmoData, String> stancolumn;
- @FXML
- private TableColumn<EmoData, String> czascolumn;
- private dbconnection dc;
- private ObservableList<EmoData> data;
- private String sql = "SELECT * FROM stany";
- public void initialize(URL url, ResourceBundle rb) {
- this.dc = new dbconnection();
- }
- @FXML
- private void loadstanydata(ActionEvent event) throws SQLException {
- try {
- Connection conn = dbconnection.getConnection();
- this.data = FXCollections.observableArrayList();
- ResultSet rs = conn.createStatement().executeQuery(sql);
- while (rs.next()) {
- this.data.add(new EmoData(rs.getString(1), rs.getString(2)));
- }
- } catch (SQLException e) {
- System.out.println("Error:" + e);
- }
- }
- @FXML
- public void addGood(ActionEvent event){
- String sqlInsertGood = "INSERT INTO STANY VALUES ('DOBRZE', NOW())";
- try{
- Connection conn = dbconnection.getConnection();
- PreparedStatement stat1 = conn.prepareStatement(sqlInsertGood);
- stat1.execute();
- conn.close();
- }catch (SQLException e){
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement