CData JDBC Driver for MongoDB 2017J - Online Help

ストアドプロシージャの呼び出し

CData JDBC Driver for MongoDB 2017J - Build 17.0.6669

You can use CallableStatement and Statement objects to execute stored procedures. Use CallableStatement objects to execute parameterized stored procedure calls. Use Statement objects to execute stored procedures as SQL statements with the EXEC syntax.

Using Callable Statement Objects

To execute a stored procedure as a parameterized query, you can use the CallableStatement class: the example below uses the generic execute method, which can be used to execute any stored procedure.

To return the stored procedure's results, call getResultSet.

To return a count of updated rows, call getUpdateCount.

String connectionString = "jdbc:mongodb:Server=127.0.0.1;Port=27017;Database=test;User=test;Password=test;"; 

Connection conn = DriverManager.getConnection(connectionString);
CallableStatement cstmt = conn.prepareCall("EVAL");
cstmt.setString("jsFunction", "function () { return db.restaurants.findOne(); }");
boolean ret = cstmt.execute();   
if (!ret){
  int count=cstmt.getUpdateCount();
  if (count!=-1)
  {
      System.out.println("Affected rows: "+count);
  }
}
else{
  ResultSet rs=cstmt.getResultSet();
  while(rs.next()){
    for(int i=1;i<=rs.getMetaData().getColumnCount();i++)
    {
      System.out.println(rs.getMetaData().getColumnName(i) +"="+rs.getString(i));
    }
  }
}

Using Statement Objects

When using the Statement class, use the EXEC syntax below:

EXECUTE my_proc @first = 1, @second = 2, @third = 3;

EXEC my_proc @first = 1, @second = 2, @third = 3;

The following example uses the generic execute method, which can be used to execute any stored procedure.

To return the stored procedure's results, call getResultSet.

To return a count of updated rows, call getUpdateCount.

String connectionString = "jdbc:mongodb:Server=127.0.0.1;Port=27017;Database=test;User=test;Password=test;"; 

Connection conn = DriverManager.getConnection(connectionString);
Statement stmt = conn.createStatement();
boolean ret = stmt.execute("EXEC EVAL jsFunction = 'function () { return db.restaurants.findOne(); }'");
       
  if (!ret){
    int count=stmt.getUpdateCount();
    if (count!=-1)
    {
      System.out.println("Affected rows: "+count);
    }
  }
  else{
    ResultSet rs=stmt.getResultSet();
    while(rs.next()){
      for(int i=1;i<=rs.getMetaData().getColumnCount();i++)
      {
        System.out.println(rs.getMetaData().getColumnName(i) +"="+rs.getString(i));
      }
    }
  }

 
 
Copyright (c) 2018 CData Software, Inc. - All rights reserved.
Build 17.0.6669.0