Advertisement
Guest User

Ébauche Assignement Everflow

a guest
May 2nd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.34 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.io.InputStream;
  3. import java.net.URL;
  4. import java.sql.*;
  5.  
  6. import org.glassfish.json.JsonProviderImpl;
  7. import javax.json.*;
  8. import javax.json.stream.JsonParser;
  9. import javax.json.stream.JsonParser.Event;
  10.  
  11. public class Main {
  12.  
  13.     public static void main(String[] args) {
  14.        
  15.        
  16.        
  17.         try {
  18.             Class.forName("com.mysql.jdbc.Driver");
  19.             Connection con = DriverManager.getConnection(  
  20.                     "jdbc:mysql://localhost:3306/everflow","root","benben");
  21.             Statement stmt = con.createStatement();
  22.             String query = "INSERT INTO test VALUES (";
  23.            
  24.             URL url = new URL("https://newsapi.org/v2/top-headlines?country=us&category=business&apiKey=57f4696d8c28484f849bc09108670849");
  25.             InputStream is = url.openStream();
  26.             JsonReader reader = Json.createReader(is);
  27.            
  28.             JsonObject obj = reader.readObject();
  29.             JsonArray results = obj.getJsonArray("data");
  30.            
  31.            
  32.             for(JsonObject result : results.getValuesAs(JsonObject.class)) {
  33.                 //Pour tester que les objets sont biens reçus
  34.                 System.out.print(result.getJsonObject("from").getString("name"));
  35.                 System.out.print(": ");
  36.                 System.out.println(result.getString("message", ""));
  37.                 System.out.println("-----------");
  38.             }
  39.            
  40.            
  41.            
  42.         } catch (IOException | ClassNotFoundException | SQLException e) {
  43.             e.printStackTrace();
  44.         }
  45.  
  46.     }
  47.  
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement