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.
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):
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); . . . }