{"id":195,"date":"2019-07-12T15:00:35","date_gmt":"2019-07-12T09:30:35","guid":{"rendered":"https:\/\/techieshouts.com\/?p=195"},"modified":"2022-08-09T19:08:11","modified_gmt":"2022-08-09T13:38:11","slug":"export-teradata-table-to-csv-using-java","status":"publish","type":"post","link":"https:\/\/techieshouts.com\/home\/export-teradata-table-to-csv-using-java\/","title":{"rendered":"Export Teradata table to CSV easily in Java"},"content":{"rendered":"\n<p>In this post, we will see how we can connect to Teradata using the JDBC connection in Java using the credentials and copy the contents of the table to a CSV file.<\/p>\n\n\n\n<p><span style=\"color: #000000;\"><strong>JDBC stands for<\/strong><\/span> Java Database Connector which is nothing but a set of libraries that will have supported functions to communicate with Teradata API.<\/p>\n\n\n\n<p>The JDBC library will be different for each RDBMS that we want to connect from Java.<\/p>\n\n\n\n<p>The JARS(libraries) used in this code to connect Teradata are <strong><a href=\"https:\/\/mvnrepository.com\/artifact\/com.teradata.jdbc\" target=\"_blank\" rel=\"noopener\">tdgssconfig.jar and terajdbc4.jar<\/a><\/strong>. The below-given java program will help us to export Teradata table to a CSV file.<\/p>\n\n\n\n<h2>Export Teradata table to CSV<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">package com.tera.executor;\n\nimport java.io.File;\nimport java.io.FileNotFoundException;\nimport java.io.IOException;\nimport java.io.PrintWriter;\nimport java.sql.Connection;\nimport java.sql.DriverManager;\nimport java.sql.PreparedStatement;\nimport java.sql.ResultSet;\nimport java.sql.ResultSetMetaData;\nimport java.sql.SQLException;\n\npublic class executor {\n\n\tpublic static void main(String[] args) throws ClassNotFoundException, SQLException, IOException {\n\t\t\n\t\t\n\t\ttry {\n\t\t\tString connurl=\"jdbc:teradata:\/\/your_teradata_server_name\";\n\n\t\t\tClass.forName(\"com.teradata.jdbc.TeraDriver\");\n\t\t\tConnection conn=DriverManager.getConnection(connurl, \"username\", \"password\");\n\t\t\tString query=\"select top 5 * from databaseName.tblName\";\n\t\t\t\n\t\t\tPreparedStatement stmt=conn.prepareStatement(query);\n\t\t\tResultSet rs=stmt.executeQuery();\n\t\t\t\t\t\t\n\t\t\tString outputFile = \"C:\\\\TeraExports\\tblName.csv\";\n\t\t\tresultToCsv(rs, outputFile);\n\t\t\tSystem.out.println(\"Output file: \" + outputFile + \" is created successfully\");\n\t\t\t} \n\t\t\tcatch (Exception e) {\n\t\t\t\/\/ TODO: handle exception\n\t\t\te.printStackTrace();\n\t\t}\n\t\t\n\n\t}\n\t\n\tpublic static void resultToCsv(ResultSet rs, String outputFile) throws SQLException, FileNotFoundException {\n \n        PrintWriter csvWriter = new PrintWriter(new File(outputFile)) ;\n        ResultSetMetaData meta = rs.getMetaData() ; \n        int numberOfColumns = meta.getColumnCount() ; \n        String dataHeaders = \"\\\"\" + meta.getColumnName(1) + \"\\\"\" ; \n        for (int i = 2 ; i &lt; numberOfColumns + 1 ; i ++ ) { \n                dataHeaders += \",\\\"\" + meta.getColumnName(i).replaceAll(\"\\\"\",\"\\\\\\\"\") + \"\\\"\" ;\n        }\n        csvWriter.println(dataHeaders) ;\n        while (rs.next()) {\n            String row = \"\\\"\" + rs.getString(1).trim().replaceAll(\"\\\"\",\"\\\\\\\"\") + \"\\\"\"  ; \n            for (int i = 2 ; i &lt; numberOfColumns + 1 ; i ++ ) {\n                row += \",\\\"\" + rs.getString(i).trim().replaceAll(\"\\\"\",\"\\\\\\\"\") + \"\\\"\" ;\n            }\n        csvWriter.println(row) ;\n        }\n        csvWriter.close();\n    }\n\n}<\/pre>\n\n\n\n<p>In the above piece of code, the main place where the connection with Teradata is established at,<\/p>\n\n\n\n<p><strong>Connection conn = DriverManager.getConnection(url, username, password);<\/strong><\/p>\n\n\n\n<p>Pay attention to the classes PreparedStatement ,  ResultSetMetaData  and ResultSet. <\/p>\n\n\n\n<p><strong>PreparedStatement<\/strong>  &#8211; This class is used to pass the query to the connection object<\/p>\n\n\n\n<p><strong>ResultSetMetaData<\/strong> &#8211; This class is used to get the Meta Information about the ResultSet. The main usage of this class is to fetch the column names from the ResultSet<\/p>\n\n\n\n<p><strong>ResultSet<\/strong> &#8211; This is the object type that will be returned when we run the query<\/p>\n\n\n\n<p>The function &#8220;resultToCsv&#8221; is where the logic to export Teradata table to CSV file is written. It will receive the result set and the output file name as arguments.<\/p>\n\n\n\n<p>Also check, &#8220;<a href=\"https:\/\/techieshouts.com\/home\/export-oracle-table-to-csv-file-in-java\/\">Exporting Oracle table to CSV<\/a>&#8220;, &#8220;<a href=\"https:\/\/techieshouts.com\/home\/java-parse-sql-select-query-using-jsqlparser\/\">Parsing SQL Select query using Java<\/a>&#8220;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post, we will see how we can connect to Teradata using the JDBC connection in Java using the credentials and copy the contents of the table to a CSV file. JDBC stands for Java Database Connector which is nothing but a set of libraries that will have supported functions to communicate with Teradata\u2026 <span class=\"read-more\"><a href=\"https:\/\/techieshouts.com\/home\/export-teradata-table-to-csv-using-java\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":280,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[6,9],"tags":[110,111],"_links":{"self":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/195"}],"collection":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/comments?post=195"}],"version-history":[{"count":10,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/195\/revisions"}],"predecessor-version":[{"id":1172,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/195\/revisions\/1172"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/media\/280"}],"wp:attachment":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/media?parent=195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/categories?post=195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/tags?post=195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}