An example of embedding SQL statements into Java applications

Source: Internet
Author: User

We areSQL statementWhen embedding an application, you must precompile the application and associate it with the database by following the two steps below:

1. Create a source file to includeEmbedded SQLStatement program.

Format: # SQL {SQL statement }.

2. Connect to the database and pre-compile each source file.

Syntax: SQLJ source file name.

Example:

 
 
  1. import java.sql.*;  
  2.  
  3. import sqlj.runtime.*;  
  4.  
  5. import sqlj.runtime.ref.*;  
  6.  
  7. #sql iterator App_Cursor1 (String empno, String firstnme) ;  
  8.  
  9. #sql iterator App_Cursor2 (String) ;  
  10.  
  11. class App  
  12.  
  13. {  
  14.  
  15. static  
  16.  
  17. {  
  18.  
  19. try  
  20.  
  21. {  
  22.  
  23. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();  
  24.  
  25. }  
  26.  
  27. catch (Exception e)  
  28.  
  29. {  
  30.  
  31. e.printStackTrace();  
  32.  
  33. }  
  34.  
  35. }  
  36.  
  37. public static void main(String argv[])  
  38.  
  39. {  
  40.  
  41. try  
  42.  
  43. {  
  44.  
  45. App_Cursor1 cursor1;  
  46.  
  47. App_Cursor1 cursor2;  
  48.  
  49. String str1 = null;  
  50.  
  51. String str2 = null;  
  52.  
  53. int count1;  
  54.  
  55. Connection con = null;  
  56.  
  57. String url = "jdbc:odbc:tese2";  
  58.  
  59. DefaultContext ctx = DefaultContext.getDefaultContext();  
  60.  
  61. if (ctx == null) {  
  62.  
  63. try {  
  64.  
  65. if (argv.length == 0) {  
  66.  
  67. String userid ="tdl";  
  68.  
  69. String passwd ="user";  
  70.  
  71. con = DriverManager.getConnection(url, userid, passwd);  
  72.  
  73. }  
  74.  
  75. else if (argv.length == 2) {  
  76.  
  77. // connect with default id/password  
  78.  
  79. con = DriverManager.getConnection(url);  
  80.  
  81. }  
  82.  
  83. else {  
  84.  
  85. System.out.println("Usage: java App [username password]");  
  86.  
  87. System.exit(0);  
  88.  
  89. }  
  90.  
  91. con.setAutoCommit(false);  
  92.  
  93. ctx = new DefaultContext(con);  
  94.  
  95. }  
  96.  
  97. catch (SQLException e) {  
  98.  
  99. System.out.println("Error: could not get a default context");  
  100.  
  101. System.err.println(e) ;  
  102.  
  103. System.exit(1);  
  104.  
  105. }  
  106.  
  107. DefaultContext.setDefaultContext(ctx);  
  108.  
  109. }  
  110.  
  111. #sql cursor1 = { SELECT empno, firstnme from db2admin.employee };  
  112.  
  113.  
  114. System.out.println("Received results:");  
  115.  
  116. while (cursor1.next()) {  
  117.  
  118. str1 = cursor1.empno();  
  119.  
  120. str2 = cursor1.firstnme();  
  121.  
  122. System.out.print (" empno= " + str1);  
  123.  
  124. System.out.print (" firstname= " + str2);  
  125.  
  126. System.out.print ("");  
  127.  
  128. }  
  129.  
  130. cursor1.close();  
  131.  
  132. #sql cursor2 = { SELECT firstnme from db2admin.employee where empno = :str1 };  
  133.  
  134. System.out.println("Received results:");  
  135.  
  136. while (true) {  
  137.  
  138. #sql { FETCH :cursor2 INTO :str2 };  
  139.  
  140. if (cursor2.endFetch()) break;  
  141.  
  142. System.out.print (" empno= " + str1);  
  143.  
  144. System.out.print (" firstname= " + str2);  
  145.  
  146. System.out.print ("");  
  147.  
  148. }  
  149.  
  150. cursor2.close();  
  151.  
  152. // rollback the update  
  153.  
  154. System.out.println("Rollback the update...");  
  155.  
  156. #sql { ROLLBACK work };  
  157.  
  158. System.out.println("Rollback done.");  
  159.  
  160. }  
  161.  
  162. catch( Exception e )  
  163.  
  164. {  
  165.  
  166. e.printStackTrace();  
  167.  
  168. }  
  169.  
  170. }  
  171.  

Note: The program uses the JDBCODBC bridge to access the database. The ODBC data source must be configured.

Here is the introduction of embedded SQLSQLJ. I hope this introduction will bring you some gains!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.