Guest User

Untitled

a guest
Mar 23rd, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.63 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 javafxloginwithdb;
  7.  
  8. import javafx.application.Application;
  9. import javafx.stage.Stage;
  10. import java.awt.event.*;
  11. import java.sql.Connection;
  12. import java.sql.DriverManager;
  13. import java.sql.Statement;
  14. import javafx.event.EventHandler;
  15. import javafx.geometry.Pos;
  16. import javafx.scene.Scene;
  17. import javafx.scene.control.Alert;
  18. import javafx.scene.control.Button;
  19. import javafx.scene.control.PasswordField;
  20. import javafx.scene.control.TextField;
  21. import javafx.scene.layout.VBox;
  22. import javafx.scene.text.Text;
  23. import javax.swing.*;
  24.  
  25. /**
  26.  *
  27.  * @author jubai
  28.  */
  29. public class MyClass extends Application {
  30.  
  31.     @Override
  32.     public void start(Stage primaryStage) throws Exception {
  33.         VBox root = new VBox();
  34.         Text txt1 = new Text("Id");
  35.         Text txt2 = new Text("First Name");
  36.         Text txt3 = new Text("Last Name");
  37.         Text txt4 = new Text("Age");
  38.         TextField txf1 = new TextField();
  39.         TextField txf2 = new TextField();
  40.         TextField txf3 = new TextField();
  41.         TextField txf4 = new TextField();
  42.        
  43.         Button add = new Button("Add");
  44.         Button delete = new Button("Delete");
  45.         Button update = new Button("Update");
  46.        
  47.         add.setOnAction(new EventHandler<javafx.event.ActionEvent>() {
  48.            
  49.             @Override
  50.             public void handle(javafx.event.ActionEvent event) {
  51.                 try{
  52.                     theQuery("insert into users (fname,lname,age) values('"+txf2.getText()+"','"+txf3.getText()+"',"+txf4.getText()+")");
  53.                 }
  54.                 catch(Exception ex){}
  55.                
  56.             }
  57.         });
  58.        
  59.         delete.setOnAction(new EventHandler<javafx.event.ActionEvent>() {
  60.            
  61.             @Override
  62.             public void handle(javafx.event.ActionEvent event) {
  63.                 try{
  64.          
  65.                     theQuery("delete from users where id = "+txf1.getText());
  66.                 }
  67.                 catch(Exception ex){}
  68.                
  69.             }
  70.         });
  71.        
  72.         update.setOnAction(new EventHandler<javafx.event.ActionEvent>() {
  73.            
  74.             @Override
  75.             public void handle(javafx.event.ActionEvent event) {
  76.                 try{
  77.          
  78.                     theQuery("update users set fname = '"+txf2.getText()+"',lname = '"+txf3.getText()+"', age = "+txf4.getText()+" where id = "+txf1.getText());
  79.                 }
  80.                 catch(Exception ex){}
  81.                
  82.             }
  83.         });
  84.        
  85.         root.getChildren().addAll(txt1,txt2,txt3,txt4,txf1,txf2,txf3,txf4,add,delete,update);
  86.         root.setAlignment(Pos.CENTER);
  87.         root.setSpacing(20);
  88.        
  89.         Scene scene = new Scene(root, 200, 300);
  90.         primaryStage.setMaxWidth(200);
  91.         primaryStage.setMaxHeight(300);
  92.         primaryStage.setTitle("Student_Info");
  93.         primaryStage.setScene(scene);
  94.         primaryStage.show();
  95.        
  96.     }
  97.    
  98.    
  99.    
  100.    
  101.    
  102.    
  103.    
  104.  //function to execute the insert update delete query
  105.   public void theQuery(String query){
  106.       Connection con = null;
  107.       Statement st = null;
  108.       try{
  109.           con = DriverManager.getConnection("jdbc:mysql://localhost/test_db","root","");
  110.           st = con.createStatement();
  111.           st.executeUpdate(query);
  112.           JOptionPane.showMessageDialog(null,"Query Executed");
  113.       }catch(Exception ex){
  114.           JOptionPane.showMessageDialog(null,ex.getMessage());
  115.       }
  116.   }
  117.  
  118.  
  119.      public static void main(String[] args){
  120.      
  121.          new  MyClass();
  122.      }
  123.  
  124. }
Add Comment
Please, Sign In to add comment