package com.chenyang.nse.bussiness.tools.jdbc;

import com.chenyang.nse.bussiness.entity.db.PrimaryKeyInfo;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OceanbaseTool {
   public static void main(String[] args) {
      String url = "jdbc:oceanbase://t1uk3n8v6d21smo.oceanbase.aliyuncs.com:1521/oracle_pkdl?useUnicode=true&characterEncoding=utf-8";
      String username = "oracle_pkdl";
      String password = "Root123456";
      Connection conn = null;
      DatabaseMetaData meta = null;
      PreparedStatement ps = null;
      ResultSet rs = null;
      String schema = "oracle_pkdl";
      String table = "ASTUDENTS";
      List<PrimaryKeyInfo> list = new ArrayList();

      try {
         Class.forName("com.alipay.oceanbase.obproxy.mysql.jdbc.Driver");
         conn = DriverManager.getConnection(url, username, password);
         String querySql = "Select  * from all_constraints con,all_cons_columns col where con.constraint_name=col.constraint_name and con.constraint_type='P' and col.table_name='" + table.toUpperCase() + "' and con.owner = '" + schema.toUpperCase() + "'";
         ps = conn.prepareStatement(querySql);
         rs = ps.executeQuery();

         while(rs.next()) {
            String tableschem = rs.getString("OWNER");
            String tablename = rs.getString("TABLE_NAME");
            String columnname = rs.getString("COLUMN_NAME");
            String keyseq = rs.getString("INDEX_NAME");
            String pkname = rs.getString("CONSTRAINT_NAME");
            PrimaryKeyInfo temp = new PrimaryKeyInfo((String)null, tableschem, tablename, columnname, keyseq, pkname);
            list.add(temp);
         }

         ps = conn.prepareStatement("select to_char(sysdate,'yyyy-MM-dd HH24:mi:ss') from dual;");
         rs = ps.executeQuery();
         rs.next();
         System.out.println("sysdate is:" + rs.getString(1));
         rs.close();
         ps.close();
      } catch (Exception e) {
         e.printStackTrace();
      } finally {
         if (null != conn) {
            try {
               conn.close();
            } catch (SQLException e) {
               e.printStackTrace();
            }
         }

      }

   }
}
