我是宁静网络服务的新手..当一个帖子请求发送到“/ adduser”时,就像请求数据为{“iduser”:5,“name”:“bob”},其余客户端给出500错误和eclipse控制台提供MySqlSyntaxErrorException:'字段列表'中的未知列'bob'
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>
答案 0 :(得分:2)
MySqlSyntaxErrorException
表示SQL上存在语法错误。
"insert into users values("+iduser+","+name+")"
字符串值需要引号。 name
值需要用引号
+",'"+name+"')"
数字不需要它们。
除此之外,您甚至没有正确使用PreparedStatement
。您应该使用?
语法,并使用PreparedStatment
API替换它们。 e.g。
PreparedStatement ps=con.prepareStatement("insert into users values(?,?)");
ps.setInt(1, isuser);
ps.setString(2, name);
int result = ps.executeUpdate();
另见: