I am trying to create a rest webservice to add an element to data base. This is my connection class
@Path(value="/user")
public class classeConnection {
Connection cn=null;
Statement st=null;
public classeConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e ){};
}
public void connecter ()throws SQLException {
cn= DriverManager.getConnection("jdbc:mysql://localhost:3306/codezone4", "root","");
st=cn.createStatement();
System.out.print("Ping!!");
}
public ResultSet execSelect(String req)throws SQLException {
return(st.executeQuery(req));
}
@GET
@Path (value="/add/{nom}/{prenom}/{email}/{login}/{psw}")
@Produces(MediaType.APPLICATION_JSON)
public String execMAJ(@PathParam(value="nom")String nom,
@PathParam(value="prenom")String prenom,
@PathParam(value="email")String email ,@PathParam(value="login")String login,
@PathParam(value="psw")String psw) throws SQLException {
String req;
req="INSERT INTO user (nom,prenom,email,login,psw) values(
'"+nom+"','"+prenom+"','"+email+"','"+login+"','"+psw+"')";
int r=0;
System.out.println(st);
r=st.executeUpdate(req);
System.out.println(st);
return "succee d'ajout";
}
public void fermeture()throws SQLException {
st.close(); // line 57
cn.close();
}
public static void main(String[] args) throws SQLException {
classeConnection c=new classeConnection();
c.connecter();
}
}
And this is my web.xml
<?xml version="1.0" encoding="ASCII"?>
<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_3_0.xsd"
version="3.0">
<display-name>rest</display-name>
<servlet>
<description>JAX-RS Tools Generated - Do not modify</description>
<servlet-name>JAX-RS Servlet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.server.provider.packages</param-name>
<param-value>com.example</param-value>
</init-param><load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JAX-RS Servlet</servlet-name>
<url-pattern>/myrest/*</url-pattern>
</servlet-mapping>
</web-app>
When I run my webservice using the url : /AddToDataBase/myrest/user/add/test/test/[email protected]/test/test, I get this error :
java.lang.NullPointerException
com.example.classeConnection.execMAJ(classeConnection.java:57)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)
com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302)
com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1542)
com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1473)
com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419)
com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409)
com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409)
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:540)
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:715)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Are there any suggestions to this issue? Please help!
classeConnection
?execMAJ
method. But you pointed out line 57 infermeture()
method. Kindly confirm.