Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package javafxloginwithdb;
- import javafx.application.Application;
- import javafx.stage.Stage;
- import java.awt.event.*;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.Statement;
- import javafx.event.EventHandler;
- import javafx.geometry.Pos;
- import javafx.scene.Scene;
- import javafx.scene.control.Alert;
- import javafx.scene.control.Button;
- import javafx.scene.control.PasswordField;
- import javafx.scene.control.TextField;
- import javafx.scene.layout.VBox;
- import javafx.scene.text.Text;
- import javax.swing.*;
- /**
- *
- * @author jubai
- */
- public class MyClass extends Application {
- @Override
- public void start(Stage primaryStage) throws Exception {
- VBox root = new VBox();
- Text txt1 = new Text("Id");
- Text txt2 = new Text("First Name");
- Text txt3 = new Text("Last Name");
- Text txt4 = new Text("Age");
- TextField txf1 = new TextField();
- TextField txf2 = new TextField();
- TextField txf3 = new TextField();
- TextField txf4 = new TextField();
- Button add = new Button("Add");
- Button delete = new Button("Delete");
- Button update = new Button("Update");
- add.setOnAction(new EventHandler<javafx.event.ActionEvent>() {
- @Override
- public void handle(javafx.event.ActionEvent event) {
- try{
- theQuery("insert into users (fname,lname,age) values('"+txf2.getText()+"','"+txf3.getText()+"',"+txf4.getText()+")");
- }
- catch(Exception ex){}
- }
- });
- delete.setOnAction(new EventHandler<javafx.event.ActionEvent>() {
- @Override
- public void handle(javafx.event.ActionEvent event) {
- try{
- theQuery("delete from users where id = "+txf1.getText());
- }
- catch(Exception ex){}
- }
- });
- update.setOnAction(new EventHandler<javafx.event.ActionEvent>() {
- @Override
- public void handle(javafx.event.ActionEvent event) {
- try{
- theQuery("update users set fname = '"+txf2.getText()+"',lname = '"+txf3.getText()+"', age = "+txf4.getText()+" where id = "+txf1.getText());
- }
- catch(Exception ex){}
- }
- });
- root.getChildren().addAll(txt1,txt2,txt3,txt4,txf1,txf2,txf3,txf4,add,delete,update);
- root.setAlignment(Pos.CENTER);
- root.setSpacing(20);
- Scene scene = new Scene(root, 200, 300);
- primaryStage.setMaxWidth(200);
- primaryStage.setMaxHeight(300);
- primaryStage.setTitle("Student_Info");
- primaryStage.setScene(scene);
- primaryStage.show();
- }
- //function to execute the insert update delete query
- public void theQuery(String query){
- Connection con = null;
- Statement st = null;
- try{
- con = DriverManager.getConnection("jdbc:mysql://localhost/test_db","root","");
- st = con.createStatement();
- st.executeUpdate(query);
- JOptionPane.showMessageDialog(null,"Query Executed");
- }catch(Exception ex){
- JOptionPane.showMessageDialog(null,ex.getMessage());
- }
- }
- public static void main(String[] args){
- new MyClass();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement