|
1 |
| -# Execute SQL (JDBC) |
| 1 | +# JDBC Driver Usage |
| 2 | + |
| 3 | +## Downloading the driver |
| 4 | + |
| 5 | +The jt400.jar driver is included in the [JTOpen package on Maven Central](https://mvnrepository.com/artifact/net.sf.jt400/jt400). |
| 6 | + |
| 7 | +The driver must be placed in the directory that is specified by the application. |
| 8 | + |
| 9 | +## JDBC URL |
| 10 | + |
| 11 | +JDBC URL: jdbc:as400://hostname/default-schema |
| 12 | + |
| 13 | +replace hostname and default-schema with the database details from the IBMi server. |
| 14 | + |
| 15 | +e.g. `jdbc:as400://pub400.com/qgpl` |
| 16 | + |
| 17 | +## Usage |
| 18 | + |
| 19 | +Javadoc can be found at [https://javadoc.io/doc/net.sf.jt400/jt400/latest/index.html](https://javadoc.io/doc/net.sf.jt400/jt400/latest/index.html) |
| 20 | + |
| 21 | +```java |
| 22 | +import java.sql.Connection; |
| 23 | +import java.sql.DriverManager; |
| 24 | +import java.sql.PreparedStatement; |
| 25 | +import java.sql.ResultSet; |
| 26 | +import java.sql.SQLException; |
| 27 | +import java.util.Properties; |
| 28 | + |
| 29 | +public class AS400JDBCExample { |
| 30 | + public static void main(String[] args) { |
| 31 | + String url = "jdbc:as400://pub400.com"; |
| 32 | + |
| 33 | + Properties props = new Properties(); |
| 34 | + props.put("user", "user"); |
| 35 | + props.put("password", "password"); |
| 36 | + props.put("prompt", "true"); // login form if needed |
| 37 | + // add more properties here |
| 38 | + |
| 39 | + try (Connection conn = DriverManager.getConnection(url, props)) { |
| 40 | + System.out.println("Connected to IBM i database successfully!"); |
| 41 | + |
| 42 | + String sql = "SELECT SERVICE_NAME, EXAMPLE FROM QSYS2.SERVICES_INFO where SERVICE_CATEGORY = 'PRODUCT'"; |
| 43 | + try (PreparedStatement stmt = conn.prepareStatement(sql); |
| 44 | + ResultSet rs = stmt.executeQuery()) { |
| 45 | + |
| 46 | + while (rs.next()) { |
| 47 | + System.out.println("Service: " + rs.getString("SERVICE_NAME")); |
| 48 | + System.out.println("Example SQL: "); |
| 49 | + System.out.println(rs.getString("EXAMPLE")); |
| 50 | + System.out.println(); |
| 51 | + System.out.println(); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + System.out.println("Connection closed."); |
| 56 | + } catch (SQLException e) { |
| 57 | + System.err.println("SQL Exception: " + e.getMessage()); |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +``` |
| 63 | + |
| 64 | +## IBM Toolbox for Java JDBC properties |
| 65 | + |
| 66 | +Many properties can be specified when connecting to DB2 for IBM i using JDBC. All properties are optional and can be specified either as part of the URL or in a java.util.Properties object. If a property is set in both the URL and a Properties object, the value in the URL will be used. |
| 67 | + |
| 68 | +Driver properties can be found at [https://javadoc.io/doc/net.sf.jt400/jt400/latest/com/ibm/as400/access/AS400JDBCDriver.html](https://javadoc.io/doc/net.sf.jt400/jt400/latest/com/ibm/as400/access/AS400JDBCDriver.html) |
0 commit comments