Advertisement
ahrlab

Javafx DB

May 5th, 2015
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.88 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3.  
  4. import javafx.application.Application;
  5. import javafx.event.ActionEvent;
  6. import javafx.event.EventHandler;
  7. import javafx.scene.Scene;
  8. import javafx.scene.control.Button;
  9. import javafx.scene.control.Label;
  10. import javafx.scene.control.TextField;
  11. import javafx.scene.layout.VBox;
  12. import javafx.stage.Stage;
  13.  
  14.  
  15. public class Login extends Application {
  16.    
  17.     @Override
  18.     public void start(Stage primaryStage) {
  19.         Label lbl1=new Label("User Name");
  20.         TextField txt1=new TextField();
  21.         Label lbl2=new Label("Password");
  22.         TextField txt2=new TextField();
  23.        
  24.         Button btn = new Button("Login");
  25.         btn.setScaleX(1);
  26.         btn.setScaleY(1);
  27.        
  28.         btn.setOnAction(new EventHandler<ActionEvent>() {
  29.             @Override
  30.             public void handle(ActionEvent event) {
  31.                 String query="Select * from user";
  32.                 System.out.println(query);
  33.                
  34.                 try {
  35.                    
  36.                     Class.forName("com.mysql.jdbc.Driver");
  37.  
  38.         Connection con=DriverManager.getConnection("jdbc:mysql://www.db4free.net:3306/demo","demo","demo");//try your
  39.  
  40.           Statement stmt=con.createStatement();
  41.                
  42.          
  43.         ResultSet rs=stmt.executeQuery(query);
  44.         while(rs.next())
  45.             System.out.println(rs.getString(1)+"  "+rs.getString(2));
  46.  
  47.             con.close();
  48.  
  49.                 }
  50.                 catch(Exception e){ System.out.println("\nError"+e);}
  51.             }
  52.             });
  53.        
  54.         VBox root = new VBox();
  55.         root.getChildren().addAll(lbl1,txt1,lbl2,txt2,btn);
  56.        
  57.         Scene scene = new Scene(root, 600, 550);
  58.        
  59.         primaryStage.setTitle("User Login");
  60.         primaryStage.setScene(scene);
  61.         primaryStage.show();
  62.     }
  63.  
  64.  
  65.    
  66.     public static void main(String[] args) {
  67.         launch(args);
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement