Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.71 KB | None | 0 0
  1. package fr.su.commande.service.business.impl;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8.  
  9. import org.junit.Test;
  10.  
  11. public class JsonTest {
  12.  
  13.     // private static final String DB_DRIVER = "oracle.jdbc.driver.OracleDriver";
  14.     // private static final String DB_CONNECTION = "jdbc:oracle:thin:@localhost:1521:MKYONG";
  15.     // private static final String DB_USER = "user";
  16.     // private static final String DB_PASSWORD = "password";
  17.     private static final String DB_DRIVER = "com.ibm.as400.access.AS400JDBCDriver";
  18.     private static final String DB_CONNECTION = "jdbc:as400://u3reca83;date format=iso;prompt=false;naming=system;trace=false;";
  19.     private static final String DB_USER = "smetivie";
  20.     private static final String DB_PASSWORD = "superu";
  21.  
  22.     public JsonTest() {
  23.         super();
  24.     }
  25.  
  26.     public static void main(String[] argv) {
  27.  
  28.     }
  29.  
  30.     @Test
  31.     public void selectRecordsFromTable() throws SQLException {
  32.  
  33.         Connection dbConnection = null;
  34.         PreparedStatement preparedStatement = null;
  35.  
  36.         String selectSQL = "SELECT * FROM $tableName";
  37.         String query2 = "SELECT * FROM TABLE (MANLIB.JSONSME1('DMOREAU')) a";
  38.         String maTable = "MANLIB.SM";
  39.  
  40.         try {
  41.             dbConnection = getDBConnection();
  42.  
  43.             String query = selectSQL.replace("$tableName", maTable);
  44.             preparedStatement = dbConnection.prepareStatement(query2);
  45.             // preparedStatement.setString(1, maTable);
  46.  
  47.             // execute select SQL stetement
  48.             ResultSet rs = preparedStatement.executeQuery();
  49.  
  50.             while (rs.next()) {
  51.  
  52.                 String json = rs.getString("DONNEE");
  53.  
  54.                 System.out.println("json : " + json);
  55.  
  56.             }
  57.  
  58.         } catch (SQLException e) {
  59.  
  60.             System.out.println(e.getMessage());
  61.  
  62.         } finally {
  63.  
  64.             if (preparedStatement != null) {
  65.                 preparedStatement.close();
  66.             }
  67.  
  68.             if (dbConnection != null) {
  69.                 dbConnection.close();
  70.             }
  71.  
  72.         }
  73.  
  74.     }
  75.  
  76.     private static Connection getDBConnection() {
  77.  
  78.         Connection dbConnection = null;
  79.  
  80.         try {
  81.  
  82.             Class.forName(DB_DRIVER);
  83.  
  84.         } catch (ClassNotFoundException e) {
  85.  
  86.             System.out.println(e.getMessage());
  87.  
  88.         }
  89.  
  90.         try {
  91.  
  92.             dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
  93.             return dbConnection;
  94.  
  95.         } catch (SQLException e) {
  96.  
  97.             System.out.println(e.getMessage());
  98.  
  99.         }
  100.  
  101.         return dbConnection;
  102.  
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement