Show TOC

Background documentationNative SQL Access Locate this document in the navigation structure

 

In exceptional cases it may be necessary to program nonportable SQL statements that exceed the scope of the Open SQL/JDBC language. For this purpose, the help class com.sap.sql.NativeSQLAccess is provided.

Features

Using the static method NativeSQLAccess.getVendorID(Connection conn), you can determine the underlying database vendor. You use this method to ensure that a nonportable SQL statement is issued on the corresponding database platform only.

The NativeSQLAccess class offers three options for issuing a nonportable SQL statement:

  • NativeSQLAccess.createNativeStatement() - generates an object of the class java.sql.Statement.

  • NativeSQLAccess.prepareNativeStatement() - generates an object of the class java.sql.PreparedStatement.

  • NativeSQLAccess.prepareNativeCall() - generates an object of the class java.sql.CallableStatement (a Stored Procedure is called).

The following code snippet is an example of database-specific coding (where conn is a database connection object that already exists):

Example Example

  1. import java.sql.PreparedStatement;
    import com.sap.sql.NativeSQLAccess;
    
    String mssOnlyStmt = "…";
    // ensure correct process environment
    if (NativeSQLAccess.getVendorID(conn) !=
    	NativeSQLAccess.VENDOR_MS_SQL_SERVER) {
    // error handling
    } else {
    // variant 1
    	PreparedStatement ps =
       NativeSQLAccess.prepareNativeStatement(
    	   conn, mssOnlyStmt);
    	. . . 
    // variant 2
    	Statement stmt =
    		NativeSQLAccess.createNativeStatement(conn);
    	int result = stmt.execute(mssOnlyStmt);
    	. . .
    // variant 3
    	CallableStatement cs =
    		NativeSQLAccess.prepareNativeCall(
    			conn, mssOnlyStmt);
    	. . .
    }
    
End of the code.