Code からの接続
このセクションでは、JDBC DriverManager またはSAPBusinessOneDataSource インターフェースに接続する方法について説明します。
DriverManager で接続
DriverManager クラスを使用して接続する場合、CData JDBC Driver for SAP Business One はJDBC 規則に従います。最初にSAPBusinessOne ドライバークラスをロードします。次に、接続を作成します。
ドライバーのロード
JDBC 4.0仕様では、次の手順はオプションです。Class.forName("cdata.jdbc.sapbusinessone.SAPBusinessOneDriver");
接続の確立
静的なDriverManager クラスのgetConnection メソッドで接続文字列を指定します。接続文字列を"jdbc:sapbusinessone:" で始めます。一般的な接続文字列は以下のとおりです。
Connection conn = DriverManager.getConnection("jdbc:sapbusinessone:Url=http://localhost:50000/b1s/v1;User=username;Password=password;CompanyDB=dbname;");または、Properties オブジェクトを使用して接続オプションを準備できます。Properties オブジェクトをDriverManager に渡します。
Properties prop = new Properties(); prop.setProperty("Property1","Value1"); prop.setProperty("Property2","Value2"); Connection conn = DriverManager.getConnection("jdbc:sapbusinessone:,");
SAPBusinessOneDataSource クラスで接続
以下の例に示すように、SAPBusinessOneDataSource クラスを使用してプールされた接続を作成できます。詳しくは、接続プール を参照してください。
次の例は、プールされたConnection オブジェクトをインスタンス化します。
SAPBusinessOneDataSource ds = new SAPBusinessOneDataSource("jdbc:sapbusinessone:UseConnectionPooling=true;Url=http://localhost:50000/b1s/v1;User=username;Password=password;CompanyDB=dbname;");
Connection conn = ds.getConnection();