JDBC客户端连接Starrocks 2.5



    4.0.0

    org.example
    TestStarrocksJdbc
    1.0-SNAPSHOT

    
        8
        8
        UTF-8
    

    
        
            mysql
            mysql-connector-java

            8.0.28
        
    

    
        
            
                maven-assembly-plugin
                
                    
                        jar-with-dependencies
                    
                    
                        
                            true
                            com.example.Test
                        
                    
                
                
                    
                        make-assembly
                        package
                        
                            single
                        
                    
                
            
        
    

package com.example;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test {
    private static Connection connection;

    public Test() {
    }

    public static List> starRocksRead(String sql) {
        Long start = System.currentTimeMillis();
        System.out.println("sql is: " + sql);
        try {
            List> list = new ArrayList();
            PreparedStatement statement = connection.prepareStatement(sql);
            ResultSet resultSet = statement.executeQuery();
            ResultSetMetaData rsmd = resultSet.getMetaData();
            int count = rsmd.getColumnCount();

            while(resultSet.next()) {
                Map map = new HashMap();

                for(int i = 1; i <= count; ++i) {
                    String columnName = rsmd.getColumnName(i);
                    Object value = resultSet.getObject(i);
                    map.put(columnName, value);
                }

                list.add(map);
            }
            System.out.println("result is: " + list);
            return list;
        } catch (Exception var11) {
            var11.printStackTrace();
            Long end = System.currentTimeMillis();
            System.out.println((end - start) / 60L);
            return null;
        }
    }

    public static void main(String[] args) {
        try {
            if (args.length != 5) {
                System.out.println("未指定连接地址和sql,程序自动退出");
                System.exit(1);
            }

            String ip = args[0];
            Integer port = Integer.valueOf(args[1]);
            String account = args[2];
            String password = args[3];
            String sql = args[4];

            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
            } catch (ClassNotFoundException var7) {
                throw new SQLException(var7);
            }

            String jdbcUrl = String.format("jdbc:mysql://%s:%s?connectTimeout=5000&socketTimeout=30000", ip, port);
            connection = DriverManager.getConnection(jdbcUrl, account, password);
            starRocksRead(sql);
            if (connection != null) {
                connection.close();
            }
        } catch (Exception var8) {
            var8.printStackTrace();
        }

    }
}

sql=$(cat test.sql )

java -jar ~/TestStarrocksJdbc-1.0-SNAPSHOT-jar-with-dependencies.jar 127.0.0.1 9030 username password "$sql"

你可能感兴趣的:(数据库)