Sunday, April 22, 2018

Dev Tips#23 Convert twos complement to decimal value Algorithm and Code ...

Converting a 16 bit twos complements value to decimal value : algorithm and implementation

Converting a 16 bits twos complement value to a decimal value equivalent. In this tutorial a simple algorithm and implementation are shared.

Main sub

Sub Main
    
 Dim result As Short = 0
 Dim value = 50
 result = GetDecimalValue(value)
 Console.WriteLine("Decimal value of " & value & " is "& result)
End Sub

  


Get the sign sub


Function GetSign16Bit( value)
'This function returns -1 if the 16 bits value is negative and 1 if it is zero or positive
   Dim sign = 0
   value = value/ 256    'Get the left most byte
   value = value / 16    'Get the leftmost 4-bits
   value = value / 4     'Get the leftmost 2-bits
   value = value / 2     'Get the  leftmost 1-bit
    If  value  >=1 Then
         sign = -1
    Else
          sign= 1
    End If
   Return sign
End Function

  


Converting to decimal value sub


Function GetDecimalValue(value)
'This function takes the twos complement of a 16 bits value and  return the equivalent decimal value
' We know that, 2^16 =65536
    Dim ValueOneComplement, Result 
 If  GetSign16Bit(value) = 1 Then  ' Positive number
         Result = value
    Else                              'Negative number
            ValueOneComplement = Not  value  ' get the one's complement of the value
            Result =  (65536  + ValueOneComplement ) +1 
            Result = - Result  'Make the value negative by prefixing it with minus
    End If
 Return Result
End Function

  

This is just a trick, hope it paved your way out. 

Saturday, March 3, 2018

Dev Tips#22 LINQPAD Insert data into MySQL using VB

LINQPAD - How to Insert data into MySQL using VB

This snippet connect to MySQL in LINQPAD using Visual Basic. Make sure you download the MySql.Data.DLL, unblock the zip file then extract it and add that reference in LINQPAD by pressing F4 as shown in the video here

Main sub

Sub Main
 Dim query As String
 Dim stid As Integer
 Dim name, programme As String
 stid = 13
 name = "Nemo"
 programme = "Search of Nemo"
 query ="insert into Student(Stid,Name,Programme) values (" & stid & ",'" & name & "','" &  programme & "')"
 insertInDB (query)
End Sub

  

Insertion sub



Sub InsertInDB (query As String)

      'Declaring variables
     Dim conn As New MySqlConnection()
  Dim sqlCmd As MySqlCommand 
  'Connection string to MySQL
        conn.ConnectionString = "Persist Security Info=False;database=springboot;server=localhost;Connect Timeout=30;user id=springboot; pwd=springboot"
        Try
      'Connecting and executing the query
            conn.Open()
   sqlCmd = New MySqlCommand( query, conn)
   sqlCmd.ExecuteNonQuery()
   'Closing the query
   conn.Close()
        Catch myerror As MySqlException
  'Throwing exceptions
            Console.WriteLine("Error connecting to database!")
            Exit Sub
        End Try
        Console.WriteLine("connected to database!")
End Sub
  

This is just a trick, hope it paved your way out. 

Dev Tips#21 LINQPAD connect to MySql Oracle SQLite

Sunday, February 25, 2018

Structured Text#19 REFACTORING

Structured Text#18 UNION

Structured Text#17 ALIAS

Structured Text#16 STRUCTURE

Structured Text#15 ENUMERATION

Structured Text#14 CONSTANT

Structured Text#13 CONTINUE in loop

Structured Text#12 REPEAT Loop

Structured Text#11 WHILE Loop

Structured Text#10 FOR Loop

Structured Text#09 Arrays

Saturday, February 17, 2018

CPP Essential#40 Array with characters and string

CPP Essential#39 Array as parameter to a function

CPP Essential#38 Array Multidimension

CPP Essential#37 Array more

CPP Essential#36 Array

CPP Essential#35 Nested loops

CPP Essential#34 Scope and visibility

CPP Essential#33 Function Overloading More

CPP Essential#32 Function Overloading

CPP Essential#31 Function example with parameters and return

CPP Essential#30 Function example with parameters no return

CPP Essential#29 Function example with no parameters no return

CPP Essential#28 Function Syntax

CPP Essential#27 Selection statement Switch

CPP Essential#26 Jump statement Continue

CPP Essential#25 Jump statement Break

CPP Essential#24 Iteration statement Do While Loop

CPP Essential#23 Iteration statement While Loop

CPP Essential#22 Iteration statement For Loop More

CPP Essential#21 Iteration statement For Loop

CPP Essential#20 Selection statement IF ELSEIF ELSE

CPP Essential#19 Selection statement IF ELSE

CPP Essential#18 Selection statement IF

CPP Essential#17 Statements and Flow Control

CPP Essential#16 Flash back 1

CPP Essential#15 Basic calculator

CPP Essential#14 Namespace

CPP Essential#13 Basic input and output

CPP Essential#12 Operators and operands

CPP Essential#11 Constants

CPP Essential#10 Compound type String

CPP Essential#9 Variables

CPP Essential#9 Variables

CPP Essential#8 Identifiers

CPP Essential#07 Primitive data types

CPP Essential#06 Comments

CPP Essential#05 Basic CPP program structure

CPP Essential#04 Create your first CPP program in Code Blocks

CPP Essential#03 some examples of IDE and Compilers for CPP

CPP Essential#02 How to wrtie a program

CPP Essential#01 Introduction

Saturday, February 10, 2018

Hardware Programming # 04 Codesys Installation

9-Spring Boot JPA Mysql Connection SOLVED

How to connect Spring Boot to Mysql using JPA SOLVED


application.properties

  
spring.datasource.url=jdbc:mysql://localhost:3306/springboot?autoReconnect=true
spring.datasource.username=springboot
spring.datasource.password=springboot
spring.jpa.generate-ddl=true

  
  


Student Entity in the Model


  package com.example.demo.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;



@Entity
@Table(name="student")
public class Student implements Serializable{
 
 private static final long serialVersionUID = -6899110682583120414L;

 @Id
 
    @Column(name="Stid")
 private int Stid;
 
    @Column(name="Name")
 private String Name;
    
    @Column(name="Programme")
 private String Programme;
 
 public Student() {
  //super();
 }

 
 public Student(int stid, String name, String programme) {
  super();
  Stid = stid;
  Name = name;
  Programme = programme;
 }

 

 @Override
 public String toString() {
  return "Student [Stid=" + Stid + ", Name=" + Name + ", Programme=" + Programme + "]";
 }
 
    
}

  


Student Repository


package com.example.demo.repository;

import org.springframework.data.repository.CrudRepository;

import com.example.demo.model.Student;

public interface StudentRepository extends CrudRepository {
   
}
  


Student Controller


   package com.example.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.model.Student;
import com.example.demo.repository.StudentRepository;

@RestController
public class StudentController {

 @Autowired
 StudentRepository repository;
 
 @RequestMapping("/findall")
  @ResponseBody
 public String findall(){
  String result = "";
          for(Student student:repository.findAll()){
        result += " --- " +student.toString();
       }
 return result  ;
 }
 
 @RequestMapping("/findbystid")
  @ResponseBody
 public String findByStid(@RequestParam("stid") int stid){
  String result="";
           result=repository.findOne(stid).toString();
 return result  ;
 }
 
 @RequestMapping("/register")
 String register(Student student) {
  repository.save(new Student(10,"JPA","JPA Tut"));
  return "Student Registered";
 }

 
 
}
  


Main Class


package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class HelloJpaMySql1Application {

 public static void main(String[] args) {
  SpringApplication.run(HelloJpaMySql1Application.class, args);
 }
}

  


This is just a trick, hope it paved your way out. 

Thursday, February 8, 2018

How Write the name of all contents of a folder to a file in Powershell



Write the name of all contents of a folder to a file using powershell



$files = Get-ChildItem -Path 'D:\doc\' -Recurse |?{$_.PSIsContainer -eq $false -and $_.Extension -ne '.txt'  }

"Total : "+$files.Count+" files" | Out-File -Append 'D:\doc\MyFiles.txt'
ForEach($file in $files)
{
 $file.Name | Out-File -Append 'D:\doc\MyFiles.txt'  
}

  

This is just a trick, hope it paved your way out. 

powershell#07 Write the name of all contents of a folder to a file

powershell#06 Call ps1 scripts from a ps1 scripts

powershell#05 Add or Substract Times to Date

powershell#04 Add or Substract Years to Date

powershell#02 Add or Substract days to Date

powershell#01 Date Comparison in Powershell

Tuesday, February 6, 2018

Dev Tips#20 Quickly determine the number of Cores in your PC

Using Spring Boot and JDBC for Mysql Connection SOLVED

In this tutorial I share with you some snippets explained in this video tutorial


application.properties

  

server.port=9001
spring.datasource.url=jdbc:mysql://localhost:3306/springboot?autoReconnect=true
spring.datasource.username=springboot
spring.datasource.password=springboot
spring.datasource.platform=mysql

  
  


HelloMySql2Application



    package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class HelloMySql2Application {
 
 @Autowired
    StudentService  studentService;
 
 public static void main(String[] args) {
  //SpringApplication.run(HelloMySqlApplication.class, args);
  ApplicationContext context = SpringApplication.run(HelloMySql2Application.class, args);
  StudentService  studentService = context.getBean(StudentService.class);

   Student student = new Student();
   student.setStid(4);
   student.setName("I got it");
   student.setProgramme("Spring Booter");
   
   //Insert a student
   studentService.insertStudent(student);
   
   //Get student Stid = 1
   studentService.getStudentByStid(1);
   
   //Get all Students
   studentService.getAllStudents();
  
 }
}
  


Student


package com.example.demo;

public class Student {
   int Stid;
   String Name;
   String Programme;
   
public int getStid() {
 return Stid;
}
public void setStid(int stid) {
 Stid = stid;
}
public String getName() {
 return Name;
}
public void setName(String name) {
 Name = name;
}
public String getProgramme() {
 return Programme;
}
public void setProgramme(String programme) {
 Programme = programme;
}

@Override
public String toString() {
 return "Student [Stid=" + Stid + ", Name=" + Name + ", Programme=" + Programme + "]";
}
   
   
}

  


StudentDAO



 package com.example.demo;

import java.util.List;

public interface StudentDAO {

  void insertStudent(Student std);
  Student getStudentByStid(int stid);
  List getAllStudents();
}


  


StudentDAOImplementation



package com.example.demo;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;

@Repository
public class StudentDAOImplementation extends JdbcDaoSupport implements StudentDAO {

 @Autowired 
    DataSource dataSource;
 
 @PostConstruct
 private void initialize(){
  setDataSource(dataSource);
 }
 
 
 @Override
 public void insertStudent(Student std) {
  String query = "INSERT INTO student (Stid, Name, Programme) VALUES (?, ?,?)" ;
  getJdbcTemplate().update(query, new Object[]{ std.getStid(), std.getName(), std.getProgramme()});
    
 }

 @Override
 public Student getStudentByStid(int stid) {
  String query = "SELECT * FROM student WHERE Stid = ?";
  return (Student)getJdbcTemplate().queryForObject(query, new Object[]{stid}, new RowMapper(){
   @Override
   public Student mapRow(ResultSet resultSet, int rwNumber) throws SQLException {
    Student std = new Student();
    std.setStid(resultSet.getInt("Stid"));
    std.setName(resultSet.getString("Name"));
    std.setProgramme(resultSet.getString("Programme"));
    return std;
   }
  });
 }

 @Override
 public List getAllStudents() {
  String query = "SELECT * FROM student";
  List> studentRow = getJdbcTemplate().queryForList(query);
  
  List students = new ArrayList();
  for(Map row:studentRow){
   Student student = new Student();
   student.setStid((int)row.get("Stid"));
   student.setName((String)row.get("Name"));
   student.setProgramme((String)row.get("Programme"));
   students.add(student);
  }
  
  return students;
 }

 
}
  


StudentService



package com.example.demo;

public interface StudentService {
 void insertStudent(Student emp);
 void getStudentByStid(int stid);
 void getAllStudents();
}

  


StudentServiceImplementation



 package com.example.demo;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StudentServiceImplementation implements StudentService{

 @Autowired
    StudentDAO studentDAO;
 @Override
 public void insertStudent(Student std) {
       studentDAO.insertStudent(std);  
 }

 @Override
 public void getStudentByStid(int stid) {
          Student student = studentDAO.getStudentByStid(stid);  
       System.out.println(student);
 }

 @Override
 public void getAllStudents() {
      List students = studentDAO.getAllStudents();
       students.forEach(student -> System.out.println(student.toString()));
 }

}

  

8 - Spring Boot JDBC Mysql Connection SOLVED

DBA#02 Check SQL Server Edition Version Properties