From 7d1e8bdcdae8bb14fd5ba0a204a4f60048860d81 Mon Sep 17 00:00:00 2001 From: Waseem Awashra Date: Sat, 31 Aug 2019 12:24:07 +0300 Subject: [PATCH 1/2] create valid for student class --- .../src/com/luv2code/springdemo/mvc/Student.java | 6 ++++++ .../src/com/luv2code/springdemo/mvc/StudentController.java | 7 ++++++- .../com/luv2code/springdemo/mvc/validation/CourseCode.java | 2 +- .../mvc/validation/CourseCodeValidattorConstraint.java | 1 + 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/spring-mvc-demo/src/com/luv2code/springdemo/mvc/Student.java b/spring-mvc-demo/src/com/luv2code/springdemo/mvc/Student.java index 753a659..e89a39a 100644 --- a/spring-mvc-demo/src/com/luv2code/springdemo/mvc/Student.java +++ b/spring-mvc-demo/src/com/luv2code/springdemo/mvc/Student.java @@ -1,9 +1,15 @@ package com.luv2code.springdemo.mvc; + +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Size; + public class Student { private String firstName; private String lastName; private String country; + @NotNull private String favoriteLanguage; + @Size(min = 1) private String favoriteOs[]; public Student(String firstName, String lastName) { this.firstName = firstName; diff --git a/spring-mvc-demo/src/com/luv2code/springdemo/mvc/StudentController.java b/spring-mvc-demo/src/com/luv2code/springdemo/mvc/StudentController.java index 12ff0ac..8a24467 100644 --- a/spring-mvc-demo/src/com/luv2code/springdemo/mvc/StudentController.java +++ b/spring-mvc-demo/src/com/luv2code/springdemo/mvc/StudentController.java @@ -2,9 +2,12 @@ import java.util.Map; +import javax.validation.Valid; + import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; +import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; @@ -37,7 +40,9 @@ public String showFourm(Model model) { } @RequestMapping("/processForm") - public String processForm(@ModelAttribute("student") Student theStudent) { + public String processForm(@Valid @ModelAttribute("student") Student theStudent ,BindingResult result) { + if(result.hasErrors()) + return "student-form"; return "student-confirmation"; } } diff --git a/spring-mvc-demo/src/com/luv2code/springdemo/mvc/validation/CourseCode.java b/spring-mvc-demo/src/com/luv2code/springdemo/mvc/validation/CourseCode.java index 787d0d2..4d8c323 100644 --- a/spring-mvc-demo/src/com/luv2code/springdemo/mvc/validation/CourseCode.java +++ b/spring-mvc-demo/src/com/luv2code/springdemo/mvc/validation/CourseCode.java @@ -12,7 +12,7 @@ @Target({ElementType.METHOD,ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) public @interface CourseCode { - // define default corse code + // define default course code public String value() default "LUV"; // define default error message public String message() default "must start with LUV"; diff --git a/spring-mvc-demo/src/com/luv2code/springdemo/mvc/validation/CourseCodeValidattorConstraint.java b/spring-mvc-demo/src/com/luv2code/springdemo/mvc/validation/CourseCodeValidattorConstraint.java index 1dfe32f..b32e1e7 100644 --- a/spring-mvc-demo/src/com/luv2code/springdemo/mvc/validation/CourseCodeValidattorConstraint.java +++ b/spring-mvc-demo/src/com/luv2code/springdemo/mvc/validation/CourseCodeValidattorConstraint.java @@ -14,6 +14,7 @@ public void initialize(CourseCode constraintAnnotation) { @Override public boolean isValid(String theCode, ConstraintValidatorContext theConstraintValidatorContext) { if(theCode == null) { + return true; }else { return theCode.toUpperCase().startsWith(coursePrefixes.toUpperCase()); From c7686014c2ac757ab37c429a1d86d9057045d663 Mon Sep 17 00:00:00 2001 From: Waseem Awashra Date: Sat, 28 Sep 2019 15:24:50 +0300 Subject: [PATCH 2/2] create demo class --- .../hibernate/demo/CreateStudentDemo.java | 49 ++++++++++++ .../hibernate/demo/entity/Student.java | 79 +++++++++++++++++++ .../src/com/luv2code/jdbc/TestJdbc.java | 32 ++++++++ hibernate-tutorial/src/hibernate.cfg.xml | 29 +++++++ 4 files changed, 189 insertions(+) create mode 100644 hibernate-tutorial/src/com/luv2code/hibernate/demo/CreateStudentDemo.java create mode 100644 hibernate-tutorial/src/com/luv2code/hibernate/demo/entity/Student.java create mode 100644 hibernate-tutorial/src/com/luv2code/jdbc/TestJdbc.java create mode 100644 hibernate-tutorial/src/hibernate.cfg.xml diff --git a/hibernate-tutorial/src/com/luv2code/hibernate/demo/CreateStudentDemo.java b/hibernate-tutorial/src/com/luv2code/hibernate/demo/CreateStudentDemo.java new file mode 100644 index 0000000..3be7839 --- /dev/null +++ b/hibernate-tutorial/src/com/luv2code/hibernate/demo/CreateStudentDemo.java @@ -0,0 +1,49 @@ +package com.luv2code.hibernate.demo; + +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.cfg.Configuration; + +import com.luv2code.hibernate.demo.entity.Student; + +public class CreateStudentDemo { + public static void main(String[] args) { + + for (int i = 0; i < 10; i++) { + System.out.print(sum(i) + " , "); + } + + SessionFactory factory = new Configuration() + .configure("hibernate.cfg.xml") + .addAnnotatedClass(Student.class) + .buildSessionFactory(); + // create session + Session session = factory.getCurrentSession(); + + try { + // use the session object to save java object . + // create a student object + // start a transaction + // save the student object + // commit transaction + } catch (Exception e) { + // TODO: handle exception + }finally { + factory.close(); + } + } + + public static int sum(int n) { + int a = 0,b =1,c=0; + if(n == 0) + return a; + else if(n == 1) + return b; + for (int i = 2; i <= n; i++) { + c = a+b; + a = b; + b = c; + } + return c; + } +} diff --git a/hibernate-tutorial/src/com/luv2code/hibernate/demo/entity/Student.java b/hibernate-tutorial/src/com/luv2code/hibernate/demo/entity/Student.java new file mode 100644 index 0000000..8892843 --- /dev/null +++ b/hibernate-tutorial/src/com/luv2code/hibernate/demo/entity/Student.java @@ -0,0 +1,79 @@ +package com.luv2code.hibernate.demo.entity; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name="student") +public class Student { + + @Id + @GeneratedValue(strategy=GenerationType.IDENTITY) + @Column(name="id") + private int id; + + @Column(name="first_name") + private String firstName; + + @Column(name="last_name") + private String lastName; + + @Column(name="email") + private String email; + + public Student() { + + } + + public Student(String firstName, String lastName, String email) { + this.firstName = firstName; + this.lastName = lastName; + this.email = email; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + @Override + public String toString() { + return "Student [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email + "]"; + } + +} + + + + diff --git a/hibernate-tutorial/src/com/luv2code/jdbc/TestJdbc.java b/hibernate-tutorial/src/com/luv2code/jdbc/TestJdbc.java new file mode 100644 index 0000000..586edff --- /dev/null +++ b/hibernate-tutorial/src/com/luv2code/jdbc/TestJdbc.java @@ -0,0 +1,32 @@ +package com.luv2code.jdbc; + +import java.sql.Connection; +import java.sql.DriverManager; + +public class TestJdbc { + + public static void main(String[] args) { + + String jdbcUrl = "jdbc:mysql://localhost:3307/hb_student_tracker?useSSL=false&serverTimezone=UTC"; + String user = "root"; + String pass = ""; + + try { + System.out.println("Connecting to database: " + jdbcUrl); + + Connection myConn = + DriverManager.getConnection(jdbcUrl, user, pass); + + System.out.println("Connection successful!!!"); + + } + catch (Exception exc) { + exc.printStackTrace(); + } + + } + +} + + + diff --git a/hibernate-tutorial/src/hibernate.cfg.xml b/hibernate-tutorial/src/hibernate.cfg.xml new file mode 100644 index 0000000..c363e01 --- /dev/null +++ b/hibernate-tutorial/src/hibernate.cfg.xml @@ -0,0 +1,29 @@ + + + + + + + + com.mysql.cj.jdbc.Driver + jdbc:mysql://localhost:3307/hb_student_tracker?useSSL=false&serverTimezone=UTC + root + + + + 1 + + + org.hibernate.dialect.MySQLDialect + + + true + + + thread + + + + \ No newline at end of file