Loading csv file to GreenPlum failed on java - java

I'm trying to import a csv file to GreenPlum DB,
It's working with the following command from psql console (9.4.0, server 8.2.15)
\COPY data."TableName" FROM 'D:/Document.csv' DELIMITER ',' CSV;
But fails when I tried to it from my java code:
public boolean InsertFile(Path tempFile, FileDetails file) {
try{
Connection conn = GetConnectionString();
String cmd = "\\COPY data.\"TableName\" FROM 'D:/Document.csv' DELIMITER ',' CSV;";
Statement st = conn.createStatement();
int res = st.executeUpdate(selectCmd);
conn.close();
return true;
}
catch(Exception e){
return false;
}
}
The exception is syntax error because of the backslash (\)

COPY comes in two separate variants, COPY and \COPY: COPY is server based, \COPY is client based. If you just try COPY without the backslash, it should work.
public static void main(String[] args) throws Exception {
String url = "jdbc:postgresql://localhost:54380/gpadmin";
Properties props = new Properties();
props.setProperty("user","gpadmin");
props.setProperty("password","changeme");
Connection conn = DriverManager.getConnection(url, props);
String cmd = "COPY timeout FROM '/tmp/timeout.csv' DELIMITER ',' CSV;";
Statement st = conn.createStatement();
int res = st.executeUpdate(cmd);
System.out.println(res);
conn.close();
}
This worked perfectly for me.

Related

run java program in debian 10 command line with external jar

I'm using postgresql-42.2.23.jar in my following program.i have tested the code in windows environment in intelij ide and it is working fine.But i want to run it in the linux machine( which runs debian 10)using the comand line.Right now postgresql-42.2.23.jar file and java class files are in the Music folder.
when i compile the program with
"chathu#giottestserver:~/Music$ javac -cp Music/postgresql-42.2.23.jar serverinsert.java"
command,it compiles and create the class file.
But when i run it with
"chathu#giottestserver:~/Music$ java -cp .:Music/postgresql-42.2.23.jar:serverinsert"
it gives following messageenter image description here
I have tried with following command also.But no output.
" chathu#giottestserver:~/Music$ java -cp .:Music/postgresql-42.2.23.jar serverinsert"
can anyone help me to solve this?
import java.sql.*;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class serverinsert {
public static void main(String []V){
Connection c = null;
Statement stmt = null;
try
{
//Connecting to the database in local machine
Class.forName("org.postgresql.Driver");
c = DriverManager
.getConnection("jdbc:postgresql://localhost/",
"postgres", "gflow123");
System.out.println("Opened database successfully");
//Creation of a table to insert data in the local machine
stmt = c.createStatement();
String sql = "CREATE TABLE jason " +
"(ID INT NOT NULL," +
" sendata json NOT NULL)";
stmt.executeUpdate(sql); //updates the table
System.out.println("Table created successfully");
//Opening connection for DLC client
ServerSocket ss = new ServerSocket(4000);
Socket s = ss.accept();
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line;
// DataOutputStream Dout = new DataOutputStream(s.getOutputStream());
// BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//String MsgIn="",MsgOut="";
// inserting the DLC Data into a string variable called content
String content="";
while((line = br.readLine()) != null){
sb.append(line).append(System.lineSeparator());
content = sb.toString();
}
//Removing the http header content from the DLC Data
String jsn=content.substring(content.indexOf("{"));
//DLC received data processing
System.out.println(jsn);
String jsonreplacd=jsn.replace("[{\"name","\"name");
System.out.println(jsonreplacd);
String jsonreplacd1=jsonreplacd.replace("\"name","[\"INSERT INTO jason (ID,sendata) \" + \"VALUES ('1','')");
System.out.println(jsonreplacd1);
String jsonreplacd2=jsonreplacd1.replace("')\":\"press\",\"datatype\":\"float\",\"content\":","");
System.out.println(jsonreplacd2);
String jsn2=jsonreplacd2.substring(jsonreplacd2.indexOf("["));
System.out.println(jsn2);
String jsn3=jsn2.replace("[{","{");
System.out.println(jsn3);
String jsn4=jsn3.replace("\"t\"","\\\"t\\\"");
System.out.println(jsn4);
String jsn5=jsn4.replace("\"v\"","\\\"v\\\"");
System.out.println(jsn5);
String jsn6=jsn5.replace(":",":\\\"");
System.out.println(jsn6);
String jsn7=jsn6.replace(",\\","\\\",\\");
System.out.println(jsn7);
String jsn8=jsn7.replace("},","\\\"},");
System.out.println(jsn8);
String jsn9=jsn8.replace("}]}]}}","\\\"}')\"");
System.out.println(jsn9);
String jsn10=jsn9.replace("[","");
System.out.println(jsn10);
String jsn11=jsn10.replace("},{","}')\",\"INSERT INTO jason (ID,sendata) \" + \"VALUES ('1','{");
System.out.println(jsn11);
String jsn12=jsn11.replace("\",\"","\"#\"");
System.out.println(jsn12);
// String jsn3=jsn2.replace("\"","\\\"");
//
//
// String jsn6=jsn5.replace(",","\\\",");
// String jsn7=jsn6.
//
// String jsn9=jsn8.replace("}\\\"","}");
// System.out.println(jsn9);
// Creation of string array type to execute sql command
//String[] strArray = new String[] {jsn11};
//System.out.println(strArray[0]);
//String hi="hi";
//String[] data = {"INSERT INTO jason (ID,sendata) " + "VALUES ('1','{\"t\":\"2104492460\",\"v\":\"-0.000769\"}')"};
String[] data = jsn12.split("#");
for(int i=0;i<5;i++) {
//stmt.executeUpdate(data[i]);
System.out.println(data[i]);
}
s.close();
}
catch(Exception e){
}
}
}
You are trying to run application with postgresql-42.2.23.jar dependency.
"javac -cp Music/postgresql-42.2.23.jar serverinsert.java" - works, as syntax is correct
"java -cp .:Music/postgresql-42.2.23.jar:serverinsert" - does not work, because serverinsert is not a .jar file (see also this)
"java -cp .:Music/postgresql-42.2.23.jar serverinsert" - normally it shall work, but you did not post your "serverinsert" code. Instead we have "writedbtst", which I believe is your entry point (method main) and you shall run it instead of "serverinsert". You could save it as "serverinsert.java" if it was not public.
To sum up, If it works in your IDE, try running it on the same platform using command line and replacing main class accordingly. And please follow java class naming conventtion.

Exception: Operating System error code 3

I am doing text file bulk upload in SQL Server. Whenever i tries to upload the files, gets Following Exception:
[Microsoft] [ODBC SQL Server Driver] [SQL Server]Could not bulk insert
because file 'C:/Form/Input_File/Form.txt' could not be opened.
Operating System error code 3(The system cannot find the path
specified).
Please find the below code:
public void uploadFiles()
{
File dir = new File(inputFilesPath);
String[] children = dir.list();
String completePathFileName = "";
System.out.println(" Inside Upload ::");
String saveFileNames = "";
PreparedStatement prepStat;
DBConnection dbConnection=new DBConnection();
Connection conHandler= dbConnection.getConnection();
if(null!=conHandler)
System.out.println(" Clear ::"+conHandler);
try
{
if (children != null)
{
for (int i = 0; i < children.length; i++)
{
String filename = children[i];
System.out.println(" children[i]::"+children[i]);
// File is validated based on some business rules.
if (isValidFile(filename) == 1)
{
String[] fileSplit = filename.split("E");
String[] extnSplit = fileSplit[1].trim().split(".TXT");
completePathFileName += (completePathFileName.equals(""))
? extnSplit[0] : "^" + extnSplit[0];
saveFileNames += (saveFileNames.equals(""))
? filename : "," + filename;
System.out.println(extnSplit[0]);
}
else
{
inValidFileNames += (inValidFileNames.equals(""))
? filename : ";\n" + filename;
}
}
if (!completePathFileName.trim().equals(""))
{
System.out.println(completePathFileName);
prepStat = conHandler.prepareStatement("Exec StartFileImport ?");
prepStat.setString(1, completePathFileName);
prepStat.execute();
saveFileNameToDatabase(saveFileNames);
}
}
}
catch (SQLException sql)
{
System.out.println(sql.getMessage());
}
}
Getting Connection Object from the below code:
public Connection getConnection()
{
System.out.println("In side DB Connection...");
try{
// get a database connection
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Before Driver");
conn= DriverManager.getConnection("jdbc:odbc:form26qa","form26","form26");
System.out.println("After Driver");
if(conn != null)
{
System.out.println("Connection established...");
}//if
else
{
System.out.println("Connection failed...");
}//else
}//try
catch(Exception e)
{
System.out.println("Exception ocurred..."+e);
e.printStackTrace();
}//catch
return conn;
}
Explanation:
I am reading files from the input path and tried to get the fileName and file path and uploading file into SQL Server.
Application is able to find the input file in the specified path. while uploading i am getting the above mentioned Exception
Please check and suggest me to fix the issue.
The file needs to be accessable on the server. The file path is
relative to the server, not your PC. Also, if you are trying to use a
share or a mapped drive it will not work. You need to use the UNC
path.
UNC Name Examples
\\teela\admin$ (to reach C:\WINNT)
\\teela\admin$\system32 (to reach C:\WINNT\system32)
\\teela\temp (to reach C:\temp)

Making Oracle DB connection Dynnamically in JSP

Connecting to database dynamically jsp
Hi, I'm trying to make connection to database dynamically.
So when user clicks link from index page, it will send parameter "OS"
so my test page will receive parameter OS, looks for matchs in textfile that has list of database information like
XP-jdbc:oracle:thin#xx.xxx.xx.xx:xxxx:XPXP1-XP_user-XP_pass
W7-jdbc:oracle:thin#YY.YYY.YY.YY:YYYY:W7W71-W7_user-W7_pass
MAC-jdbc:oracle:thin#ZZ.ZZZ.ZZ.ZZ:ZZZZ:MACO1-MAC_user-MAC_pass
LINNUX-jdbc:oracle:thin#AA.AAA.A.AA:AAAA:LINN1-LINNUX_user-LINNUX_ph1
my attempt:
String userName = request.getParameter("OS");
try{
String db = "";
String[] temp1;
String dblist = root + "\\" + "dblist.txt";
BufferedReader dbin = new BufferedReader(new FileReader(dblist));
while ((db = dbin.readLine()) != null){
temp1=db.split("-");
if ((temp1[0].equals(userName))){
connString = temp1[1].toString();
connUser = temp1[2].toString();
connPass = temp1[3].toString();
}
}
dbin.close();
}catch (IOException ex) {
System.out.println(ex);
}
try{
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
Connection conn = DriverManager.getConnection(connString, connUser, connPass);
Statement stmt = conn.createStatement();
}
My problem is, this doesn't work!
I get java.sql.SQLException: Invalid Oracle URL specified when i open my web page....
What did i have wrong?
Apparently my property file was corrutped >.> that was the reason why my property file only read half of it's components...thanks for your help anyways
you could use Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); instead of DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
Also,have you checked if YY.YYY.YY.YY:YYYY is replaced by proper IP and port?

hxtt DBF driver locks its files

My web app receives archive, unpacks it to temp folder, reads data from extracted DBFs and then should kill garbage. Though it fails to kill temp folder since DBF files in it are locked. Here is a sample code:
public static void main( String a[] ) throws Exception
{
Class.forName( "com.hxtt.sql.dbf.DBFDriver" ).newInstance();
String url = "jdbc:DBF:/C:/TEMP/";
Properties properties = new Properties();
properties.setProperty( "charSet", "cp866" );
Connection con = null;
Statement st = null;
java.sql.Driver d = null;
con = DriverManager.getConnection( url, properties );
d = DriverManager.getDriver( url );
st = con.createStatement();
ResultSet rs = st.executeQuery( "SELECT * FROM 6QQQ201010" );
rs.close();
st.close();
con.close();
}
I put breakpoint past last line and 6QQQ201010.DBF is still locked. Any ideas? Or just a bug in the driver?
Add properties.setProperty( "delayedClose", "0" ); and driver would close handles imediately.

Difference in Reading .CSV file in UNIX System & Windows System

I have created a JSP code where we can upload a .csv file. The JSP Code is supported by a java code that reads the .csv file and compares the urls in the file with the DB and adds it into to the DB if the urls are not already present.
The above scenario works absolutely fine when its executed in a windows system.
I uploaded the succesfully executed web application folder to a unix system. When I executed the program in the UNIX system, the tool is not comparing the URLs with the DB and adds it.
I suspect there should be some problem in reading the .csv file in a UNIX sytem.
Am using fedora(linux) OS. Kindly let me know whether there is any differences in reading .csv file between a windows system and a unix system.
The .csv file I am using has the following contents,
http://www.topix.com,sdfasdf
http://rss.news.yahoo.com/rss/topstories,Apple
http://www.apple.com/354,sdfasdf
http://www.topix.com/rss/city/emporia-ks,sdfasdf
http://www.topix.com/rss/,sdfasdf
http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topsongs/limit=10/xml,sdfasdf
http://www.topix.com/rss/city/emp,sdfasdf
http://www.topix.com/rss/city/sandy-ut,dfgsdfg
http://www.apple.com,Yahoo
UPDATE FOR JEFF
try {
List items = uploadHandler.parseRequest(request);
Iterator itr = items.iterator();
while(itr.hasNext()) {
FileItem item = (FileItem) itr.next();
if(item.isFormField()) {
out.println("File Name = "+item.getFieldName()+", Value = "+item.getString());
} else {
File file = new File(destinationDir,item.getName());
item.write(file);
//String temp=item.getName();
String fileToBeRead = "C:/Program Files/Apache Software Foundation/Tomcat 5.5/webapps/Readcsv/files/"+item.getName();
String urlcnt="";
String srccnt="";
String contentType="";
Connection con=null;
Statement stmt=null;
final String rssvar="Rss";
final String other="Other";
int i=0;
int j=0;
try {
BufferedReader br = new BufferedReader(new FileReader(fileToBeRead));
String strLine = "";
StringTokenizer st = null;
while( (strLine = br.readLine()) != null)
{
st = new StringTokenizer(strLine, ",");
while(st.hasMoreTokens()){
urlcnt=st.nextToken();
srccnt=st.nextToken();
}
if(con==null){
SQLConnection.setURL("jdbc:sqlserver://192.168.2.53\\SQL2005;user=sa;password=365media;DatabaseName=LN_ADWEEK");
con=SQLConnection.getNewConnection();
stmt=con.createStatement();
}
try{
ResultSet rs;
boolean hasRows=false;
rs=stmt.executeQuery("select url from urls_linkins where url='"+urlcnt+"'");
while(rs.next()){
hasRows=true;
i++;
}
if(!hasRows){
j++;
URL url = new URL(urlcnt);
URLConnection url1=url.openConnection();
contentType=url1.getContentType();
PreparedStatement insertUrlStatement = con.prepareStatement("INSERT INTO urls_linkins(url, source_name, is_active, is_periodic, Link_Type, New_Entry) VALUES(?, ?, ?, ?, ?, ?)");
if(contentType.contains("rss") || contentType.contains("xml"))
{
insertUrlStatement.setString(1, urlcnt);
insertUrlStatement.setString(2, srccnt);
insertUrlStatement.setInt(3, 1);
insertUrlStatement.setInt(4, 0);
insertUrlStatement.setString(5, rssvar);
insertUrlStatement.setInt(6, 1);
insertUrlStatement.executeUpdate();
insertUrlStatement.close();
}
else{
insertUrlStatement.setString(1, urlcnt);
insertUrlStatement.setString(2, srccnt);
insertUrlStatement.setInt(3, 1);
insertUrlStatement.setInt(4, 0);
insertUrlStatement.setString(5, other);
insertUrlStatement.setInt(6, 1);
insertUrlStatement.executeUpdate();
insertUrlStatement.close();
}
}
}
catch(Exception e){
e.printStackTrace();
}
}
}catch(Exception e){
e.printStackTrace();
}finally{
out.println("<h2>"+j+" url has been added and "+i+" url already exists in the DB</h2>");
out.println("<a href=Addurl.jsp>Check URL</a>");
out.println("<a href=Addurl1.jsp>Add Single URL</a>");
out.println("<a href=uploadcsv.jsp>Add Multiple URL</a>");
}
}
out.close();
}
}catch(FileUploadException ex) {
log("Error encountered while parsing the request",ex);
} catch(Exception ex) {
log("Error encountered while uploading file",ex);
}
This is my reading code of the .csv file.
Yes there will be differences in reading the .csv file when you transfer from a windows machine to a unix machine even when it's a text file. There are hidden space characters which may be represented differently on the unix machine.
I suspect that the reason it is not comparing the URLs is because the space characters might be different ASCII values so it thinks they are different and adds the URL into the DB.
One suggestion would be to use the dos2unix command.
http://kb.iu.edu/data/acux.html
Hope it helps.

Categories