-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Fixes #547: Add MSSQL Driver Features Support Add an Example on how to connect with MSSQL using ZDBC natively. Adding the entry into main ZDBC Readme, on features for MSSQL and Redshift. * Fixes #547: Add MSSQL Driver Features Support Add an Example on how to connect with MSSQL using ZDBC natively. Adding the entry into main ZDBC Readme, on features for MSSQL and Redshift. Signed-off-by: NataS <[email protected]> --------- Signed-off-by: NataS <[email protected]>
- Loading branch information
1 parent
cc02c58
commit 05951eb
Showing
8 changed files
with
234 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Microsoft SQL Server Example | ||
This project contains a simple java application that uses the [OpenZiti](https://openziti.github.io/ziti/overview.html) Java SDK to connect to a Microsoft SQL Server (MSSQL) database and read the contents of a table. | ||
|
||
# What you will need | ||
* (Maven) [https://maven.apache.org/] to build the sample | ||
* A Java JDK to build and run the sample. If you don't have one you can get one from the [Oracle Open JDK download site](https://jdk.java.net/) | ||
|
||
# Building the sample | ||
Compilation is done using the Maven build tooling. | ||
|
||
1. Change into the `ziti-sdk-jvm/samples/ziti-jdbc-mssql` sample directory | ||
1. execute `../../gradlew -PbuildForAndroid=false build` | ||
|
||
# Running the example | ||
Running the example is done using the gradle run facility. | ||
It's assumed you already have the OpenZiti network running and the ziti services configured. | ||
|
||
Your services must have as intercept configuration the host `mssql_host` with the port `1433`; if different you may change the connection string in the `MSSQLExample.java` file. | ||
|
||
It's also assumed you have a Microsoft SQL Server running and a database named `DBNAME` with a table called `TABLENAME` with two columns `column_1` and `column_2`. | ||
|
||
|
||
You can run the example from the sample directory using: | ||
|
||
``` | ||
../../gradlew -PbuildForAndroid=false run | ||
``` | ||
|
||
## Example Output | ||
``` | ||
> Task :ziti-jdbc-mssql:runWithJavaExec | ||
[main] INFO org.openziti.impl.ZitiImpl - ZitiSDK version 0.25.1 @344b49b() | ||
Feb 22, 2024 5:32:02 PM org.openziti.jdbc.ZitiDriver setupZiti | ||
INFO: Ziti JDBC wrapper is configuring Ziti identities. Production applications should manage Ziti identities directly | ||
[DefaultDispatcher-worker-2] INFO org.openziti.api.Controller - controller[https://ziti-edge-controller/] version(v0.28.1/f9a62c0baf1c) | ||
[DefaultDispatcher-worker-1] INFO org.openziti.api.Controller - controller[https://ziti-edge-controller/] version(v0.28.1/f9a62c0baf1c) | ||
Column_1: AAA0000 - Column_2: 10.10.10.0 | ||
Column_1: AAA0001 - Column_2: 10.10.10.1 | ||
Column_1: AAA0002 - Column_2: 10.10.10.2 | ||
Column_1: AAA0003 - Column_2: 10.10.10.3 | ||
Column_1: AAA0004 - Column_2: 10.10.10.4 | ||
Column_1: AAA0005 - Column_2: 10.10.10.5 | ||
Column_1: AAA0006 - Column_2: 10.10.10.6 | ||
Column_1: AAA0007 - Column_2: 10.10.10.7 | ||
Column_1: AAA0008 - Column_2: 10.10.10.8 | ||
Column_1: AAA0009 - Column_2: 10.10.10.9 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright (c) 2018-2024 NetFoundry, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
plugins { | ||
id 'java' | ||
} | ||
|
||
ext { | ||
javaMainClass = "org.openziti.jdbc.MSSQLExample" | ||
} | ||
|
||
dependencies { | ||
implementation "org.openziti:ziti-jdbc:${versions.ziti}" | ||
implementation deps.ziti | ||
implementation files('lib/mssql-jdbc-12.6.0.jre11.jar') | ||
implementation deps.slf4jSimple | ||
} | ||
|
||
task runWithJavaExec(type: JavaExec) { | ||
group = "Execution" | ||
description = "Run the ZDBC example" | ||
classpath = sourceSets.main.runtimeClasspath | ||
mainClass = javaMainClass | ||
args "network/DBClient.json" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Microsoft SQL Server Test Network | ||
This directory contains the identity JSON. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>org.openziti.jdbc.samples</groupId> | ||
<artifactId>mssql</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<maven.compiler.source>11</maven.compiler.source> | ||
<maven.compiler.target>11</maven.compiler.target> | ||
</properties> | ||
|
||
<dependencies> | ||
|
||
<!-- MSSQL jdbc driver --> | ||
<dependency> | ||
<groupId>com.microsoft.sqlserver</groupId> | ||
<artifactId>mssql-jdbc</artifactId> | ||
<version>12.6.0.jre11</version> | ||
</dependency> | ||
|
||
<!-- ZDBC wrapper jar --> | ||
<dependency> | ||
<groupId>org.openziti</groupId> | ||
<artifactId>zdbc</artifactId> | ||
</dependency> | ||
|
||
<!-- Ziti SDK --> | ||
<dependency> | ||
<groupId>org.openziti</groupId> | ||
<artifactId>ziti</artifactId> | ||
</dependency> | ||
|
||
<!-- Optional logging dependencies --> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-api</artifactId> | ||
<version>1.7.22</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-simple</artifactId> | ||
<version>1.7.22</version> | ||
</dependency> | ||
|
||
</dependencies> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.codehaus.mojo</groupId> | ||
<artifactId>exec-maven-plugin</artifactId> | ||
<version>3.0.0</version> | ||
<configuration> | ||
<mainClass>org.openziti.jdbc.MSSQLExample</mainClass> | ||
<arguments> | ||
<argument>network/DBClient.json</argument> | ||
</arguments> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
68 changes: 68 additions & 0 deletions
68
samples/ziti-jdbc-mssql/src/main/java/org/openziti/jdbc/MSSQLExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright (c) NetFoundry, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package org.openziti.jdbc; | ||
|
||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.ResultSet; | ||
import java.sql.Statement; | ||
import java.util.Properties; | ||
|
||
import org.openziti.Ziti; | ||
import org.openziti.ZitiContext; | ||
|
||
public class MSSQLExample { | ||
public static void main(String[] args) throws Exception { | ||
if (args.length != 1) { | ||
System.out.println("Usage: App <ziti identity.json>"); | ||
System.exit(1); | ||
} | ||
|
||
// Initialize the OpenZiti Java SDK with our identity | ||
final ZitiContext zitiContext = Ziti.newContext(args[0], "".toCharArray()); | ||
|
||
String url = "zdbc:sqlserver://mssql_host:1433;databaseName=DBNAME"; | ||
|
||
Properties props = new Properties(); | ||
|
||
// General MSSQL properties | ||
props.setProperty("user", "zdbc"); | ||
props.setProperty("password", "netfoundry!1"); | ||
props.setProperty("connectTimeout", "240"); | ||
props.setProperty("encrypt", "False"); | ||
|
||
// To use MSSQL with zdbc driver we need to set the mssql driver's socketFactoryClass to ZitiSocketFactory | ||
props.setProperty("socketFactoryClass", "org.openziti.net.ZitiSocketFactory"); | ||
|
||
// Ziti specific properties | ||
props.setProperty("zitiIdentityFile", "network\\DBClient.json"); | ||
props.setProperty("zitiDriverUrlPattern", "^zdbc:sqlserver:.*"); | ||
props.setProperty("zitiDriverClassname", "com.microsoft.sqlserver.jdbc.SQLServerDriver"); | ||
|
||
|
||
try (Connection conn = DriverManager.getConnection(url, props)) { | ||
try (Statement stmt = conn.createStatement()) { | ||
try (ResultSet rs = stmt.executeQuery("select top 10 * from dbo.TABLE")) { | ||
while (rs.next()) { | ||
System.out.println("Column_1: " + rs.getString(1) + " - Column_2: " + rs.getString(2)); | ||
} | ||
} | ||
} | ||
} finally { | ||
Ziti.getContexts().forEach(c -> c.destroy()); | ||
} | ||
|
||
System.exit(0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters