Recent

Author Topic: LAMW - ORACLE DB 11, 12c component  (Read 1073 times)

Mongkey

  • Sr. Member
  • ****
  • Posts: 433
LAMW - ORACLE DB 11, 12c component
« on: July 13, 2024, 11:26:15 am »
Tutorial

- OJDBC 8: certified with JDK 8, for Oracle database 11g and 12c.
- OJDBC 10: certified with JDK 10, for Oracle database 18c and 19c.

https://www.codejava.net/java-se/jdbc/connect-to-oracle-database-via-jdbc
« Last Edit: July 21, 2024, 01:43:04 am by Mongkey »

Mongkey

  • Sr. Member
  • ****
  • Posts: 433
Re: LAMW - ORACLE DB 11, 12c component
« Reply #1 on: July 20, 2024, 01:41:57 pm »
Code: Java  [Select][+][-]
  1. package org.lamw.test;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6.  
  7. import java.util.Properties;
  8.  
  9. import java.io.IOException;
  10. import java.sql.Connection;
  11. import java.sql.DriverManager;
  12. import java.sql.ResultSet;
  13. import java.sql.ResultSetMetaData;
  14. import java.sql.SQLException;
  15. import java.sql.Statement;
  16.  
  17.  
  18. /**
  19.  * This program demonstrates how to make database connection with Oracle
  20.  * database server.
  21.  * @author www.codejava.net
  22.  *
  23.  */
  24.  
  25.  
  26.  
  27. public class JdbcOracleConnection {
  28.     private static final String DEFAULT_URL = "jdbc:oracle:thin:@192.168.0.102:1521:oracle"; // Change IP_Address:Database
  29.     private static String DEFAULT_USERNAME;
  30.     private static String DEFAULT_PASSWORD;
  31.     private static String DEFAULT_SQL;
  32.     private static final String DEFAULT_DRIVER = "oracle.jdbc.driver.OracleDriver";
  33.  
  34.     /**
  35.      * URL to connect database
  36.      */
  37.  
  38.     public static Connection createConnection(String driver, String url, String username, String password) throws ClassNotFoundException, SQLException {
  39.         Class.forName(driver);
  40.         return DriverManager.getConnection(url, username, password);
  41.     }
  42.  
  43.  
  44.     public static void main(String[] args) {
  45.         Connection conn = null;
  46.         try {
  47.             // registers Oracle JDBC driver - though this is no longer required
  48.             // since JDBC 4.0, but added here for backward compatibility
  49.             //------1st METHOD
  50.             //Class.forName("oracle.jdbc.OracleDriver");
  51.  
  52.             //Properties properties = new Properties();
  53.             //properties.put("user", DEFAULT_USERNAME);
  54.             //properties.put("password", DEFAULT_PASSWORD);
  55.             //properties.put("defaultRowPrefetch", "20");
  56.             //conn = DriverManager.getConnection(DEFAULT_URL, properties);
  57.             //------2nd METHOD
  58.             conn  = createConnection(DEFAULT_DRIVER,DEFAULT_URL,DEFAULT_USERNAME,DEFAULT_PASSWORD);
  59.  
  60.             if (conn != null) {
  61.                 System.out.println("Connected with oracle connection");
  62.                 Statement stmt = conn.createStatement();
  63.                 StringBuilder stringBuilder = new StringBuilder();
  64.                                                 /*
  65.                                   Executing Query for get Data
  66.                                  */
  67.                 ResultSet rs = stmt.executeQuery(DEFAULT_SQL); // write your table name here..
  68.  
  69.                                 /*
  70.                                   Getting MetaData
  71.                                  */
  72.                 ResultSetMetaData rsmd = rs.getMetaData();
  73.                 int columnsNumber = rsmd.getColumnCount();
  74.                 // print column names
  75.                 for (int i = 1; i <= columnsNumber; i++) {
  76. //                    TextView textView = new TextView(context);
  77. //                    textView.setText(getString(R.string.tab).concat(rsmd.getColumnName(i)).concat(getString(R.string.tab)));
  78. //                    textView.setLayoutParams(rowParams);// TableRow is the parent view
  79. //                    textView.setTypeface(textView.getTypeface(), Typeface.BOLD);
  80. //                    tableRow.addView(textView);
  81.                 }
  82.  
  83. //                tableLayout.addView(tableRow);
  84.  
  85.                                 /*
  86.                                   Getting each row
  87.                                  */
  88.                 while (rs.next()) {
  89. //                    TableRow tableRow1 = new TableRow(context);
  90. //                    tableRow1.setLayoutParams(rowParams);// TableLayout is the parent view
  91.  
  92.                                         /*
  93.                                           Getting Columns
  94.                                          */
  95.                     for (int i = 1; i <= columnsNumber; i++) {
  96.                         String columnValue = rs.getString(i);
  97.                         stringBuilder.append(columnValue);
  98.  
  99. //                        TextView textView = new TextView(context);
  100. //                        textView.setText(getString(R.string.tab).concat(columnValue).concat(getString(R.string.tab)));
  101. //                        textView.setLayoutParams(rowParams);// TableRow is the parent view
  102. //                        tableRow1.addView(textView);
  103.                     }
  104.                     /* Adding into Table */
  105. //                    tableLayout.addView(tableRow1);
  106.                 }
  107.                                 /*
  108.                                   Closing connection.
  109.                                  */
  110.                 conn.close();
  111.     }
  112.         } catch (ClassNotFoundException ex) {
  113.             ex.printStackTrace();
  114.         } catch (SQLException ex) {
  115.             ex.printStackTrace();
  116.         } finally {
  117.             try {
  118.                 if (conn != null && !conn.isClosed()) {
  119.                     conn.close();
  120.                 }
  121.             } catch (SQLException ex) {
  122.                 ex.printStackTrace();
  123.             }
  124.         }
  125.     }
  126. }
  127.  

Code: Pascal  [Select][+][-]
  1. unit ujdbcoracleconnection;
  2.  
  3. {$mode delphi}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, And_jni, {And_jni_Bridge,} AndroidWidget;
  9.  
  10. type
  11.  
  12. {Draft Component code by "LAMW: Lazarus Android Module Wizard" [7/21/2024 7:34:18 AM]}
  13. {https://github.com/jmpessoa/lazandroidmodulewizard}
  14.  
  15. {jControl template}
  16.  
  17. JdbcOracleConnection = class(jControl)
  18.  private
  19.  
  20.  public
  21.     constructor Create(AOwner: TComponent); override;
  22.     destructor  Destroy; override;
  23.     procedure Init; override;
  24.     procedure main(var args: TDynArrayOfString);
  25.  
  26.  published
  27.  
  28. end;
  29.  
  30. procedure JdbcOracleConnection_main(env: PJNIEnv; _jdbcoracleconnection: JObject; var args: TDynArrayOfString);
  31.  
  32.  
  33. implementation
  34.  
  35. {---------  JdbcOracleConnection  --------------}
  36.  
  37. constructor JdbcOracleConnection.Create(AOwner: TComponent);
  38. begin
  39.   inherited Create(AOwner);
  40. //your code here....
  41. end;
  42.  
  43. destructor JdbcOracleConnection.Destroy;
  44. begin
  45.   if not (csDesigning in ComponentState) then
  46.   begin
  47.      if FjObject <> nil then
  48.      begin
  49.        jFree();
  50.        FjObject:= nil;
  51.      end;
  52.   end;
  53.   //you others free code here...'
  54.   inherited Destroy;
  55. end;
  56.  
  57. procedure JdbcOracleConnection.Init;
  58. begin
  59.  
  60.   if FInitialized  then Exit;
  61.   inherited Init; //set default ViewParent/FjPRLayout as jForm.View!
  62.   //your code here: set/initialize create params....
  63.   FjObject := jCreate(); //jSelf !
  64.  
  65.   if FjObject = nil then exit;
  66.  
  67.   FInitialized:= True;
  68. end;
  69.  
  70.  
  71. procedure JdbcOracleConnection.main(var args: TDynArrayOfString);
  72. begin
  73.   //in designing component state: set value here...
  74.   if FInitialized then
  75.      JdbcOracleConnection_main(gApp.jni.jEnv, FjObject, args);
  76. end;
  77.  
  78. {-------- JdbcOracleConnection_JNI_Bridge ----------}
  79.  
  80. procedure JdbcOracleConnection_main(env: PJNIEnv; _jdbcoracleconnection: JObject; var args: TDynArrayOfString);
  81. var
  82.   jParams: array[0..0] of jValue;
  83.   jMethod: jMethodID=nil;
  84.   jCls: jClass=nil;
  85.   newSize0: integer;
  86.   jNewArray0: jObject=nil;
  87.   i: integer;
  88. label
  89.   _exceptionOcurred;
  90. begin
  91.  
  92.   if (env = nil) or (_jdbcoracleconnection = nil) then exit;
  93.   jCls:= env^.GetObjectClass(env, _jdbcoracleconnection);
  94.   if jCls = nil then goto _exceptionOcurred;
  95.   jMethod:= env^.GetMethodID(env, jCls, 'main', '([Ljava/lang/String;)V');
  96.   if jMethod = nil then begin env^.DeleteLocalRef(env, jCls); goto _exceptionOcurred; end;
  97.  
  98.   newSize0:= ?; //Length(?);
  99.   jNewArray0:= env^.NewObjectArray(env, newSize0, env^.FindClass(env,'java/lang/String'),env^.NewStringUTF(env, PChar('')));
  100.   for i:= 0 to newSize0 - 1 do
  101.   begin
  102.      env^.SetObjectArrayElement(env,jNewArray0,i,env^.NewStringUTF(env, PChar(args[i])));
  103.   end;
  104.   jParams[0].l:= jNewArray0;
  105.  
  106.   env^.CallVoidMethodA(env, _jdbcoracleconnection, jMethod, @jParams);
  107. env^.DeleteLocalRef(env,jParams[0].l);
  108.  
  109.   env^.DeleteLocalRef(env, jCls);
  110.  
  111.   _exceptionOcurred: jni_ExceptionOccurred(env);
  112. end;
  113.  
  114.  
  115.  
  116. end.
  117.  
« Last Edit: July 21, 2024, 02:45:14 am by Mongkey »

Mongkey

  • Sr. Member
  • ****
  • Posts: 433

 

TinyPortal © 2005-2018