Advertisement
Guest User

Untitled

a guest
Feb 8th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.89 KB | None | 0 0
  1. package DbUtil;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.SQLException;
  5.  
  6. public class dbconnection {
  7. private static final String username = "username";
  8. private static final String password = "password";
  9. private static final String OONN = "jdbc:mysql://localhost/login";
  10. private static final String SCONN = "jdbc:sqlite:stany.sqlite";
  11.  
  12. public static Connection getConnection()throws SQLException{
  13.  
  14. try{
  15. System.out.println("Tutaj wchodze dbconneciton");
  16. Class.forName("org.sqlite.JDBC");
  17. return DriverManager.getConnection(SCONN);
  18. }catch (ClassNotFoundException ex){
  19. System.out.println("Tutaj wchodze dbconneciton catch");
  20. ex.printStackTrace();
  21. }
  22. return null;
  23. }
  24. }
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31. package loginapp;
  32.  
  33. import javafx.application.Application;
  34. import javafx.fxml.FXMLLoader;
  35. import javafx.scene.Parent;
  36. import javafx.scene.Scene;
  37. import javafx.stage.Stage;
  38.  
  39.  
  40. public class LoginApp extends Application {
  41.  
  42. public void start(Stage stage) throws Exception{
  43. Parent root = (Parent) FXMLLoader.load(getClass().getResource("login.fxml"));
  44.  
  45. Scene scene = new Scene(root);
  46. stage.setScene(scene);
  47. stage.setTitle("EmotionalGraph");
  48. stage.show();
  49.  
  50. }
  51. public static void main(String []args ){
  52. launch(args);
  53. }
  54. }
  55.  
  56.  
  57.  
  58.  
  59.  
  60. package loginapp;
  61.  
  62. import User.UserContoller;
  63. import javafx.event.ActionEvent;
  64. import javafx.fxml.FXML;
  65. import javafx.fxml.FXMLLoader;
  66. import javafx.fxml.Initializable;
  67. import javafx.scene.Scene;
  68. import javafx.scene.control.Button;
  69. import javafx.scene.control.Label;
  70. import javafx.scene.control.PasswordField;
  71. import javafx.scene.control.TextField;
  72. import javafx.scene.layout.Pane;
  73. import javafx.stage.Stage;
  74.  
  75. import java.io.IOException;
  76. import java.net.URL;
  77. import java.util.ResourceBundle;
  78.  
  79. public class LoginController implements Initializable {
  80.  
  81. LoginModel loginModel = new LoginModel();
  82.  
  83. @FXML
  84. private Label dbstatus;
  85. @FXML
  86. private TextField username;
  87. @FXML
  88. private PasswordField password;
  89. @FXML
  90. private Button loginButton;
  91. @FXML
  92. private Label loginStatus;
  93.  
  94.  
  95. public void initialize(URL url, ResourceBundle rb){
  96. if(this.loginModel.isDataBaseConnected()){
  97. this.dbstatus.setText("Connected");
  98. }else{
  99. this.dbstatus.setText("Not Connected");
  100. }
  101.  
  102.  
  103. }
  104. @FXML
  105. public void login(ActionEvent event){
  106. try{
  107. if(this.loginModel.isLogin(this.username.getText(),this.password.getText())) {
  108. Stage stage = (Stage) this.loginButton.getScene().getWindow();
  109. userLogin();
  110. stage.close();
  111.  
  112. }else {
  113. loginStatus.setText("Wrong Username or Password");
  114. }
  115.  
  116.  
  117. }catch(Exception localException){
  118. }
  119. }
  120. public void userLogin(){
  121. try {
  122. Stage userStage = new Stage();
  123. FXMLLoader loader = new FXMLLoader();
  124. Pane root = (Pane)loader.load(getClass().getResource("/User/UserFXML.fxml").openStream());
  125.  
  126. UserContoller userContoller = (UserContoller)loader.getController();
  127. Scene scene = new Scene(root);
  128. userStage.setScene(scene);
  129. userStage.setTitle("Emotions");
  130. userStage.setResizable(false);
  131. userStage.show();
  132. }catch (IOException ex){
  133. ex.printStackTrace();
  134. }
  135.  
  136. }
  137.  
  138.  
  139. }
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147. package loginapp;
  148.  
  149. import java.sql.Connection;
  150. import java.sql.PreparedStatement;
  151. import java.sql.SQLException;
  152. import java.sql.ResultSet;
  153.  
  154. import DbUtil.dbconnection;
  155.  
  156.  
  157.  
  158.  
  159. public class LoginModel {
  160. Connection connection;
  161.  
  162. public LoginModel(){
  163. try{
  164. this.connection = dbconnection.getConnection();
  165. }catch(SQLException ex){
  166. ex.printStackTrace();
  167. }
  168. if(this.connection==null){
  169. System.exit(1);
  170. }
  171. }
  172.  
  173. public boolean isDataBaseConnected(){
  174. return this.connection != null;
  175. }
  176. public boolean isLogin(String username,String password)throws Exception{
  177.  
  178. PreparedStatement pr = null;
  179.  
  180. ResultSet rs = null;
  181.  
  182.  
  183. String sql= "SELECT * FROM login where username = ? and password = ?";
  184.  
  185.  
  186.  
  187. try {
  188.  
  189.  
  190. pr = this.connection.prepareStatement(sql);
  191.  
  192. pr.setString(1, username);
  193. pr.setString(2, password);
  194.  
  195. rs = pr.executeQuery();
  196.  
  197. boolean boll1;
  198.  
  199. if (rs.next()) {
  200. return true;
  201. }
  202. return false;
  203. }catch(SQLException ex){
  204. System.out.println(ex);
  205. return false;
  206. }
  207. finally {
  208. pr.close();
  209. rs.close();
  210. }
  211. }
  212.  
  213.  
  214. }
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221. package User;
  222.  
  223.  
  224. import javafx.beans.property.SimpleStringProperty;
  225. import javafx.beans.property.StringProperty;
  226.  
  227. public class EmoData {
  228.  
  229. private final StringProperty stan;
  230. private final StringProperty czas;
  231.  
  232.  
  233. public EmoData(String Stan, String Czas) {
  234. this.stan = new SimpleStringProperty(Stan);
  235. this.czas = new SimpleStringProperty(Czas);
  236. }
  237.  
  238. public String getCzas() {
  239. return czas.get();
  240. }
  241. public StringProperty czasProperty() {
  242. return this.czas;
  243. }
  244. public void setCzas(String czas) {
  245. this.czas.set(czas);
  246. }
  247. public String getStan() {
  248. return stan.get();
  249. }
  250. public StringProperty stanProperty() {
  251. return this.stan;
  252. }
  253. public void setStan(String stan) {
  254. this.stan.set(stan);
  255. }
  256.  
  257. }
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265. package User;
  266.  
  267. import DbUtil.dbconnection;
  268. import javafx.collections.FXCollections;
  269. import javafx.collections.ObservableList;
  270. import javafx.fxml.FXML;
  271. import javafx.fxml.Initializable;
  272. import javafx.scene.control.Button;
  273. import javafx.scene.control.TableColumn;
  274. import javafx.scene.control.TableView;
  275. import java.awt.event.ActionEvent;
  276. import java.net.URL;
  277. import java.sql.Connection;
  278. import java.sql.PreparedStatement;
  279. import java.sql.ResultSet;
  280. import java.sql.SQLException;
  281. import java.util.ResourceBundle;
  282.  
  283.  
  284. public abstract class UserContoller implements Initializable {
  285. @FXML
  286. private Button badbut;
  287. @FXML
  288. private Button natbut;
  289. @FXML
  290. private Button goodbut;
  291. @FXML
  292. private TableView<EmoData> emotable;
  293. @FXML
  294. private TableColumn<EmoData, String> stancolumn;
  295. @FXML
  296. private TableColumn<EmoData, String> czascolumn;
  297.  
  298. private dbconnection dc;
  299. private ObservableList<EmoData> data;
  300. private String sql = "SELECT * FROM stany";
  301.  
  302. public void initialize(URL url, ResourceBundle rb) {
  303. this.dc = new dbconnection();
  304. }
  305.  
  306. @FXML
  307. private void loadstanydata(ActionEvent event) throws SQLException {
  308. try {
  309. Connection conn = dbconnection.getConnection();
  310. this.data = FXCollections.observableArrayList();
  311.  
  312. ResultSet rs = conn.createStatement().executeQuery(sql);
  313. while (rs.next()) {
  314. this.data.add(new EmoData(rs.getString(1), rs.getString(2)));
  315. }
  316. } catch (SQLException e) {
  317. System.out.println("Error:" + e);
  318. }
  319.  
  320.  
  321. }
  322. @FXML
  323. public void addGood(ActionEvent event){
  324. String sqlInsertGood = "INSERT INTO STANY VALUES ('DOBRZE', NOW())";
  325.  
  326. try{
  327. Connection conn = dbconnection.getConnection();
  328. PreparedStatement stat1 = conn.prepareStatement(sqlInsertGood);
  329.  
  330. stat1.execute();
  331. conn.close();
  332. }catch (SQLException e){
  333. e.printStackTrace();
  334. }
  335.  
  336. }
  337. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement