Jersey Hk2 Layers Basic Example Using JDBC - Part-1: Mysql DB Table

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

-by RAGHU SIR [NARESH IT, HYDERABAD, P: 040-2374 6666/ 9000994007 /08]

Jersey HK2 Layers


Basic Example using JDBC -PART-1
MySQL DB Table
CREATE TABLE STUDENT (
SID INT(11) ,
SNAME VARCHAR(25) ,
SFEE DOUBLE ,
COURSE VARCHAR(25) ,
DISCOUNT DOUBLE
)

Db Connection
package in.nit.conn;
import java.sql.Connection;
import java.sql.DriverManager;
public class DbContext {

private static Connection con=null;

static {
try {
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/nit", "root", "root");
} catch (Exception e) {
e.printStackTrace();
}
}

public static Connection getConn() {


return con;
}

1|Page
-by RAGHU SIR [NARESH IT, HYDERABAD, P: 040-2374 6666/ 9000994007 /08]

1. Model class
package in.nit.model;

public class Student {

private Integer sid;


private String sname;
private Double sfee;
private String course;
private Double discount;

public Student() {
super();
}
public Student(Integer sid, String sname, Double sfee, String course,
Double discount) {
super();
this.sid = sid;
this.sname = sname;
this.sfee = sfee;
this.course = course;
this.discount = discount;
}
public Integer getSid() {
return sid;
}
public void setSid(Integer sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}

2|Page
-by RAGHU SIR [NARESH IT, HYDERABAD, P: 040-2374 6666/ 9000994007 /08]

public void setSname(String sname) {


this.sname = sname;
}
public Double getSfee() {
return sfee;
}
public void setSfee(Double sfee) {
this.sfee = sfee;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
public Double getDiscount() {
return discount;
}
public void setDiscount(Double discount) {
this.discount = discount;
}
@Override
public String toString() {
return "Student [sid=" + sid + ", sname=" + sname + ", sfee=" +
sfee + ", course=" + course + ", discount="
+ discount + "]";
}

3|Page
-by RAGHU SIR [NARESH IT, HYDERABAD, P: 040-2374 6666/ 9000994007 /08]

2. Dao
package in.nit.dao;

import in.nit.model.Student;

public interface IStudentDao {

public int saveStudent(Student s);


}

3. DaoImpl
package in.nit.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import in.nit.conn.DbContext;
import in.nit.dao.IStudentDao;
import in.nit.model.Student;

public class StudentDaoImpl


implements IStudentDao
{

public int saveStudent(Student s) {


int i=0;
try {
String SQL="INSERT INTO STUDENT VALUES(?,?,?,?,?)";
Connection con=DbContext.getConn();
PreparedStatement pstmt=con.prepareStatement(SQL);

pstmt.setInt(1, s.getSid());

4|Page
-by RAGHU SIR [NARESH IT, HYDERABAD, P: 040-2374 6666/ 9000994007 /08]

pstmt.setString(2, s.getSname());
pstmt.setDouble(3, s.getSfee());
pstmt.setString(4, s.getCourse());
pstmt.setDouble(5, s.getDiscount());

i=pstmt.executeUpdate();

} catch (SQLException e) {
e.printStackTrace();
}

return i;
}
}

4. IService
package in.nit.service;

import in.nit.model.Student;

public interface IStudentService {

public int saveStudent(Student s);


}

5. ServiceImpl
package in.nit.service.impl;

import javax.inject.Inject;

import in.nit.dao.IStudentDao;
import in.nit.model.Student;

5|Page
-by RAGHU SIR [NARESH IT, HYDERABAD, P: 040-2374 6666/ 9000994007 /08]

import in.nit.service.IStudentService;

public class StudentServiceImpl


implements IStudentService
{
@Inject
private IStudentDao dao;

public int saveStudent(Student s) {


double sfee=s.getSfee();
double discount=sfee*10/100.0;
s.setDiscount(discount);
return dao.saveStudent(s);
}

6. RestController
package in.nit.controller;

import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import in.nit.model.Student;
import in.nit.service.IStudentService;

@Path("/student")
public class StudentRestController {

6|Page
-by RAGHU SIR [NARESH IT, HYDERABAD, P: 040-2374 6666/ 9000994007 /08]

@Inject
private IStudentService service;

@POST
@Consumes("application/json")
public Response save(Student s) {
Response resp=null;
try {
int count=service.saveStudent(s);
if(count==1)
resp=Response.ok("SUCCESSFULLY CREATED").build();
else
resp=Response.ok()
.entity("PROBLEM FOUND")
.status(Status.BAD_REQUEST)
.build();
} catch (Exception e) {
resp=Response.ok()
.entity(e.getMessage())
.status(Status.INTERNAL_SERVER_ERROR)
.build();
e.printStackTrace();
}
return resp;
}

7|Page
-by RAGHU SIR [NARESH IT, HYDERABAD, P: 040-2374 6666/ 9000994007 /08]

pom.xml

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>13</maven.compiler.source>
<maven.compiler.target>13</maven.compiler.target>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>

<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.29</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>2.29</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.29</version>
</dependency>

</dependencies>
FB: https://www.facebook.com/groups/thejavatemple/
email : [email protected]

8|Page

You might also like