Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Table of Contents

Introduction

This sample demonstrates how to use databases  and database users created through WSO2 Storage Server, in a typical Java application that accesses a database via JDBC.

Building sample application

...

  1. Extract the JDBC URL of the created database. 

...

  1. Set the user name and the password of a user who is attached to the database.

...

  1. Select appropriate JDBC Driver class based on the backend RDBMS.

    Code Block
    languagejava
    Connection connection = null;
    
    String driverClass = "com.mysql.jdbc.Driver";
    String dbUser =  "user123";
    String dbPassword = "user123";
    String jdbcUrl = "jdbc:mysql://localhost:3306/testDB";
    
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName(driverClass);
    ds.setUrl(jdbcUrl);
    ds.setUsername(dbUser);
    ds.setPassword(dbPassword);
    
    try {
        connection = ds.getConnection(); 
        //your code goes here
    } catch (SQLException e) {
       log.error(e);
    } finally {
       try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            log.error(e);
        }
    }