Advertisement
Guest User

Untitled

a guest
Apr 12th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. import java.net.URI;
  2. import java.net.URISyntaxException;
  3. import java.sql.*;
  4.  
  5. public class PrestoJDBC {
  6. // JDBC driver name and database URL
  7. static final String JDBC_DRIVER = "com.facebook.presto.jdbc.PrestoDriver";
  8. //static final String JDBC_DRIVER = "com.teradata.presto.jdbc42.Driver";
  9.  
  10. static final String DB_URL = "jdbc:presto://ec2-xx-xx-xxx-xxx.ap-northeast-1.compute.amazonaws.com:8889/hive/default";
  11.  
  12. // Database credentials
  13. static final String USER = "hadoop";
  14. static final String PASS = "";
  15.  
  16. public static void main(String[] args) throws URISyntaxException {
  17. Connection conn = null;
  18. Statement stmt = null;
  19. try{
  20. //STEP 2: Register JDBC driver
  21. Class.forName(JDBC_DRIVER);
  22.  
  23. //STEP 3: Open a connection
  24. //conn = DriverManager.getConnection(DB_URL,USER,PASS);
  25. conn = DriverManager.getConnection(DB_URL,USER,PASS);
  26.  
  27. //STEP 4: Execute a query
  28. stmt = conn.createStatement();
  29. String sql;
  30. sql = "select * from sales";
  31. ResultSet rs = stmt.executeQuery(sql);
  32.  
  33. //STEP 5: Extract data from result set
  34. while(rs.next()){
  35. /*
  36. //Retrieve by column name
  37. int nationkey = rs.getInt("n_nationkey");
  38. String name = rs.getString("n_name");
  39. int regionkey = rs.getInt("n_regionkey");
  40. String comment = rs.getString("n_comment");
  41. */
  42. //Display values
  43. System.out.println(rs.getString(1));
  44. }
  45. //STEP 6: Clean-up environment
  46. rs.close();
  47. stmt.close();
  48. conn.close();
  49. }catch(SQLException se){
  50. //Handle errors for JDBC
  51. se.printStackTrace();
  52. }catch(Exception e){
  53. //Handle errors for Class.forName
  54. e.printStackTrace();
  55. }finally{
  56. //finally block used to close resources
  57. try{
  58. if(stmt!=null)
  59. stmt.close();
  60. }catch(SQLException se2){
  61. }
  62. try{
  63. if(conn!=null)
  64. conn.close();
  65. }catch(SQLException se){
  66. se.printStackTrace();
  67. }
  68. }
  69. System.out.println("Goodbye!");
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement