1
私は安らかなWebサービスに新しいです.. {"iduser":5、 "name": "ボブ "}、残りのクライアントは、500エラーを与え、日食コンソールがMySqlSyntaxErrorExceptionいます: 'フィールドリスト' に不明な列 'ボブ' を安心なwebservices jax-rsのポストメソッドが動作しない
DbConnection.java
package com.dbproject;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DbConnection {
public static Connection getConnection(){
Connection con=null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
con=DriverManager.getConnection("jdbc:mysql://localhost:3307/thedb","root","root");
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
}
DbServices.java
package com.dbproject;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class DbServices {
Connection con=DbConnection.getConnection();
public String addUser(int iduser,String name) throws SQLException{
PreparedStatement ps=con.prepareStatement("insert into users values("+iduser+","+name+")");
int res=ps.executeUpdate();
con.close();
if(res>0){
System.out.println("Insert Query Successfull");
return "Done";
}
else{
System.out.println("Insert Query Failed");
return "Error";
}
}
}
Resources.java
package com.dbproject;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
@Path("/user")
public class Resources {
DbServices dbServices=new DbServices();
@POST
@Path("/adduser")
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
public String addUser(String s) throws SQLException{
Gson gson=new Gson();
UserModel userModel=gson.fromJson(s,UserModel.class);
String str=dbServices.addUser(userModel.getIduser(),userModel.getName());
return str;
}
}
UserModel.java
package com.dbproject;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class UserModel {
private int iduser;
private String name;
public int getIduser() {
return iduser;
}
public String getName() {
return name;
}
public void setIduser(int iduser) {
this.iduser = iduser;
}
public void setName(String name) {
this.name = name;
}
}
の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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>dbproject</display-name>
<servlet>
<servlet-name>Jersey Web Services</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.dbproject</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Services</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
</web-app>
おかげでたくさんのpeeskillet。 :) –