I am a programmer but I hate grammar. Here you will find what I did, doing , will do ... Scattered pieces of knowledge that I have deleted from my mind to the trash bin through out my boring daily coding life. I will also report some of my failures in life so dear to me not success, because I have always learnt much only when I fail.
Sunday, April 22, 2018
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
Get the sign sub
Converting to decimal value sub
This is just a trick, hope it paved your way out.
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.
Sunday, April 1, 2018
Saturday, March 31, 2018
Tuesday, March 13, 2018
Saturday, March 3, 2018
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
Insertion sub
This is just a trick, hope it paved your way out.
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.
Sunday, February 25, 2018
Monday, February 19, 2018
Sunday, February 18, 2018
Saturday, February 17, 2018
Sunday, February 11, 2018
Saturday, February 10, 2018
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.
Tuesday, February 6, 2018
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
HelloMySql2Application
Student
StudentDAO
StudentDAOImplementation
StudentService
StudentServiceImplementation
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
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()));
}
}
Sunday, February 4, 2018
Saturday, February 3, 2018
Saturday, January 27, 2018
Thursday, January 25, 2018
Subscribe to:
Comments (Atom)