Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

To connect in Derby at home in your local desktop or laptop:

1.Go to Services
2.choose Databases -> select Java Db-> select create Database
You will get this screen.

Enter the database name, username, password and select ok


For ex: logindb,rose,123,123
3. In the above screen right click the blue selection and give
connect. Your database will be created.
4. Create table as shown below
5.After creating Table go to javafx application and create a new
project and type the below code.
import javafx.application.Application;

import javafx.event.ActionEvent;

import javafx.event.EventHandler;

import javafx.geometry.Insets;

import javafx.geometry.Pos;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.control.Label;

import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;

import javafx.scene.image.Image;

import javafx.scene.image.ImageView;

import javafx.scene.layout.HBox;

import javafx.scene.layout.StackPane;

import javafx.scene.layout.VBox;

import javafx.scene.paint.Color;

import javafx.scene.text.Font;

import javafx.stage.Stage;

import java.sql.*;

/**

* @author Rosy

*/

public class Loginjdbc extends Application {

Label l1,l2,l3;

TextField t1,t2;

PasswordField p1;

Button b1;

@Override

public void start(Stage primaryStage) {

// Button btn = new Button();

HBox h1 = new HBox();

HBox h2 = new HBox();

VBox root = new VBox();

root.setMaxSize(200, 200);

Image i1 = new Image("personicon.png");

Image i2 = new Image("humanicon.jpg");

ImageView iv1 = new ImageView(i1);

ImageView iv2= new ImageView(i2);


iv1.setFitHeight(40);

iv1.setFitWidth(40);

iv2.setFitHeight(40);

iv2.setFitWidth(40);

l1 = new Label("Login Page");

l1.setFont(new Font(25));

l1.setTextFill(Color.AQUAMARINE);

t1 = new TextField();

t1.setPromptText("UserName");

h1.setMaxSize(170,170);

h1.getChildren().addAll(iv1,t1);

p1= new PasswordField();

h2.setMaxSize(170,170);

h2.getChildren().addAll(iv2,p1);

b1 = new Button("Login");

b1.setMaxSize(130,130);

root.getChildren().addAll(l1,h1,h2,b1);

root.setAlignment(Pos.CENTER);

root.setPadding(new Insets(10));

root.setSpacing(10);

//root.setStyle();

Scene scene = new Scene(root, 300, 300);

scene.setFill(Color.AQUA);

scene.getStylesheets().add(getClass().getResource("css1.css").toExternalForm());

b1.setOnAction(new EventHandler<ActionEvent>() {
@Override

public void handle(ActionEvent event) {

// System.out.println("Hello World!");

try

Class.forName("org.apache.derby.jdbc.ClientDriver");

Connection con =DriverManager.getConnection("jdbc:derby://localhost:1527/logindb",


"rose","rose");

PreparedStatement ps = con.prepareStatement("Insert into login1 values (?,?)");

ps.setString(1,(t1.getText()));

ps.setInt(2,Integer.parseInt(p1.getText()));

int r = ps.executeUpdate();

System.out.println("r records inserted"+ r);

catch(Exception e)

System.out.println(e);

});

primaryStage.setTitle("Hello World!");

primaryStage.setScene(scene);

primaryStage.show();

/**

* @param args the command line arguments

*/

public static void main(String[] args) {


launch(args);

6. To run the program do the following :


A] add the css file.
B] add the person and human icon image or download some jpg
images from internet.

7. important point :
In libraries of loginjdbc project -> right click and select Add Library -> you will get below screen.

Choose Java DB driver and select Add Library.

Jar files will be added .

Now you can run the program and get the output as follows.
Try this and still any doubt ask tommorrow in college.

Similarly try for registration Page also:


Sample code and output:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package regjdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;

/**
*
* @author Rosy
*/
public class Regjdbc extends Application {
String butvalue=null;
@Override

public void start(Stage primaryStage) {


TextField t1,t2;
Label l1,l2,l3,l4,l5;
RadioButton r1,r2,r3;

ComboBox c1;
CheckBox cb1,cb2;
Button b1,b2,b3,b4;
ToggleGroup tg;

HBox h1,h2;
VBox root = new VBox();
Text tt1 = new Text("REGISTRATION PAGE");
tt1.setFont(Font.font("Arial",30));
tt1.setX(100);
tt1.setY(100);

// tt1.setTextAlignment(TextAlignment.CENTER);
l1 = new Label("Name");
t1 = new TextField();
l2 = new Label("Age");
t2 = new TextField();
l3 = new Label("Gender");
h1 = new HBox();
cb1 = new CheckBox("Male");
cb2 = new CheckBox("Female");
h1.getChildren().addAll(cb1,cb2);
l4 = new Label("Course");
tg = new ToggleGroup();
r1 = new RadioButton("Science");
r1.setToggleGroup(tg);
r2 = new RadioButton("Arts");
r2.setToggleGroup(tg);
r3 = new RadioButton("BCom");
r3.setToggleGroup(tg);
r1.setOnAction(new EventHandler<ActionEvent>() {

@Override
public void handle(ActionEvent event) {
butvalue=r1.getText();
}
});
r2.setOnAction(new EventHandler<ActionEvent>() {

@Override
public void handle(ActionEvent event) {
butvalue=r2.getText();
}
});
r3.setOnAction(new EventHandler<ActionEvent>() {

@Override
public void handle(ActionEvent event) {
butvalue=r3.getText();
}
});

h2 = new HBox();
h2.getChildren().addAll(r1,r2,r3);

b1 = new Button("INSERT");

b1.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// System.out.println("Hello World!");
String gen;
try
{
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection con
=DriverManager.getConnection("jdbc:derby://localhost:1527/logindb", "rose","rose");
PreparedStatement ps = con.prepareStatement("Insert into reg values (?,?,?,?)");
ps.setString(1,(t1.getText()));
ps.setInt(2,Integer.parseInt(t2.getText()));

// to insert value from checkbox


if (cb1.isSelected())
gen = "MALE";
else
gen="FEMALE";
ps.setString(3,gen);

//to insert value from radiobutton

ps.setString(4,butvalue);
//labelInfo.setText("You are " + button.getText());

int r = ps.executeUpdate();
System.out.println("r records inserted"+ r);

}
catch(Exception e)
{
System.out.println(e);
}
}
});

root.getChildren().addAll(tt1,l1,t1,l2,t2,l3,h1,l4,h2,b1);

Scene scene = new Scene(root, 500,500);

primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

}
Try adding combo box and listview also to this.

You might also like