$(db)sql;…

Read(770) Label: data source, sql, result set,

Description:

Through esProc JDBC, execute a SQL statement in the data source and return the result set.

Syntax:

$(db)sql;

Note:

In the specified database db, the function executes the specified SQL statement, and returns the result set once executed. Use st.executeQuery() for the execution and return the result set. Make sure the database db must be connected.

 

In JDBC, a string starting with $select or $with will be interpreted as a simple SQL statement for execution.

Parameter:

sql

A SQL statement in the form of select * from table; it should be one of select/insert/delete/update statements.

(db)

Data source name; will be interpreted as a simple SQL statement when omitted.

SQL parameter’s value.

Return value:

Set

Example:

Execute SQL statement in the database:

public void testDataServer() {

    Connection con = null;

    java.sql.Statement st;

    try{

      Class.forName("com.esproc.jdbc.InternalDriver");

      con= DriverManager.getConnection("jdbc:esproc:local://"); 

      st=con.createStatement();

      // Query demo database student table to find the data of students who are older than 16

      ResultSet set = st.executeQuery("$(demo)select * from STUDENTS where AGE>?;16");

      printRs(set);

    }

    catch(Exception e){

      System.out.println(e);

    }

    finally{

      // Close the connection

      if (con!=null) {

        try {

          con.close();

        }

        catch(Exception e) {

          System.out.println(e);

        }

      }

    }

  }

 

Execute a simple SQL statement to query a data file:

public void testDataServer(){

     Connection con = null;

      java.sql.Statement st;

     try{

        // Establish data source connection

        Class.forName("com.esproc.jdbc.InternalDriver");

        con= DriverManager.getConnection("jdbc:esproc:local://");

        st =con.createStatement();

        //Use executeQuery method to execute the simple SQL statement and query data in cities.txt

        ResultSet set = st.executeQuery ("$select * from cities.txt ");

        ResultSetMetaData meta = set.getMetaData();

        while (set.next()) {

          for(int i=0; i<meta.getColumnCount(); i++){           

            System.out.print(set.getObject(i+1) + "\t");

          }

          System.out.println();

        }

        }

        catch(Exception e){

        System.out.println(e);

        }

        finally{

        // Close data source connection

        if (con!=null) {

        try {

        con.close();

        }

        catch(Exception e) {

        System.out.println(e);

        }

        }

        }