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, 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); ListgetAllStudents(); }
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() { Liststudents = studentDAO.getAllStudents(); students.forEach(student -> System.out.println(student.toString())); } }
Sunday, February 4, 2018
Saturday, February 3, 2018
Subscribe to:
Posts (Atom)