Apache Presto - Interface JDBC

L'interface JDBC de Presto est utilisée pour accéder à l'application Java.

Conditions préalables

Installez presto-jdbc-0.150.jar

Vous pouvez télécharger le fichier JDBC jar en visitant le lien suivant,

https://repo1.maven.org/maven2/com/facebook/presto/presto-jdbc/0.150/

Une fois le fichier jar téléchargé, ajoutez-le au chemin de classe de votre application Java.

Créer une application simple

Créons une application Java simple en utilisant l'interface JDBC.

Codage - PrestoJdbcSample.java

import java.sql.*; 
import com.facebook.presto.jdbc.PrestoDriver; 

//import presto jdbc driver packages here.  
public class PrestoJdbcSample {  
   public static void main(String[] args) {  
      Connection connection = null; 
      Statement statement = null;  
      try { 
         
         Class.forName("com.facebook.presto.jdbc.PrestoDriver");  
         connection = DriverManager.getConnection(
         "jdbc:presto://localhost:8080/mysql/tutorials", "tutorials", “"); 
         
         //connect mysql server tutorials database here 
         statement = connection.createStatement(); 
         String sql;  
         sql = "select auth_id, auth_name from mysql.tutorials.author”; 
        
         //select mysql table author table two columns  
         ResultSet resultSet = statement.executeQuery(sql);  
         while(resultSet.next()){  
            int id  = resultSet.getInt("auth_id"); 
            String name = resultSet.getString(“auth_name");  
            System.out.print("ID: " + id + ";\nName: " + name + "\n"); 
         }  
         
         resultSet.close(); 
         statement.close(); 
         connection.close(); 
         
      }catch(SQLException sqlException){ 
         sqlException.printStackTrace(); 
      }catch(Exception exception){ 
         exception.printStackTrace(); 
      } 
   } 
}

Enregistrez le fichier et quittez l'application. Maintenant, démarrez le serveur Presto dans un terminal et ouvrez un nouveau terminal pour compiler et exécuter le résultat. Voici les étapes -

Compilation

~/Workspace/presto/presto-jdbc $ javac -cp presto-jdbc-0.149.jar  PrestoJdbcSample.java

Exécution

~/Workspace/presto/presto-jdbc $ java -cp .:presto-jdbc-0.149.jar  PrestoJdbcSample

Production

INFO: Logging initialized @146ms  
ID: 1; 
Name: Doug Cutting 
ID: 2; 
Name: James Gosling 
ID: 3; 
Name: Dennis Ritchie