【发布时间】:2016-12-07 18:45:39
【问题描述】:
//你好, // 我的RESTful POST操作如下
import java.sql.SQLException;
import java.util.ArrayList;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
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 org.springframework.transaction.annotation.Transactional;
@Path("/dbuser")
public class DBUserRestServices extends DbConnectionDAO{
@POST
@Path("/postuser1") //Problem here???
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.APPLICATION_FORM_URLENCODED})
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.APPLICATION_FORM_URLENCODED})
@Transactional
public Response userPOSTOperation( @PathParam("userid") int userid,@PathParam("username") String username,
@PathParam("password") String password) throws SQLException{
UserObject userobj = new UserObject(userid, username, password);
username= userobj.getUsername();
password= userobj.getPassword();
System.out.println("UserName " + username + "\t\t"+ "Password :" + password);
dbConnection = new DbConnectionDAO();
String sqlQuery = "INSERT INTO user (username, password) values ('" +"username" +"',"+ "'password" +"')";
System.out.println("Query executed : " + sqlQuery );
try{
connection = DbConnectionDAO.setDBConnection();
statement = connection.createStatement();
numRowsChanged = statement.executeUpdate(sqlQuery);
System.out.println("numRowsChanged : " + numRowsChanged );
if (numRowsChanged<=0)
{
System.out.println("Oops!! The insertion operation failed");
}
else{
System.out.println("The POST operation with username = " + username + ", password "+ password+" has been completed");
}
} catch (Exception e)
{
System.out.println(e.getMessage());
}
return Response.ok().build();
}
}
//
// The DbConnectionDAO.java file is
public class DbConnectionDAO {
public static Connection connection;
public static Statement statement;
public static PreparedStatement pst;
public static ResultSet rs = null;
private static String sqlQuery = null;
static int numRowsChanged = 0;
public static Connection setDBConnection() throws Exception
{
try
{
String connectionURL = "jdbc:mysql://localhost:3306/test";
// Connection connection = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "root", "password");
if (connection != null) {
// System.out.println("Connected to the Database...");
}
return connection;
}
catch (SQLException e)
{
throw e;
}
catch (Exception e)
{
throw e;
}
}
// The UserObject.java file is
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "userObject")
public class UserObject {
private int userid;
private String username;
private String password;
public UserObject(){}
public UserObject(int userid, String username, String password){
this.userid = userid;
this.username = username;
this.password = password;
}
public String getPassword() {
return password;
}
public int getUserid() {
return userid;
}
public String getUsername() {
return username;
}
@XmlElement
public void setPassword(String password) {
this.password = password;
}
@XmlElement
public void setUserid(int userid) {
this.userid = userid;
}
@XmlElement
public void setUsername(String username) {
this.username = username;
}
}
当我在 Postman 中发出以下命令时:http://localhost:8080/iAdjuster/restapi/dbuser/postuser1/?username=John&password=Michael
它显示以下控制台输出:
用户名 null 密码 :null 执行的查询:INSERT INTO 用户(用户名,密码)值('用户名','密码') numRowsChanged : 1 username = null, password null 的 POST 操作已经完成
但是,MySQL 数据库显示成功的 INSERtion,用户名和密码字段中的值“用户名”和“密码”具有自动递增的用户 ID。
谁能帮我解决我的问题。
提前致谢!!
【问题讨论】:
标签: rest post jersey-2.0 mysql-error-1064