JerseyDataBaseProject:
package jerseyDataBaseProject;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
//Please refer jersey-documentation-1.1.5-ea-SNAPSHOT-user-guide.pdf to attached project
//http://blog.iparissa.com/google-app-engine-jax-rs-jersey/
//The Java class will be hosted at the URI path "/helloworld"
@Path("/dbOperation/{methodname}/{id}/{name}")
public class JerseyDataBaseProject {
String dbUrl = "jdbc:mysql://localhost/siddhutest_db";
String dbClass = "com.mysql.jdbc.Driver";
/*
// The Java method will process HTTP GET requests
@GET
// The Java method will produce content identified by the MIME Media
// type "text/plain"
@Produces("text/plain")
public String getClichedMessage(@PathParam("username") String userName) {
// Return some cliched textual content
return " Hello, "+userName +" +Welcome to the world of JAX-RS! PLAIN";
}
@GET
@Produces(MediaType.TEXT_XML)
public String getClichedMessageXML(@PathParam("username") String userName) {
// Return some cliched textual content
return " Hello, "+userName +" +Welcome to the world of JAX-RS! XML ";
}
@GET
@Produces(MediaType.TEXT_HTML)
public String getClichedMessageHTML(@PathParam("username") String userName) {
// Return some cliched textual content
return " Hello, "+userName +" +Welcome to the world of JAX-RS! HTML";
}
*/
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getDbOperate(@PathParam("methodname") String MethodName,@PathParam("id") String ID,@PathParam("name") String Name) {
// Return some cliched textual content
int i = 0;
System.out.println("Inside TEXT_PLAIN");
if(!MethodName.equals("")&&MethodName.equals("insert"))
{
try {
Class.forName(dbClass).newInstance();
Connection con = DriverManager.getConnection(dbUrl,"root","");
Statement stmt = con.createStatement();
String query = "insert into test_table (id, name) values ("+ID+",'"+Name+"')";
i = stmt.executeUpdate(query);
con.close();
} //end try
catch(Exception e) {
e.printStackTrace();
}
}
if(!MethodName.equals("")&&MethodName.equals("update"))
{
try {
Class.forName(dbClass).newInstance();
Connection con = DriverManager.getConnection(dbUrl,"root","");
Statement stmt = con.createStatement();
String query = "update test_table set name = '"+Name+"' where id ="+ID;
i = stmt.executeUpdate(query);
con.close();
} //end try
catch(Exception e) {
e.printStackTrace();
}
}
if(!MethodName.equals("")&&MethodName.equals("delete"))
{
try {
Class.forName(dbClass).newInstance();
Connection con = DriverManager.getConnection(dbUrl,"root","");
Statement stmt = con.createStatement();
String query = "delete from test_table where id ="+ID;
i = stmt.executeUpdate(query);
con.close();
//return "success";
} //end try
catch(Exception e) {
e.printStackTrace();
}
}
return "Value that has been updated in the database"+i;
}
@GET
@Produces(MediaType.TEXT_XML)
public String getDbOperateXML(@PathParam("methodname") String MethodName,@PathParam("id") String ID,@PathParam("name") String Name) {
// Return some cliched textual content
int i = 0;
System.out.println("Inside TEXT_XML");
if(!MethodName.equals("")&&MethodName.equals("insert"))
{
try {
Class.forName(dbClass).newInstance();
Connection con = DriverManager.getConnection(dbUrl,"root","");
Statement stmt = con.createStatement();
String query = "insert into test_table (id, name) values ("+ID+",'"+Name+"')";
i = stmt.executeUpdate(query);
con.close();
} //end try
catch(Exception e) {
e.printStackTrace();
}
}
if(!MethodName.equals("")&&MethodName.equals("update"))
{
try {
Class.forName(dbClass).newInstance();
Connection con = DriverManager.getConnection(dbUrl,"root","");
Statement stmt = con.createStatement();
String query = "update test_table set name = '"+Name+"' where id ="+ID;
i = stmt.executeUpdate(query);
con.close();
} //end try
catch(Exception e) {
e.printStackTrace();
}
}
if(!MethodName.equals("")&&MethodName.equals("delete"))
{
try {
Class.forName(dbClass).newInstance();
Connection con = DriverManager.getConnection(dbUrl,"root","");
Statement stmt = con.createStatement();
String query = "delete from test_table where id ="+ID;
i = stmt.executeUpdate(query);
con.close();
//return "success";
} //end try
catch(Exception e) {
e.printStackTrace();
}
}
return "Value that has been updated in the database"+i;
}
@GET
@Produces(MediaType.TEXT_HTML)
public String getDbOperateHTML(@PathParam("methodname") String MethodName,@PathParam("id") String ID,@PathParam("name") String Name) {
// Return some cliched textual content
int i = 0;
System.out.println("Inside TEXT_HTML");
if(!MethodName.equals("")&&MethodName.equals("insert"))
{
try {
Class.forName(dbClass).newInstance();
Connection con = DriverManager.getConnection(dbUrl,"root","");
Statement stmt = con.createStatement();
String query = "insert into test_table (id, name) values ("+ID+",'"+Name+"')";
i = stmt.executeUpdate(query);
con.close();
} //end try
catch(Exception e) {
e.printStackTrace();
}
}
if(!MethodName.equals("")&&MethodName.equals("update"))
{
try {
Class.forName(dbClass).newInstance();
Connection con = DriverManager.getConnection(dbUrl,"root","");
Statement stmt = con.createStatement();
String query = "update test_table set name = '"+Name+"' where id ="+ID;
i = stmt.executeUpdate(query);
con.close();
} //end try
catch(Exception e) {
e.printStackTrace();
}
}
if(!MethodName.equals("")&&MethodName.equals("delete"))
{
try {
Class.forName(dbClass).newInstance();
Connection con = DriverManager.getConnection(dbUrl,"root","");
Statement stmt = con.createStatement();
String query = "delete from test_table where id ="+ID;
i = stmt.executeUpdate(query);
con.close();
//return "success";
} //end try
catch(Exception e) {
e.printStackTrace();
}
}
return "Value that has been updated in the database"+i;
}
}
Index.html:
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Hello App Enginetitle>
head>
<body>
<h1>Hello App Engine!h1>
<table>
<tr>
<td colspan="2" style="font-weight:bold;">Available Servlets:td>
tr>
<tr>
<td><a href="resources/dbOperation/insert/20/Twenty">Additiona>td>
<td><a href="resources/dbOperation/delete/20/Twenty">Deletiona>td>
<td><a href="resources/dbOperation/update/20/Twenty Changed">Updatea>td>
tr>
table>
body>
html>
Web.xml:
xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>Jersey Web Applicationservlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainerservlet-class>
<init-param>
<param-name>com.sun.jersey.api.core.packagesparam-name>
<param-value>com.dclonline.jerseytestparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>Jersey Web Applicationservlet-name>
<url-pattern>/resources/*url-pattern>
servlet-mapping>
<welcome-file-list>
<welcome-file>index.htmlwelcome-file>
welcome-file-list>
<session-config>
<session-timeout>30session-timeout>
session-config>
No comments:
Post a Comment