/** Web service operation */ @WebMethod(operationName = "search") @WebResult(name = "Question") public ArrayList<Question> search(@WebParam(name = "key") String key) { // TODO write your implementation code here: ArrayList<Question> Questions = new ArrayList<Question>(); Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; 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 question where topic like ? or content like ?;"); ps.setString(1, "%" + key + "%"); ps.setString(2, "%" + key + "%"); rs = ps.executeQuery(); while (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); String email = rs.getString("email"); String topic = rs.getString("topic"); String content = rs.getString("content"); int vote = rs.getInt("vote"); Questions.add(new Question(id, name, email, topic, content, vote)); } } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException e) { } finally { try { if (rs != null) rs.close(); } catch (SQLException e) { } try { if (ps != null) ps.close(); } catch (SQLException e) { } try { if (conn != null) conn.close(); } catch (SQLException e) { } } return Questions; }
@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 = "getQuestionById") @WebResult(name = "Question") public Question getQuestionById(@WebParam(name = "qid") int qid) throws Exception { // TODO write your implementation code here: Question Q = new Question(); Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; 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 question where id = ?"); ps.setInt(1, qid); rs = ps.executeQuery(); while (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); String email = rs.getString("email"); String topic = rs.getString("topic"); String content = rs.getString("content"); int vote = rs.getInt("vote"); Q = new Question(id, name, email, topic, content, vote); } } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException e) { throw e; } finally { if (rs != null) rs.close(); if (ps != null) ps.close(); } return Q; }