Saturday, November 14, 2015

JavaFx Sqlite Database Tutorial 2 - How Connect Sqlite to JavaFx







package application;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;

public class LoginController implements Initializable {
 public LoginModel loginModel = new LoginModel();
 
 
   @FXML
   private Label isConnected;
 @Override
 public void initialize(URL location, ResourceBundle resources) {
  // TODO Auto-generated method stub
  if (loginModel.isDbConnected()) {
   isConnected.setText("Connected");
  } else {

   isConnected.setText("Not Connected");
  }
 }
  
}




package application;
import java.sql.*;
public class LoginModel {
  Connection conection;
  public LoginModel () {
   conection = SqliteConnection.Connector();
   if (conection == null) {

   System.out.println("connection not successful");
    System.exit(1);}
  }
  
  public boolean isDbConnected() {
   try {
  return !conection.isClosed();
 } catch (SQLException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  return false;
 }
}
}



package application;
 
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
 

public class Main extends Application {
 @Override
 public void start(Stage primaryStage) {
  try {
   Parent root = FXMLLoader.load(getClass().getResource("/application/Login.fxml"));
   Scene scene = new Scene(root);
   scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
   primaryStage.setScene(scene);
   primaryStage.show();
  } catch(Exception e) {
   e.printStackTrace();
  }
 }
 
 public static void main(String[] args) {
  launch(args);
 }
}



package application;
import java.sql.*;
public class SqliteConnection {
  public static Connection Connector() {
 try {
  Class.forName("org.sqlite.JDBC");
  Connection conn =DriverManager.getConnection("jdbc:sqlite:EmployeeDb.sqlite");
  return conn;
 } catch (Exception e) {
  System.out.println(e);
  return null;
  // TODO: handle exception
 }
}
}



<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="300.0" prefWidth="300.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.LoginController">
   <children>
      <Label fx:id="isConnected" layoutX="54.0" layoutY="35.0" prefHeight="46.0" prefWidth="192.0" text="Status" textFill="#cd1313">
         <font>
            <Font size="18.0" />
         </font>
      </Label>
   </children>
</AnchorPane>



7 comments:

  1. For android apps and info of Technology Visit
    Proandroapps9.blogpsot.com

    ReplyDelete
  2. This blog awesome and i learn a lot about programming from here.The best thing about this blog is that you doing from beginning to experts level.

    Love from

    ReplyDelete
  3. I downloaded Eclipse and the newest Java SE Development Kit 8. But I had no option to create a JavaFX Project, there was no such option in Eclipse.

    I just started a normal Java Project and tried to import all the JavaFX modules, which worked well, guess it can be done that way to? I Just started watching this tutorials, lets see if it works.

    ReplyDelete
    Replies
    1. try netbeans and scene builder

      Delete
  4. how to retrieve DB record and display in desktop application.

    ReplyDelete
  5. Where is the next part? JavaFx Sqlite Database Tutorial 3?

    ReplyDelete