@WebMethod(operationName = "update") public int update( @WebParam(name = "id") int id, @WebParam(name = "token") String token, @WebParam(name = "user-agent") String ua, @WebParam(name = "ip") String ip, @WebParam(name = "topic") String topic, @WebParam(name = "content") String content) { Connection conn = null; PreparedStatement ps = null; int res = -1; InformationToken it = new InformationToken(); String email = it.getEmail(token, ua, ip); if (it.getStatus() != 200) { return -1 * it.getStatus(); } else { try { // new com.mysql.jdbc.Driver(); Class.forName("com.mysql.jdbc.Driver").newInstance(); // conn = // DriverManager.getConnection("jdbc:mysql://localhost:3306/databasename?user=username&password=password"); String connectionUrl = "jdbc:mysql://localhost:3306/stackexchange"; String connectionUser = "******"; String connectionPassword = ""; conn = DriverManager.getConnection(connectionUrl, connectionUser, connectionPassword); ps = conn.prepareStatement("select name from user where email = ?"); ps.setString(1, email); ResultSet rs = ps.executeQuery(); rs.next(); String name = rs.getString("name"); ps = conn.prepareStatement( "update question set name = ?, email = ?, topic = ?, content = ? where id = ?;"); ps.setString(1, name); ps.setString(2, email); ps.setString(3, topic); ps.setString(4, content); ps.setInt(5, id); res = ps.executeUpdate(); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException e) { } finally { try { if (ps != null) ps.close(); } catch (SQLException e) { } try { if (conn != null) conn.close(); } catch (SQLException e) { } } return res; } }
@WebMethod(operationName = "vote") public int vote( @WebParam(name = "id") int id, @WebParam(name = "token") String token, @WebParam(name = "user-agent") String ua, @WebParam(name = "ip") String ip, @WebParam(name = "value") int val) { Connection conn = null; PreparedStatement ps = null; int executeUpdate = -2; InformationToken it = new InformationToken(); String mail = it.getEmail(token, ua, ip); if (it.getStatus() != 200) { return -1 * it.getStatus(); } try { // new com.mysql.jdbc.Driver(); Class.forName("com.mysql.jdbc.Driver").newInstance(); // conn = // DriverManager.getConnection("jdbc:mysql://localhost:3306/databasename?user=username&password=password"); String connectionUrl = "jdbc:mysql://localhost:3306/stackexchange"; String connectionUser = "******"; String connectionPassword = ""; conn = DriverManager.getConnection(connectionUrl, connectionUser, connectionPassword); ps = conn.prepareStatement( "select * from uservote where id_mail = ? and category = 'q' and id = ?"); ps.setString(1, mail); ps.setInt(2, id); ResultSet rs1 = ps.executeQuery(); if (rs1.next()) { return -1; } else { ps = conn.prepareStatement("insert into uservote values(?,'q',?)"); ps.setString(1, mail); ps.setInt(2, id); ps.executeUpdate(); ps = conn.prepareStatement("select vote from question where id = ?"); ps.setInt(1, id); ResultSet rs = ps.executeQuery(); rs.next(); int currentVote = rs.getInt("vote"); ps = conn.prepareStatement("update question set vote = ? where id = ?"); ps.setInt(1, currentVote + val); ps.setInt(2, id); executeUpdate = ps.executeUpdate(); } } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException e) { return 0; } finally { try { if (ps != null) ps.close(); } catch (SQLException e) { } try { if (conn != null) conn.close(); } catch (SQLException e) { } } return executeUpdate; }