Advertisement
Guest User

JAVA

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