Saturday, June 18, 2016
PostGre SQL Installation steps Windows
Download software from below URL
Follow the below steps to install Postgre
After the installation, you see the folders as follows
Connecting to the postgre database
Thursday, June 16, 2016
Export Oracle table into Excel Using SQLPlus Spool command
The following code exports data from dba_tables table into .CSV file
After the table export into .CSV file, if we open the file with Excel we can see each table column data into separate columns in excel.
set term off
set echo off
set underline off
set colsep ,
set linesize 100
set pagesize 0
set lines 1000 pages 1000
set trimspool on
set feedback off
set heading on
set newpage 0
set headsep off
spool h:\tmp\table_export_into_file.csv
-- Exporing the table dba_tables into csv file
select owner, table_name, tablespace_name
from dba_tables
where rownum <=10;
spool off
Output looks like as follows
After the table export into .CSV file, if we open the file with Excel we can see each table column data into separate columns in excel.
set term off
set echo off
set underline off
set colsep ,
set linesize 100
set pagesize 0
set lines 1000 pages 1000
set trimspool on
set feedback off
set heading on
set newpage 0
set headsep off
spool h:\tmp\table_export_into_file.csv
-- Exporing the table dba_tables into csv file
select owner, table_name, tablespace_name
from dba_tables
where rownum <=10;
spool off
Output looks like as follows
Tuesday, June 14, 2016
Netezza JDBC Program
Netezza JDBC Program
Steps to connect to Netezza Database using Java (JDBC) program
Download the Netezza driver nzjdbc.jar
Connect to Eclipse and upload the nzjdbc.jar using "Build Path" as shown in below images
Java Class to connect to Netezza Database and read table data
---------------------------------------------------
package sample;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcNetezzaConnection {
public static void main(String[] args) {
Connection conn1 = null;
Statement statement = null;
try {
Class.forName("org.netezza.Driver");
conn1 = DriverManager.getConnection("jdbc:netezza://servername/databasename", "username","password");
if (conn1 != null) {
System.out.println("Connected with connection #1");
}
// Create the statement to be used to get the results.
statement = conn1.createStatement();
// Create a query to use.
String query = "select tablename, owner from _v_table where owner = 'ADMIN' limit 10";
ResultSet resultSet = statement.executeQuery(query);
System.out.println("Printing result...");
while (resultSet.next()) {
String tn = resultSet.getString("tablename");
String opg = resultSet.getString("owner");
System.out.println("\tablename: " + tn + ", owner: " + opg );
}
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
try {
if (conn1 != null && !conn1.isClosed()) {
conn1.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
}
----------------------------------------------------
Output of the above class
Output from Netezza Database
Steps to connect to Netezza Database using Java (JDBC) program
Download the Netezza driver nzjdbc.jar
Connect to Eclipse and upload the nzjdbc.jar using "Build Path" as shown in below images
Java Class to connect to Netezza Database and read table data
---------------------------------------------------
package sample;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcNetezzaConnection {
public static void main(String[] args) {
Connection conn1 = null;
Statement statement = null;
try {
Class.forName("org.netezza.Driver");
conn1 = DriverManager.getConnection("jdbc:netezza://servername/databasename", "username","password");
if (conn1 != null) {
System.out.println("Connected with connection #1");
}
// Create the statement to be used to get the results.
statement = conn1.createStatement();
// Create a query to use.
String query = "select tablename, owner from _v_table where owner = 'ADMIN' limit 10";
ResultSet resultSet = statement.executeQuery(query);
System.out.println("Printing result...");
while (resultSet.next()) {
String tn = resultSet.getString("tablename");
String opg = resultSet.getString("owner");
System.out.println("\tablename: " + tn + ", owner: " + opg );
}
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
try {
if (conn1 != null && !conn1.isClosed()) {
conn1.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
}
----------------------------------------------------
Output of the above class
Output from Netezza Database
Redshift JDBC Program
Redshift JDBC Program
Steps to connect Redshift database using Java JDBC program
Download the driver from
JDBC 4.1–compatible
driver: https://s3.amazonaws.com/redshift-downloads/drivers/RedshiftJDBC41-1.1.10.1010.jar
The class name for this
driver is
com.amazon.redshift.jdbc41.Driver.
Connect to Eclipse, upload the Redshift JDBC driver using "Build Path" -> Add External Archieves as shown like in below images
The following is the Java class to read data from Redshift table
-----------------------------------------------------------
package sample;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcRedshiftConnection {
public static void main(String[] args) {
Connection conn1 = null;
Statement statement = null;
try {
Class.forName("com.amazon.redshift.jdbc41.Driver");
conn1 = DriverManager.getConnection("jdbc:redshift://servername:port/databasename", "username","password");
if (conn1 != null) {
System.out.println("Connected with connection #1");
}
// Create the statement to be used to get the results.
statement = conn1.createStatement();
// Create a query to use.
String query = "select schemaname, tablename from pg_table_def limit 10";
ResultSet resultSet = statement.executeQuery(query);
System.out.println("Printing result...");
while (resultSet.next()) {
String tn = resultSet.getString("schemaname");
String opg = resultSet.getString("tablename");
System.out.println("\t schemaname: " + tn + ", tablename: " + opg );
}
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
try {
if (conn1 != null && !conn1.isClosed()) {
conn1.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
}
------------------------------------------------------------
Sample output from the above Java Class
Sample output from Redshift Database
Oracle JDBC Program
Oracle JDBC Program
Steps to connect to Oracle Database using Java (JDBC)
Steps to connect to Oracle Database using Java (JDBC)
Download the Oracle JDBC driver ojdbc6.jar from
Go to eclipse and
Choose “ojdbc6.jar” and upload the file using above.
Create a java class as follows
-------------------------------------------------
package sample;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcOracleConnection {
public static void main(String[] args) {
Connection conn1 = null;
Statement statement = null;
try {
Class.forName("oracle.jdbc.OracleDriver");
String dbURL1 = "jdbc:oracle:thin:username/password@localhost:1521:XE";
conn1 = DriverManager.getConnection(dbURL1);
if (conn1 != null) {
System.out.println("Connected with connection #1");
}
statement = conn1.createStatement();
// Create a query to use.
String query = "select table_name, owner from dba_tables where rownum <=10";
ResultSet resultSet = statement.executeQuery(query);
System.out.println("Printing result...");
while (resultSet.next()) {
String tn = resultSet.getString("table_name");
String opg = resultSet.getString("owner");
System.out.println("\ttable_name: " + tn +", owner: " + opg );
}
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
try {
if (conn1 != null && !conn1.isClosed()) {
conn1.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
}
-----------------------------------------------------
The sample output of class JdbcOracleConnection is as follows
Tuesday, June 7, 2016
Subscribe to:
Posts (Atom)