我们在将SQL语句嵌入应用程序时,必须按以下的两个步骤预编译应用程序并将其与数据库联编,步骤如下:

公司主营业务:网站设计制作、成都网站制作、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联建站是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联建站推出攸县免费做网站回馈大家。
1.创建源文件,以包含带嵌入式SQL语句的程序。
格式: # SQL{ SQL语句 } 。
2.连接数据库,然后预编译每个源文件。
语法: SQLJ 源文件名。
实例如下:
- import java.sql.*;
 - import sqlj.runtime.*;
 - import sqlj.runtime.ref.*;
 - #sql iterator App_Cursor1 (String empno, String firstnme) ;
 - #sql iterator App_Cursor2 (String) ;
 - class App
 - {
 - static
 - {
 - try
 - {
 - Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
 - }
 - catch (Exception e)
 - {
 - e.printStackTrace();
 - }
 - }
 - public static void main(String argv[])
 - {
 - try
 - {
 - App_Cursor1 cursor1;
 - App_Cursor1 cursor2;
 - String str1 = null;
 - String str2 = null;
 - int count1;
 - Connection con = null;
 - String url = "jdbc:odbc:tese2";
 - DefaultContext ctx = DefaultContext.getDefaultContext();
 - if (ctx == null) {
 - try {
 - if (argv.length == 0) {
 - String userid ="tdl";
 - String passwd ="user";
 - con = DriverManager.getConnection(url, userid, passwd);
 - }
 - else if (argv.length == 2) {
 - // connect with default id/password
 - con = DriverManager.getConnection(url);
 - }
 - else {
 - System.out.println("Usage: java App [username password]");
 - System.exit(0);
 - }
 - con.setAutoCommit(false);
 - ctx = new DefaultContext(con);
 - }
 - catch (SQLException e) {
 - System.out.println("Error: could not get a default context");
 - System.err.println(e) ;
 - System.exit(1);
 - }
 - DefaultContext.setDefaultContext(ctx);
 - }
 - #sql cursor1 = { SELECT empno, firstnme from db2admin.employee };
 - System.out.println("Received results:");
 - while (cursor1.next()) {
 - str1 = cursor1.empno();
 - str2 = cursor1.firstnme();
 - System.out.print (" empno= " + str1);
 - System.out.print (" firstname= " + str2);
 - System.out.print ("");
 - }
 - cursor1.close();
 - #sql cursor2 = { SELECT firstnme from db2admin.employee where empno = :str1 };
 - System.out.println("Received results:");
 - while (true) {
 - #sql { FETCH :cursor2 INTO :str2 };
 - if (cursor2.endFetch()) break;
 - System.out.print (" empno= " + str1);
 - System.out.print (" firstname= " + str2);
 - System.out.print ("");
 - }
 - cursor2.close();
 - // rollback the update
 - System.out.println("Rollback the update...");
 - #sql { ROLLBACK work };
 - System.out.println("Rollback done.");
 - }
 - catch( Exception e )
 - {
 - e.printStackTrace();
 - }
 - }
 - }
 
注:本程序采用JDBCODBC桥的方式访问数据库,必须配置ODBC数据源。
关于嵌入式SQL(SQLJ)的知识就介绍到这里了,希望本次的介绍能够给您带来一些收获!
【编辑推荐】
                当前文章:一个将SQL语句嵌入到Java应用程序中的实例
                
                地址分享:http://www.csdahua.cn/qtweb/news24/230824.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网