Advertisement
Guest User

Untitled

a guest
Oct 21st, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.31 KB | None | 0 0
  1. import javafx.application.Application;
  2. import javafx.fxml.*;
  3. import javafx.scene.Parent;
  4. import javafx.scene.Scene;
  5. import javafx.stage.Stage;
  6.  
  7. public class Main extends Application {
  8.  
  9. @Override
  10. public void start(Stage primaryStage) {
  11. try {
  12. Parent root = FXMLLoader.load(getClass().getResource("login1.fxml"));
  13. primaryStage.setTitle("NOVOTEL HOTEL ");
  14. primaryStage.setScene(new Scene(root, 500, 475));
  15. primaryStage.show();
  16. }
  17. catch(Exception e )
  18. {
  19. e.printStackTrace();
  20. }
  21. }
  22.  
  23.  
  24. public static void main(String[] args) {
  25. launch(args);
  26. }
  27. }
  28.  
  29. package sample;
  30. import com.sun.istack.internal.NotNull;
  31. import com.sun.istack.internal.Nullable;
  32.  
  33. import java.sql.*;
  34.  
  35. public class LoginModel {
  36. int flag=0;
  37.  
  38. @Nullable
  39. @NotNull
  40. Connection connection;
  41.  
  42. public LoginModel() {
  43. connection = MysqlConnector.Connector();
  44. }
  45.  
  46. public boolean isDBConnected() {
  47. try {
  48. boolean b = !connection.isClosed();
  49. return b;
  50. } catch (SQLException e) {
  51. e.printStackTrace();
  52. boolean c = false;
  53. return c;
  54.  
  55.  
  56. }
  57.  
  58. }
  59.  
  60. public boolean isLogin(String user, String pass) throws SQLException {
  61. PreparedStatement preparedstatement = null;
  62. ResultSet resultSet = null;
  63. String query = "select * from employee where employeename=? and employeepassword = ?";
  64. try {
  65. preparedstatement = connection.prepareStatement(query);
  66. preparedstatement.setString(1, user);
  67. preparedstatement.setString(2, pass);
  68. resultSet = preparedstatement.executeQuery();
  69. if (resultSet.next()) {
  70. flag=1;
  71. return true;
  72. }
  73. else{
  74. return false;
  75. }
  76.  
  77.  
  78. } catch (Exception e) {
  79. return false;
  80. } finally {
  81. preparedstatement.close();
  82. resultSet.close();
  83. }
  84. }
  85. }
  86.  
  87. package sample;
  88.  
  89. import com.sun.istack.internal.NotNull;
  90. import com.sun.istack.internal.Nullable;
  91. import javafx.event.ActionEvent;
  92. import javafx.fxml.FXML;
  93. import javafx.fxml.FXMLLoader;
  94. import javafx.stage.*;
  95. import javafx.scene.*;
  96. import javafx.scene.layout.*;
  97. import javafx.scene.control.*;
  98. import javafx.geometry.*;
  99.  
  100. import javafx.fxml.Initializable;
  101. import javafx.scene.control.Label;
  102. import javafx.scene.control.TextField;
  103.  
  104. import java.net.URL;
  105. import java.sql.SQLException;
  106. import java.util.ResourceBundle;
  107. import javafx.*;
  108. public class Controller implements Initializable {
  109. @NotNull
  110. @Nullable
  111. public LoginModel loginModel = new LoginModel();
  112. @FXML
  113.  
  114. private Label isConnected,db;
  115. @FXML
  116. private TextField txtUsername;
  117. @FXML
  118. private TextField txtpassword;
  119.  
  120. @FXML
  121. Button abutton = new Button("Login");
  122.  
  123. private AddCustomer addCustomer;
  124.  
  125. @Override
  126. public void initialize(URL location, ResourceBundle resources) {
  127. // TODO Auto-generated method stub
  128. if (loginModel.isDBConnected()) {
  129. db.setText("Connected to DATABASE");
  130. } else {
  131. db.setText("Not Connected");
  132. }
  133. }
  134.  
  135. public void Login (ActionEvent event ) {
  136. try {
  137. if (loginModel.isLogin(txtUsername.getText(), txtpassword.getText())) {
  138. isConnected.setText("Correct");
  139. try {
  140. FXMLLoader addCustomer= new FXMLLoader(getClass().getResource("/sample/addcustomer.fxml"));
  141. Parent root1 = (Parent)addCustomer.load();
  142. Stage stage = new Stage();
  143. stage.setScene(new Scene(root1));
  144. stage.setTitle("ENTRY RECORD");
  145. stage.show();
  146. }
  147. catch (Exception e){
  148. e.printStackTrace();
  149. }
  150.  
  151. } else {
  152. Alert alert=new Alert(Alert.AlertType.ERROR);
  153. alert.setHeaderText(null);
  154. alert.setContentText("YOU ARE NOT AUTHENTICATED");
  155. alert.show();
  156.  
  157.  
  158. }
  159. } catch (SQLException e) {
  160.  
  161. isConnected.setText("Incorrect");
  162. e.printStackTrace();
  163. }
  164. }
  165.  
  166. package sample;
  167. import com.sun.istack.internal.NotNull;
  168. import com.sun.istack.internal.Nullable;
  169. import javafx.fxml.FXML;
  170.  
  171. import java.sql.*;
  172.  
  173.  
  174. public class MysqlConnector {
  175. @FXML
  176. public static Statement stmt;
  177. @FXML
  178. public static Connection conn;
  179.  
  180.  
  181.  
  182.  
  183. public MysqlConnector() {
  184. conn=Connector();
  185. String TableName = "Customer_Details";
  186. try {
  187. stmt = conn.createStatement();
  188. DatabaseMetaData dbm = conn.getMetaData();
  189. ResultSet tables = dbm.getTables(null, null, TableName.toUpperCase(), null);
  190. if (tables.next()) {
  191. System.out.println("Ready to go");
  192.  
  193. } else {
  194. stmt.execute("CREATE TABLE " + TableName + "("
  195. + " customername varchar(45) primary key,"
  196. + " customernumber varchar(45),n"
  197. + " customerstay int,n"
  198. + " customerguests varchar(45)"
  199. + ")");
  200. }
  201. } catch (SQLException e1) {
  202. System.err.println(e1.getMessage());
  203. }
  204. }
  205.  
  206.  
  207. @Nullable
  208. @NotNull
  209. public static Connection Connector() {
  210.  
  211.  
  212. try {
  213. Class.forName("com.mysql.jdbc.Driver");
  214. Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/login", "root", "password");
  215. return conn;
  216. } catch (Exception e) {
  217. return null;
  218. }
  219.  
  220. }
  221. @NotNull
  222. @Nullable
  223. public ResultSet executeQuery(String query) {
  224.  
  225. ResultSet result ;
  226.  
  227. try {
  228. stmt = conn.createStatement();
  229. result=stmt.executeQuery(query);
  230.  
  231.  
  232.  
  233. } catch (SQLException e) {
  234. System.out.println("Error here");
  235. e.printStackTrace();
  236. return null;
  237.  
  238. }
  239. return result;
  240. }
  241. @NotNull
  242. @Nullable
  243. public boolean executeAction(String qu) {
  244. try {
  245. stmt = conn.createStatement();
  246. stmt.execute(qu);
  247. return true;
  248.  
  249.  
  250. } catch (SQLException e) {
  251. e.printStackTrace();
  252. return false;
  253. }
  254. }
  255. }
  256.  
  257. package sample;
  258.  
  259. import com.sun.istack.internal.NotNull;
  260. import com.sun.istack.internal.Nullable;
  261. import com.sun.org.apache.bcel.internal.generic.RETURN;
  262. import javafx.event.ActionEvent;
  263. import javafx.fxml.FXML;
  264. import javafx.fxml.FXMLLoader;
  265. import javafx.scene.Parent;
  266. import javafx.scene.Scene;
  267. import javafx.scene.control.Alert;
  268. import javafx.scene.control.Button;
  269. import javafx.scene.control.TextField;
  270. import javafx.stage.Stage;
  271.  
  272. public class AddCustomer {
  273. @FXML
  274. public TextField number;
  275. @FXML
  276. public TextField name;
  277. @FXML
  278. public TextField stay;
  279. @FXML
  280. public TextField noofguests;
  281. @FXML
  282. public Button savebutton;
  283. @FXML
  284. public Button cancelbutton;
  285. @FXML
  286. @NotNull
  287. @Nullable
  288. public void addcustomer(ActionEvent actionEvent) throws Exception {
  289.  
  290.  
  291. String customername = name.getText();
  292. String customernumber = number.getText();
  293. String customerstay = stay.getText();
  294. String customerguests = noofguests.getText();
  295. if (customername.isEmpty() || customerguests.isEmpty() || customernumber.isEmpty() || customerstay.isEmpty()) {
  296. Alert alert = new Alert(Alert.AlertType.ERROR);
  297. alert.setHeaderText(null);
  298. alert.setContentText("PLEASE ENTER ALL THE FIELDS");
  299. alert.show();
  300. return;
  301.  
  302. }
  303.  
  304. String qu = "INSERT INTO Customer_Details VALUES(" + "'" + customername + "'," +
  305. "'" + customernumber + "'," +
  306. "'" + customerstay + "'," +
  307. "'" + customerguests + "'" + ")";
  308. System.out.println(qu);
  309.  
  310. @NotNull
  311. @Nullable
  312. MysqlConnector mysqlConnector=new MysqlConnector();
  313. try {
  314.  
  315. if (mysqlConnector.executeAction(qu)) {
  316. @NotNull
  317. @Nullable
  318. Alert alert = new Alert(Alert.AlertType.INFORMATION);
  319. alert.setHeaderText(null);
  320. alert.setContentText("SUCCESS");
  321. alert.show();
  322. } else {
  323. @NotNull
  324. @Nullable
  325. Alert alert = new Alert(Alert.AlertType.ERROR);
  326. alert.setHeaderText(null);
  327. alert.setContentText("FAILED");
  328. alert.show();
  329. }
  330. }
  331. catch(Exception e)
  332. { e.printStackTrace();}
  333.  
  334. }
  335. public void displaydata(ActionEvent actionEvent) {
  336. try {
  337. FXMLLoader addCustomer= new FXMLLoader(getClass().getResource("/sample/List View.fxml"));
  338. Parent root1 = (Parent)addCustomer.load();
  339. Stage stage = new Stage();
  340. stage.setScene(new Scene(root1));
  341. stage.setTitle("DISPLAY RECORD");
  342. stage.show();
  343. }
  344. catch (Exception e){
  345. e.printStackTrace();
  346. }
  347. }
  348. }
  349.  
  350. package sample;
  351.  
  352. import javafx.beans.property.SimpleStringProperty;
  353. import javafx.collections.FXCollections;
  354. import javafx.collections.ObservableList;
  355. import javafx.fxml.Initializable;
  356. import javafx.scene.control.TableColumn;
  357. import javafx.scene.control.TableView;
  358. import javafx.scene.control.cell.PropertyValueFactory;
  359.  
  360. import java.awt.print.Book;
  361. import java.net.URL;
  362. import java.sql.ResultSet;
  363. import java.sql.SQLException;
  364. import java.util.ResourceBundle;
  365.  
  366. public class ListCustomers implements Initializable {
  367. ObservableList<Hotel> list= FXCollections.observableArrayList();
  368. public TableView<Hotel> tableView;
  369. public TableColumn<Hotel, String> namecolumn;
  370. public TableColumn<Hotel, String> numbercolumn;
  371. public TableColumn<Hotel, String> staycolumn;
  372. public TableColumn<Hotel, String> guestscolumn;
  373.  
  374. public void initialize(URL location, ResourceBundle resources) {
  375. initCol();
  376.  
  377. loadData();
  378. }
  379.  
  380. private void loadData() {
  381. MysqlConnector mysqlConnector=new MysqlConnector();
  382. String qu = "SELECT * FROM Customer_Details";
  383. ResultSet rs = mysqlConnector.executeQuery(qu);
  384. try {
  385. while (rs.next()) {
  386. String name = rs.getString("customername");
  387. String number = rs.getString("customernumber");
  388. String stay= rs.getString("customerstay");
  389. String guests = rs.getString("customerguests");
  390.  
  391. list.add(new Hotel(name,number,stay,guests));
  392.  
  393. }
  394. } catch (SQLException ex) {
  395. ex.printStackTrace();
  396. }
  397.  
  398. tableView.getItems().setAll(list);
  399. }
  400.  
  401. private void initCol(){
  402. namecolumn.setCellValueFactory(new PropertyValueFactory<Hotel, String>("Name"));
  403. numbercolumn.setCellValueFactory(new PropertyValueFactory<Hotel, String>("Number"));
  404. staycolumn.setCellValueFactory(new PropertyValueFactory<Hotel, String>("Stay Period"));
  405. guestscolumn.setCellValueFactory(new PropertyValueFactory<Hotel, String>("Guests"));
  406.  
  407. }
  408.  
  409. public static class Hotel {
  410.  
  411. private final SimpleStringProperty customername;
  412. private final SimpleStringProperty customernumber;
  413. private final SimpleStringProperty customerstay;
  414. private final SimpleStringProperty customerguests;
  415.  
  416.  
  417. public Hotel(String customername, String customernumber, String customerstay, String customerguests) {
  418. this.customername = new SimpleStringProperty(customername);
  419. this.customernumber = new SimpleStringProperty(customernumber);
  420. this.customerguests = new SimpleStringProperty(customerstay);
  421. this.customerstay = new SimpleStringProperty(customerguests);
  422.  
  423. }
  424.  
  425. public String getcustomername() {
  426. return customername.get();
  427. }
  428.  
  429. public String getcustomernumber() {
  430. return customernumber.get();
  431. }
  432.  
  433. public String getcustomerstay() {
  434. return customerstay.get();
  435. }
  436.  
  437. public String customerguests() {
  438. return customerguests.get();
  439. }
  440.  
  441.  
  442. }
  443. }
  444.  
  445. <?xml version="1.0" encoding="UTF-8"?>
  446.  
  447. <?import javafx.scene.control.TableColumn?>
  448. <?import javafx.scene.control.TableView?>
  449. <?import javafx.scene.layout.AnchorPane?>
  450.  
  451.  
  452. <AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.ListCustomers">
  453. <children>
  454. <TableView fx:id="tableView" layoutX="8.0" layoutY="6.0" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
  455. <columns>
  456. <TableColumn fx:id="namecolumn" prefWidth="75.0" text="Customer Name" />
  457. <TableColumn fx:id="numbercolumn" prefWidth="75.0" text="Contact Number" />
  458. <TableColumn fx:id="staycolumn" prefWidth="75.0" text="Stay Period (Days)" />
  459. <TableColumn fx:id="guestscolumn" prefWidth="75.0" text="Total Guests" />
  460. </columns>
  461. <columnResizePolicy>
  462. <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
  463. </columnResizePolicy>
  464. </TableView>
  465. </children>
  466. </AnchorPane>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement