我是Java新手。我現在使用HashMap來存儲MySQL數據庫中的數據,我將使用JSon POST請求來獲取用戶的輸入並在HashMap中搜索相關數據並從HashMap中檢索。我需要來自用戶的三個輸入,但在HashMap中只能輸入1個鍵。所以,我試圖將鍵作爲對象輸入,但它不起作用。以下是我的代碼,用於存儲來自MySQL數據庫的數據。將3個屬性輸入到HashMap密鑰
public class AppDataService {
HashMap<AppDataRequest, AppData> appdatas = new HashMap<AppDataRequest, AppData>();
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://****:3306/****_demo";
static final String USER = "****";
static final String PASS = "****";
public AppDataService(){
Connection conn = null;
Statement stat = null;
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stat = conn.createStatement();
String sql = "SELECT * FROM testdata";
ResultSet resu = stat.executeQuery(sql);
while(resu.next()){
int id = resu.getInt("app_id");
String email = resu.getString("email");
String password = resu.getString("password");
String status = resu.getString("status");
String message = resu.getString("message");
String token = resu.getString("token");
appdatas.put(new AppDataRequest(id, email, password), new AppData(status, message, token));
}
resu.close();
stat.close();
conn.close();
}
catch(SQLException se){
se.printStackTrace();
}
catch(Exception e){
e.printStackTrace();
}
finally{
try{
if(stat!=null){
stat.close();
}
}
catch (SQLException se2){
}
try{
if(conn!=null){
conn.close();
}
}
catch(SQLException se3){
se3.printStackTrace();
}
}
}
public List<AppData> getAllAppData(){
return new ArrayList<AppData>(appdatas.values());
}
public AppData getAppData(int id){
return appdatas.get(id);
}
public AppData getSAppData(int id, String email, String password){
return appdatas.get(new AppDataRequest (id, email, password));
}
}
我的JSON POST代碼
@Path("/appdata")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
AppDataService ads = new AppDataService();
@POST
@Path("/appdatas")
public AppData getSAppData(AppDataRequest adr){
return ads.getSAppData(adr.getId(), adr.getEmail(),adr.getPassword());
}
AppData的類
public class AppData {
public String status;
public String message;
public String token;
public AppData(){
}
public AppData(String status, String message, String token) {
this.status = status;
this.message = message;
this.token = token;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
}
AppDataRequest類
public class AppDataRequest {
public int id;
public String email;
public String password;
public AppDataRequest(){
}
public AppDataRequest(int id, String email, String password) {
this.id = id;
this.email = email;
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
嗨。 @YCF_L。感謝您的回覆。我將嘗試覆蓋HashCode和equals的代碼。我們必須有83號或任何號碼才能做到嗎?我們嘗試使用ID,電子郵件和密碼,因爲電子郵件和密碼也是唯一的,因爲它們只會在我們的桌面上有一個條目。 –
嗨。 @YCF_L。我已經對83進行了一些研究。這是奇怪的素數? –