All Downloads are FREE. Search and download functionalities are using the official Maven repository.

mysql.Sample Maven / Gradle / Ivy

package mysql;

import java.sql.*;

/**
 * Sample code for EncDB MySQL JDBC driver. Refer to
 * https://help.aliyun.com/zh/rds/apsaradb-rds-for-mysql/feature-overview for
 * more detailed info.
 */
public class Sample {
    public static void main(String[] args) throws Exception {
        // prepare connection information
        String hostname = "";
        String port = "";
        String dbname = "";
        String username = "";
        String password = "";

        // Additional 3 steps you need to do.
        // Step 0. prepare MEK and ENC_ALGO
        String mek = ""; // 只是示例,建议用更复杂的密钥
        String encAlgo = "";

        // Step 1. prepare connection URL, with MEK and ENC_ALGO
        String dbUrl = String.format("jdbc:mysql:encdb://%s:%s/%s?MEK=%s&ENC_ALGO=%s", hostname, port, dbname, mek,
                encAlgo);
        // Step 2. adopt EncJDBC driver class
        Class.forName("com.alibaba.encdb.encjdbc.mysql.EncDriver");

        // do whatever just like using a conventional JDBC driver
        Connection connection = DriverManager.getConnection(dbUrl, username, password);

        int[] intData = { 1, 2, 3, 4, 5, 6 };
        String[] strData = { "abc", "bcd", "1", "def", "efg", "fgi" };

        // create table
        connection.createStatement().executeUpdate("drop table if exists test");
        connection.createStatement().executeUpdate("create table test (a int, b text)");

        // insert data
        for (int i = 0; i < 6; i++) {
            PreparedStatement pstmt = connection.prepareStatement("insert into test values (?,?)");
            pstmt.setInt(1, intData[i]);
            pstmt.setString(2, strData[i]);
            pstmt.executeUpdate();
        }

        // check plaintext data
        ResultSet rs = connection.createStatement().executeQuery("select * from test");
        while (rs.next()) {
            for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
                System.out.print(rs.getString(i + 1));
                System.out.print("\t");
            }
            System.out.print("\n");
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy