* JDBC class is singleton and login to db only on startup

* db error alert on login
This commit is contained in:
lukas-heiligenbrunner
2019-12-01 10:58:43 +01:00
parent 07a4035195
commit c9e3414b29
14 changed files with 163 additions and 55 deletions

View File

@ -5,6 +5,7 @@ import com.wasteinformationserver.basicutils.Log;
import com.wasteinformationserver.db.JDCB;
import com.wasteinformationserver.website.basicrequest.PostRequest;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
@ -13,14 +14,21 @@ public class DataRequest extends PostRequest {
@Override
public String request(HashMap<String, String> params) {
String result = "";
JDCB jdcb;
try {
jdcb = JDCB.getInstance();
} catch (IOException e) {
Log.error("no connection to db");
return "{\"query\" : \"nodbconn\"}";
}
switch (params.get("action")) {
case "newCity":
StringBuilder sb = new StringBuilder();
sb.append("{");
Log.debug(params.toString());
// check if wastezone and wasteregion already exists
JDCB jdcb = new JDCB("users", "kOpaIJUjkgb9ur6S", "wasteinformation");
Log.debug(params.get("cityname") + params.get("wastetype") + params.get("wastezone"));
ResultSet set = jdcb.executeQuery("select * from `cities` where `name`='" + params.get("cityname") + "' AND `wastetype`='" + params.get("wastetype") + "' AND `zone`='" + params.get("wastezone") + "'");
int size = 0;
@ -38,31 +46,33 @@ public class DataRequest extends PostRequest {
int status = jdcb.executeUpdate("INSERT INTO `cities`(`userid`, `name`, `wastetype`, `zone`) VALUES ('0','" + params.get("cityname") + "','" + params.get("wastetype") + "','" + params.get("wastezone") + "');");
System.out.println(status);
if (status == 1) {
result = "{\"status\" : \"inserted\"}";
sb.append("\"status\" : \"inserted\"}");
} else {
result = "{\"status\" : \"inserterror\"}";
sb.append("\"status\" : \"inserterror\"");
}
} else if (size > 1) {
Log.warning("more than one entry in db!!!");
result = "{\"status\" : \"exists\"}";
result = "\"status\" : \"exists\"";
} else {
//already exists
System.out.println("already exists");
result = "{\"status\" : \"exists\"}";
result = "\"status\" : \"exists\"";
}
sb.append(",\"query\":\"ok\"");
sb.append("}");
Log.debug(result);
break;
case "getAllCities":
// TODO: 15.11.19 database call to get all data and store it as json.
JDCB jdcbc = new JDCB("users", "kOpaIJUjkgb9ur6S", "wasteinformation");
Gson gson = new Gson();
StringBuilder builder = new StringBuilder();
ResultSet sett = jdcbc.executeQuery("select * from cities");
ResultSet sett = jdcb.executeQuery("select * from cities");
Log.debug(sett.toString());
builder.append("{\"data\":[");
try {
@ -87,10 +97,8 @@ public class DataRequest extends PostRequest {
case "deletecity":
//DELETE FROM `cities` WHERE `id`=0
JDCB jdcbcc = new JDCB("users", "kOpaIJUjkgb9ur6S", "wasteinformation");
Log.debug(params.get("id"));
int status= jdcbcc.executeUpdate("DELETE FROM `cities` WHERE `id`='" + params.get("id")+"'");
int status= jdcb.executeUpdate("DELETE FROM `cities` WHERE `id`='" + params.get("id")+"'");
Log.debug(status);
break;

View File

@ -5,6 +5,7 @@ import com.wasteinformationserver.db.JDCB;
import com.wasteinformationserver.website.HttpTools;
import com.wasteinformationserver.website.basicrequest.PostRequest;
import java.io.IOException;
import java.util.HashMap;
public class RegisterRequest extends PostRequest {
@ -14,7 +15,13 @@ public class RegisterRequest extends PostRequest {
String passhash = HttpTools.StringToMD5(params.get("password"));
JDCB myjd = new JDCB("users", "kOpaIJUjkgb9ur6S", "wasteinformation");
JDCB myjd = null;
try {
myjd = JDCB.getInstance();
} catch (IOException e) {
e.printStackTrace();
}
//new JDCB("users", "kOpaIJUjkgb9ur6S", "wasteinformation");
int s = myjd.executeUpdate("INSERT INTO `user` (`username`, `firstName`, `secondName`, `password`, `email`, `logindate`) VALUES ('"+params.get("username")+"', '"+params.get("firstname")+"', '"+params.get("lastname")+"', '"+passhash+"', '"+params.get("email")+"', current_timestamp());");
// TODO: 27.09.19 detect if register process was successful and reply right json

View File

@ -5,6 +5,7 @@ import com.wasteinformationserver.db.JDCB;
import com.wasteinformationserver.website.HttpTools;
import com.wasteinformationserver.website.basicrequest.PostRequest;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
@ -18,7 +19,16 @@ public class LoginRequest extends PostRequest {
String password = params.get("password");
String username = params.get("username");
ResultSet s = new JDCB("users", "kOpaIJUjkgb9ur6S", "wasteinformation").executeQuery("select * from user where username ='" + username + "'");
JDCB jdcb;
try {
jdcb = JDCB.getInstance();
} catch (IOException e) {
Log.error("no connection to db");
return "{\"status\" : \"nodbconn\"}";
}
ResultSet s = jdcb.executeQuery("select * from user where username ='" + username + "'");;
//new JDCB("users", "kOpaIJUjkgb9ur6S", "wasteinformation").executeQuery("select * from user where username ='" + username + "'");
Log.debug("successfully logged in to db");
String response = "{\"accept\": false}";
try {

View File

@ -14,7 +14,7 @@ public class LoginState {
String lastname;
String email;
boolean loggedin = false;
boolean loggedin = true; //todo set back!!!
public void logIn(){
loggedin=true;