Jersey Hk2 Layers Basic Example Using JDBC - Part-1: Mysql DB Table
Jersey Hk2 Layers Basic Example Using JDBC - Part-1: Mysql DB Table
Jersey Hk2 Layers Basic Example Using JDBC - Part-1: Mysql DB Table
Db Connection
package in.nit.conn;
import java.sql.Connection;
import java.sql.DriverManager;
public class DbContext {
static {
try {
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/nit", "root", "root");
} catch (Exception e) {
e.printStackTrace();
}
}
1|Page
-by RAGHU SIR [NARESH IT, HYDERABAD, P: 040-2374 6666/ 9000994007 /08]
1. Model class
package in.nit.model;
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]
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;
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;
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;
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;
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