Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Severe: java.lang.NullPointerException
- at fi.allu.savukelaskuri.KulutuksetResource.postJson(KulutuksetResource.java:90)
- at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
- at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
- at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
- at java.lang.reflect.Method.invoke(Method.java:498)
- import java.sql.*;
- public class Tietokanta {
- protected Connection yhteys = null;
- protected PreparedStatement valmisteltukysely = null;
- protected Statement kysely = null;
- protected ResultSet tulosjoukko = null;
- public boolean avaaYhteys() {
- boolean ok = true;
- try {
- Class.forName("com.mysql.cj.jdbc.Driver");
- yhteys = DriverManager.getConnection("jdbc:mysql://localhost/savukelaskuri?serverTimezone=UTC", "root", "");
- } catch (Exception e) {
- e.printStackTrace();
- ok = false;
- }
- return ok;
- }
- public boolean suljeYhteys() {
- boolean ok = true;
- try {
- this.yhteys.close();
- } catch (Exception e) {
- ok = false;
- }
- return ok;
- }
- }
- import javax.ws.rs.core.Context;
- import javax.ws.rs.core.UriInfo;
- import javax.ws.rs.Produces;
- import javax.ws.rs.Consumes;
- import javax.ws.rs.DELETE;
- import javax.ws.rs.GET;
- import javax.ws.rs.POST;
- import javax.ws.rs.Path;
- import javax.ws.rs.PUT;
- import javax.ws.rs.PathParam;
- import javax.ws.rs.core.MediaType;
- import org.json.JSONArray;
- import org.json.JSONObject;
- @Path("kulutukset")
- public class KulutuksetResource extends Tietokanta {
- JSONArray jsontaulu = new JSONArray();
- JSONObject jsonolio = null;
- @Context
- private UriInfo context;
- public KulutuksetResource() {
- this.avaaYhteys();
- }
- @POST
- @Consumes(MediaType.APPLICATION_JSON)
- @Produces(MediaType.APPLICATION_JSON)
- public boolean postJson(String content) {
- boolean ok = true;
- JSONObject json = new JSONObject(content);
- String pvm = json.getString("pvm");
- String klo = json.getString("klo");
- String kulutus = json.getString("kulutus");
- try {
- String sql = "INSERT INTO kulutus (pvm, klo, kulutus) VALUES (?,?,?)";
- this.valmisteltukysely.setString(1, pvm);
- this.valmisteltukysely.setString(2, klo);
- this.valmisteltukysely.setString(3, kulutus);
- this.suljeYhteys();
- } catch (Exception e) {
- e.printStackTrace();
- ok = false;
- }
- return ok;
- }
- }
Add Comment
Please, Sign In to add comment