教程: 使用PreparedStatement访问DLA

大家都知道PreparedStatement相比手动拼写SQL有很多好处,比如:

  • 它会自动做敏感字符的转义,防止SQL Injection攻击。
  • 它可以帮助我们动态执行SQL,Prepare一次之后,后续执行只需要替换参数就可以了。
  • 它可以帮助以OOP的方式来写SQL相关相关代码,因为我们是通过 PrepareSteatement.setXxx()的方式而不是字符串拼接的方式来设置参数。

等等,PreparedStatement的好处还有很多,更多可以参考这篇《JDBC Statement vs PreparedStatement – SQL Injection Example》, 上面说的很详细。

今天我们Data Lake Analytics也引入了对PreparedStatement的支持, 今天给大家演示一下,如何用 PreparedStatement 来访问 DLA.

准备工作

为了准备演示的环境,大家可能要先熟悉一下怎么在DLA上面创建数据库,创建表,在云栖社区上面通过关键字 DLA 可以搜索到很多文章,比如创建访问RDS的DLA库可以看这篇文章: 教程:使用Data Lake Analytics读/写RDS数据 .

我们在这篇教程里面要演示的表的结构是这样的:

CREATE EXTERNAL TABLE `type_test` (
    `id` bigint(20) NULL DEFAULT NULL COMMENT '',
    `tinyint_col` tinyint(4) NULL DEFAULT NULL COMMENT '',
    `int_col` int(11) NULL DEFAULT NULL COMMENT '',
    `char_col` char(10) NULL DEFAULT 'NULL' COMMENT '',
    `varchar_col` varchar(10) NULL DEFAULT 'NULL' COMMENT '',
    `float_col` double NULL DEFAULT NULL COMMENT '',
    `double_col` double NULL DEFAULT NULL COMMENT '',
    `decimal_col` decimal(20, 4) NULL DEFAULT NULL COMMENT '',
    `time_col` time(3) NULL DEFAULT 'NULL' COMMENT '',
    `datetime_col` datetime(6) NULL DEFAULT NULL COMMENT '',
    `timestamp_col` timestamp(6) NOT NULL COMMENT '',
    `string_col` varchar(100) NULL DEFAULT 'NULL' COMMENT '',
    `date_col` date NULL DEFAULT 'NULL' COMMENT '',
    `smallint_col` smallint(6) NULL DEFAULT NULL COMMENT '',
    `mediumint_col` int NULL DEFAULT NULL COMMENT '',
    `bigint_col` bigint(20) NULL DEFAULT NULL COMMENT ''
)
COMMENT ''

里面的数据是这样的:

> select * from type_test\G;
*************************** 1. row ***************************
           id: 1
  tinyint_col: 2
      int_col: 3
     char_col: hello1
  varchar_col: 5
    float_col: 6.01
   double_col: 7.02
  decimal_col: 8.0300
     time_col: 01:02:01.000
 datetime_col: 1986-10-01 01:02:03.000000
timestamp_col: 2018-11-29 14:04:28.305523
   string_col: hello
     date_col: 2018-09-07
 smallint_col: NULL
mediumint_col: NULL
   bigint_col: 2
*************************** 2. row ***************************
           id: 1111111
  tinyint_col: 127
      int_col: 4
     char_col: hello2
  varchar_col: 5555555555
    float_col: 9996.01
   double_col: 7777777.02
  decimal_col: 888888888.0300
     time_col: 01:02:02.000
 datetime_col: 1986-10-01 01:02:03.000000
timestamp_col: 2018-11-29 14:36:05.486738
   string_col: hello
     date_col: 2018-09-08
 smallint_col: NULL
mediumint_col: NULL
   bigint_col: 1111112
*************************** 3. row ***************************
           id: 3
  tinyint_col: 127
      int_col: 5
     char_col: hello3
  varchar_col: 5555555555
    float_col: 9997.01
   double_col: 7777777.02
  decimal_col: 888888888.0300
     time_col: 01:02:03.000
 datetime_col: 1986-10-01 01:02:03.000000
timestamp_col: 2018-11-20 10:31:40.112000
   string_col: hello
     date_col: 2018-09-09
 smallint_col: 3
mediumint_col: NULL
   bigint_col: 4
3 rows in set (0.00 sec)

Java

因为我们兼容MySQL协议,我们使用MySQL的JDBC驱动来访问DLA。值得注意的是 MySQL JDBC驱动支持客户端 PrepareStatement (也是很厉害啊), 要使用服务端的PreparedStatement的功能,需要在JDBC连接串的末尾加上 useServerPrepStmts=true 的参数,如下:

import java.sql.*;

public class DLAPrepStmtMain {
    public static void main(String[] args) throws Exception {
        Class.forName("com.mysql.jdbc.Driver");

        String sql = "select * from type_test where `key` = ?";
        try (Connection dlaConn = DriverManager.getConnection(
                "jdbc:mysql://1013022312866336-fake.cn-hangzhou.datalakeanalytics.aliyuncs.com:10000/yourdb?useServerPrepStmts=true",
                "your-username",
                "your-password");
             PreparedStatement stmt = dlaConn.prepareStatement(sql)) {
            stmt.setString(1, "key01");
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
                    System.out.print(rs.getString(i + 1) + ", ");
                }
                System.out.println();
            }
        }
    }
}

执行会发现,控制台会正确打印出结果:

1, 2, 3, hello1, 5, 6.01, 7.02, 8.03, 01:02:01, 1986-10-01 01:02:03.0, 2018-11-29 14:04:28.305, hello, 2018-09-07, null, null, 2,

Php

PHP不愧是世界上最好的语言,写起PreparedStatement起来也是非常的简洁:

<?php
$mysqli = new mysqli("1013022312866336-91e4f70e.cn-hangzhou.datalakeanalytics.aliyuncs.com:10000",
                     "oa_101302231286633637597c", "123456789qQ", "tableau_mysql");
$stmt = $mysqli->stmt_init();
$stmt->prepare("select * from type_test where id = ?");

$id = 1;
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
var_dump($result->fetch_all());
?>

运行结果:

array(1) {
  [0]=>
  array(16) {
    [0]=>
    int(1)
    [1]=>
    int(2)
    [2]=>
    int(3)
    [3]=>
    string(6) "hello1"
    [4]=>
    string(1) "5"
    [5]=>
    float(6.01)
    [6]=>
    float(7.02)
    [7]=>
    float(8.03)
    [8]=>
    string(8) "01:02:01"
    [9]=>
    string(19) "1986-10-01 01:02:03"
    [10]=>
    string(19) "2018-11-29 14:04:28"
    [11]=>
    string(5) "hello"
    [12]=>
    string(10) "2018-09-07"
    [13]=>
    NULL
    [14]=>
    NULL
    [15]=>
    int(2)
  }
}

CSharp

C#不愧是比Java更好的语言,写起来也是虎虎生风:

        public static void Main()
        {
            string connStr = "server=your-endpoint.cn-hangzhou.datalakeanalytics.aliyuncs.com;UID=your-username;database=yourdb;port=10000;password=your-password;SslMode=none";

            MySqlConnection conn = new MySqlConnection(connStr);
            try
            {
                Console.WriteLine("Connecting to MySQL...");
                conn.Open();
                string sql = "select * from type_test where id = @var1";
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                cmd.Prepare();
                cmd.Parameters.AddWithValue("@var1", 1);

                MySqlDataReader res = cmd.ExecuteReader();
                while (res.Read())
                {
                    for (int i = 0; i < res.FieldCount; i++)
                    {
                        Console.Write(res[i] + ",");
                    }
                }
                while (res.NextResult())
                {

                }
                res.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            conn.Close();
            Console.WriteLine("Done.");
        }
    }

结果如下:

Connecting to MySQL...
1,2,3,hello1,5,6.01,7.02,8.03,01:02:01,10/01/1986 01:02:03,11/29/2018 14:04:28,hello,09/07/2018 00:00:00,,,2,Done.

总结

随着DLA对于各种语言PreparedStatement的支持,大家可以用PreparedStatement替换原先手动拼SQL的代码,让你的代码更OOP,更安全!

猜你喜欢

转载自yq.aliyun.com/articles/674094