package org.springblade.sybase;

import lombok.SneakyThrows;
import org.springblade.datasource.model.TableColumnDto;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 喜欢番茄火锅
 * @date 2024/8/7 上午11:28
 */
public class JdbcMongodb {
    @SneakyThrows
    public static void main(String[] args) {
//        String uri = "jdbc:mongodb://mongodb:Platomix@mongodb123@192.168.0.251:27017/etl";
        String uri = "jdbc:sybase:Tds:192.168.0.251:5000/etl2";
        java.util.Properties p = new java.util.Properties();
//        p.setProperty("database", "etl");
        p.setProperty("user", "sa");
        p.setProperty("password", "password");
        Connection conn = DriverManager.getConnection(uri, p);
        DatabaseMetaData metaData = conn.getMetaData();
//        System.out.println(1);
//        System.out.println(getColumns(metaData, "dbo", "user_etl"));

        Statement statement = conn.createStatement();
        String sql  = "select * from employees";
        ResultSet rs = statement.executeQuery(sql);
        // 处理结果集
        while (rs.next()) {
            // 获取列值
            int id = rs.getInt("id");
            String name = rs.getString("first_name");
            // 假设还有其他列，可以继续获取

            // 输出数据
            System.out.println("ID: " + id + ", first_name: " + name);
        }

    }

    public static List<TableColumnDto> getColumns(DatabaseMetaData metaData, String schema, String tableViewName) {
        List<TableColumnDto> list = new ArrayList<TableColumnDto>();
        try {
            ResultSet rs = metaData.getColumns(null, null, tableViewName, "%");
            ResultSetMetaData md = rs.getMetaData();
            while (rs.next()) {
                for (int i = 1; i <= md.getColumnCount(); i++) {
                    System.out.println(md.getColumnName(i) + "==" + rs.getObject(i));
                }
                System.out.println("===================");
            }
            rs.close();
        } catch (Exception e) {
//            log.error(e.getMessage(), e);
        }
        return list;
    }
}
