Saturday, September 23, 2017

gdb tutorial

In the prev tutorial we have seen that how to compile and run some files which was written in C and C++ G++ introduction
Now we will se how to see the debugging steps

first of all remember to add -g option to each of the commands to attach debugging information into the object files and the executable file

after that run gdb by typing
 gdb reciprocal

then run the program by typing
run 7
 we can now see the steps byb typing next

Copy writes  Advanced Linux Programming by Mark Mitchell, Jeffrey Oldham,and Alex Samuel

G++ tutorial

What is G++?
It's a cpp compiler which is a part from GNU Compiler Collection(gcc)

What we will do in this post?
We will write a simple C++ and C code then Compile them using gcc

Tools we need:
Text Editor (I use vim)
Command line knowledge
Linux distro

LETS'S START

Step 1:
write the main file which will be a C file

#include<stdio.h>
#include"reciprocal.hpp"

int main(char argc, char **argv){
    int i;
    i = atoi(argv[1]);
    printf("The reciprocal of %d is %g\n", i, reciprocal(i));
    return 0;
}
 Don't do anything else, as u can c that there is a header file called  reciprocal.hpp
so we will need to write the implementation of this header file (Source code(.cpp) and header file(.hpp))

First write the header file (reciprocal.hpp):
#ifdef __cplusplus
extern "C"{
#endif
    extern double reciprocal(int i);
#ifdef __cplusplus
}
#endif
and save  it, then open another file which will be the source code (reciprocal.cpp):
#include<cassert>
#include"reciprocal.hpp"

double reciprocal(int i){
    assert(i != 0);
    return 1.0/i;
}

 Now we have three files, Two source files (main.c and reciprocal.cpp) and one header file (reciprocal.hpp) so let's compile them.
First of all notice that reciprocal.hpp is included in both of the source files so we will need to compile it first (Actually we will compile the source file reciprocal.cpp) by this command
 g++ -c -I reciprocal.hpp reciprocal.cpp

 g++ is the compiler command (For C++)
-c an option to tell the compiler that we need only the object file
-I an option to include the file reciprocal.hpp
Second we will need to compile the main.c file by this command
gcc -c -I reciprocal.hpp main.c

again gcc is the command name (For C)
and the others are the same

Now we need to link the object files to create the executeable file
    g++ -o reciprocal main.o reciprocal.o

*Note that we use g++ to compile the files although the main file is a C file , that's it if u have many source files written in both C & C++ then you should use g++

Debugging:
We can see the debugging informaion by invoking the -g option in every command






Copy writes Advanced Linux Programming by Mark Mitchell, Jeffrey Oldham, and Alex Samuel
 

Friday, April 21, 2017

Populate data from MySql into TableView JavaFX


Populate data from database into TableView JavaFX
Step1:
Connect to the databse
Step2:
Design the form :-
put TableView and the button
Step3:
populate data
Step1:
Connect to Databse:-
Connector Class:
-First of all we need a connector class that initialize our connection to the database, in this case it will be a MySql DB
Make a normal class and name it JDBCMySQLConnection (It’s just a convention)

package application;
import java.sql.*;
public class JDBCMySQLConnection {
private static JDBCMySQLConnection instance = new JDBCMySQLConnection();
//URL to the databse
/*where jdbc is essential
* mysql is the dbms
* localhost: the host of the server
* jdbcdb: the name of the databse
*/
public static final String URL="jdbc:mysql://localhost/jdbcdb";
//User name to the database
public static final String USER_NAME="";
//Password
public static final String PASSWORD="";
//Driver : it comes with the jar file
public static final String DRIVER_CLASS="com.mysql.jdbc.Driver";
private JDBCMySQLConnection(){
try{
Class.forName(DRIVER_CLASS);
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
private Connection createConnection(){
Connection connection = null;
try{
connection = DriverManager.getConnection(URL, USER_NAME, PASSWORD);
}catch(SQLException e){
System.out.println("Error Unable to connect to DB");
}
return connection;
}
public static Connection getConnection(){
return instance.createConnection();
}
}
Step2 Design the form:
Here is the FXML file

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

<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.collections.*?>
<?import javafx.control.cell.*?>
<?import javafx.scene.control.cell.PropertyValueFactory?>
<?import application.*?>
<GridPane alignment="CENTER" hgap="10" vgap="10" xmlns:fx="http://javafx.com/fxml"
fx:controller="application.Test_Controller">
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0"/>
</padding>

<Button text="Search" onAction="#searchById" GridPane.columnIndex="0" GridPane.rowIndex="4"/>

<TableView fx:id="tableView" GridPane.columnIndex="4" GridPane.rowIndex="0">
<columns>
<TableColumn fx:id="dep_name_column" text="name"/>
<TableColumn fx:id="dep_location_column" text="location"/>
</columns>
</TableView>
</GridPane>

And the last step is to make a query and put its results in the table

Here is the controller code with comments which describe it

 package application;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;

public class Test_Controller {
//FXML id's
@FXML protected TextField dep_id_field;
@FXML protected TextField dep_name_field;
@FXML protected TextField dep_location_field;
@FXML protected TableView<Department> tableView = new TableView<Department>();
@FXML TableColumn<Department, String> dep_name_column ;
@FXML TableColumn<Department, String> dep_location_column;
Department department;



//API's variables
//Objects that we will use to connect to the DB
//Connection object which establish the connection
Connection connection = null;
//Object which holds the query
Statement statement = null;
//Object which holds the query results
ResultSet rs = null;



@FXML public void initialize()
{
//This is essential if u use JavaFX 8 to prevent blank columns
    tableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);

}
//Search Button
@FXML protected void searchById(ActionEvent event)
{
//ObservableList to automatically update the table
    final ObservableList<Department> results = FXCollections.observableArrayList();
//Assign columns to data
    dep_name_column.setCellValueFactory(cellData -> new ReadOnlyStringWrapper(cellData.getValue().getDepName()));
    dep_location_column.setCellValueFactory(cellData -> new ReadOnlyStringWrapper(cellData.getValue().getDepLocation()));
//Start databse stuff
    String query = "SELECT * FROM department";
    try{
        connection = JDBCMySQLConnection.getConnection();
        statement = connection.createStatement();
        rs = statement.executeQuery(query);
        while(rs.next()){
//Assign results to the list
            department = new Department(rs.getString("dep_name"), rs.getString("location"));
            results.add(department);
        }
//Put the list into the table
        tableView.setItems(results);
    }catch(SQLException e){
        e.printStackTrace();
    }finally {
        if(connection != null) {
            try{
                connection.close();
            }catch(SQLException e){
                e.printStackTrace();
            }
        }
    }
  

}
//End Search Button
 
//This is a JOJO class which use to manipulate data, it's used to define data u //want to populate
public static class Department{
    public SimpleStringProperty depName;
    public SimpleStringProperty depLocation;
  
    public Department(String dep_Name, String dep_Location){
        this.depName = new SimpleStringProperty(dep_Name);
        this.depLocation = new SimpleStringProperty(dep_Location);
    }
    public String getDepName()
    {
        return depName.get();
    }
    public void setDepName(String dep_Name)
    {
        depName.set(dep_Name);;
    }
    public String getDepLocation()
    {
        return depLocation.get();
    }
    public void setLocation(String dep_Location)
    {
        depLocation.set(dep_Location);
    }
  
}
}




Link to the full code
 FXML file:
https://pastebin.com/ptVcSBAZ
Connector class:
https://pastebin.com/m641Wfvu
Controller class:
https://pastebin.com/NXnMN0Na


Populate ListView from JSON using AsyncTask via singleton class.

Populate ListView from JSON using AsyncTask via singleton class. In this tutorial we will try to make a simple app(not a real life...