Backslash not working for postgres query from jdbc [duplicate] - java

Unable to use copy command with jdbc Postgres. Whats wrong with the below code snippet sample.
public boolean loadReportToDB(String date) {
// TODO Auto-generated method stub
Connection connection = DBUtil.getConnection("POSTGRESS");
String fileName = "C:/_0STUFF/NSE_DATA/nseoi_" + date + ".csv";
String sql = "\\copy fno_oi FROM 'C:\\_0STUFF\\NSE_DATA\\nseoi_27102017.csv' DELIMITER ',' CSV header";
try {
PreparedStatement ps = connection.prepareStatement(sql);
System.out.println("query"+ps.toString());
int rowsaffected = ps.executeUpdate();
System.out.println("INT+" + rowsaffected);
return true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
org.postgresql.util.PSQLException: ERROR: syntax error at or near "\"
Position: 1
at org.
if we use
String sql = "copy fno_oi FROM 'C:\\_0STUFF\\NSE_DATA\\nseoi_27102017.csv' DELIMITER ',' CSV header";
then no rows are updated
postgres version postgresql-10.0-1-windows-x64

This works for me:
try (Connection conn = DriverManager.getConnection(connUrl, myUid, myPwd)) {
long rowsInserted = new CopyManager((BaseConnection) conn)
.copyIn(
"COPY table1 FROM STDIN (FORMAT csv, HEADER)",
new BufferedReader(new FileReader("C:/Users/gord/Desktop/testdata.csv"))
);
System.out.printf("%d row(s) inserted%n", rowsInserted);
}
Using copyIn(String sql, Reader from) has the advantage of avoiding issues where the PostgreSQL server process is unable to read the file directly, either because it lacks permissions (like reading files on my Desktop) or because the file is not local to the machine where the PostgreSQL server is running.

As your input file is stored locally on the computer running your Java program you need to use the equivalent of copy ... from stdin in JDBC because copy can only access files on the server (where Postgres is running).
To do that use the CopyManager API provided by the JDBC driver.
Something along the lines:
Connection connection = DBUtil.getConnection("POSTGRES");
String fileName = "C:/_0STUFF/NSE_DATA/nseoi_" + date + ".csv";
String sql = "copy fno_oi FROM stdin DELIMITER ',' CSV header";
BaseConnection pgcon = (BaseConnection)conection;
CopyManager mgr = new CopyManager(pgcon);
try {
Reader in = new BufferedReader(new FileReader(new File(fileName)));
long rowsaffected = mgr.copyIn(sql, in);
System.out.println("Rows copied: " + rowsaffected);
} catch (SQLException e) {
e.printStackTrace();
}

Related

Append row in Sql without replacing previous row java

So i have database with value like this...
i'm trying to append the value by using insert into without replacing it,the data from this txt file...
but when i reload/refresh the database there is no new data being appended into the database...,
here is my code....
public static void importDatabase(String fileData){
try{
File database = new File(fileData);
FileReader fileInput = new FileReader(database);
BufferedReader in = new BufferedReader(fileInput);
String line = in.readLine();
line = in.readLine();
String[] data;
while (line != null){
data = line.split(",");
int ID = Integer.parseInt(data[0]);
String Nama = data[1];
int Gaji = Integer.parseInt(data[2]);
int Absensi = Integer.parseInt(data[3]);
int cuti = Integer.parseInt(data[4]);
String Status = data[5];
String query = "insert into list_karyawan values(?,?,?,?,?,?)";
ps = getConn().prepareStatement(query);
ps.setInt(1,ID);
ps.setString(2,Nama);
ps.setInt(3,Gaji);
ps.setInt(4,Absensi);
ps.setInt(5,cuti);
ps.setString(6,Status);
line = in.readLine();
}
ps.executeUpdate();
ps.close();
con.close();
System.out.println("Database Updated");
in.close();
}catch (Exception e){
System.out.println(e);
}
}
When i run it, it shows no error but the data never get into database, where did i go wrong?.,...
Auto-commit mode is enabled by default.
The JDBC driver throws a SQLException when a commit or rollback operation is performed on a connection that has auto-commit set to true.
Symptoms of the problem can be unexpected application behavior
update the JVM configuration for the ActiveMatrix BPM node to use the following Oracle connection property:
autoCommitSpecCompliant=false Try once
Note:I am not able to put as comment so i posted as a answer

Correct way to use copy Postgres jdbc

Unable to use copy command with jdbc Postgres. Whats wrong with the below code snippet sample.
public boolean loadReportToDB(String date) {
// TODO Auto-generated method stub
Connection connection = DBUtil.getConnection("POSTGRESS");
String fileName = "C:/_0STUFF/NSE_DATA/nseoi_" + date + ".csv";
String sql = "\\copy fno_oi FROM 'C:\\_0STUFF\\NSE_DATA\\nseoi_27102017.csv' DELIMITER ',' CSV header";
try {
PreparedStatement ps = connection.prepareStatement(sql);
System.out.println("query"+ps.toString());
int rowsaffected = ps.executeUpdate();
System.out.println("INT+" + rowsaffected);
return true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
org.postgresql.util.PSQLException: ERROR: syntax error at or near "\"
Position: 1
at org.
if we use
String sql = "copy fno_oi FROM 'C:\\_0STUFF\\NSE_DATA\\nseoi_27102017.csv' DELIMITER ',' CSV header";
then no rows are updated
postgres version postgresql-10.0-1-windows-x64
This works for me:
try (Connection conn = DriverManager.getConnection(connUrl, myUid, myPwd)) {
long rowsInserted = new CopyManager((BaseConnection) conn)
.copyIn(
"COPY table1 FROM STDIN (FORMAT csv, HEADER)",
new BufferedReader(new FileReader("C:/Users/gord/Desktop/testdata.csv"))
);
System.out.printf("%d row(s) inserted%n", rowsInserted);
}
Using copyIn(String sql, Reader from) has the advantage of avoiding issues where the PostgreSQL server process is unable to read the file directly, either because it lacks permissions (like reading files on my Desktop) or because the file is not local to the machine where the PostgreSQL server is running.
As your input file is stored locally on the computer running your Java program you need to use the equivalent of copy ... from stdin in JDBC because copy can only access files on the server (where Postgres is running).
To do that use the CopyManager API provided by the JDBC driver.
Something along the lines:
Connection connection = DBUtil.getConnection("POSTGRES");
String fileName = "C:/_0STUFF/NSE_DATA/nseoi_" + date + ".csv";
String sql = "copy fno_oi FROM stdin DELIMITER ',' CSV header";
BaseConnection pgcon = (BaseConnection)conection;
CopyManager mgr = new CopyManager(pgcon);
try {
Reader in = new BufferedReader(new FileReader(new File(fileName)));
long rowsaffected = mgr.copyIn(sql, in);
System.out.println("Rows copied: " + rowsaffected);
} catch (SQLException e) {
e.printStackTrace();
}

JTDS not connecting to correct database

I am using JTDS to connect to a MS SQL Server. Connection to the database is no problem, but when I try to execute a statement, I get a Database 'java' does not exist exception.
ConnectionString:
conn = DriverManager.getConnection("jdbc:jtds:sqlserver://localhost;DatabaseName=MyDatabase;user=testuser;password=testpassword");
Trying to execute the script:
private void runStatement(String scriptLocation) {
if(scriptLocation == null) {
return;
}
try {
InputStream is = getClass().getClassLoader().getResourceAsStream(scriptLocation);
String query = is.toString();
is.close();
Statement stmt = conn.createStatement();
stmt.executeQuery(query);
} catch(IOException | SQLException ex) {
log.warning(ex.getMessage());
}
}
Stacktrace:
WARNING: Database 'java' does not exist. Make sure that the name is entered correctly.
java.sql.SQLException: Database 'java' does not exist. Make sure that the name is entered correctly.
at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:372)
at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2988)
at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2421)
at net.sourceforge.jtds.jdbc.TdsCore.getMoreResults(TdsCore.java:671)
at net.sourceforge.jtds.jdbc.JtdsStatement.executeSQLQuery(JtdsStatement.java:505)
at net.sourceforge.jtds.jdbc.JtdsStatement.executeQuery(JtdsStatement.java:1427)
at com.exampe.MyJTDSConnection.runStatement(MyJTDSConnection.java:238)
at com.exampe.MyJTDSConnection.loadPageTitle(MyJTDSConnection.java:208)
at com.exampe.MyJTDSConnection.runTesting(MyJTDSConnection.java:69)
at com.exampe.SeleniumTesting.runTest(SeleniumTesting.java:59)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
...
As mentioned in the comment to your question, applying the .toString() method to the InputStream object does not read the InputStream. Instead it just returns a String representation of the object itself, not what the object contains.
For example, my Java project has a resource file named "script.sql" that contains:
SELECT ##VERSION
The following code compares the result of simply doing a .toString() on the object vs. using Apache Commons IO to actually read the InputStream into a String:
package resourceTest;
import java.io.InputStream;
import org.apache.commons.io.IOUtils;
public class ResourceTestMain {
public static void main(String[] args) {
try (InputStream is = ResourceTestMain.class.getClassLoader().getResourceAsStream("resources/script.sql")) {
String toStringValue = is.toString();
String contents = IOUtils.toString(is, "UTF-8");
is.close();
System.out.println("is.toString() returned:");
System.out.println(" " + toStringValue);
System.out.println();
System.out.println("IOUtils.toString(is, \"UTF-8\") returned:");
System.out.println(" " + contents);
} catch (Exception e) {
e.printStackTrace(System.out);
}
}
}
The results are:
is.toString() returned:
java.io.BufferedInputStream#804a77
IOUtils.toString(is, "UTF-8") returned:
SELECT ##VERSION

MySQL DATA Truncation Error occuring from Java application but not directly on MySQL

I am using the JDBC driver to connect to a mysql database and using the "LOAD DATA INFILE" command in my java application to load(insert) a text file into the database. I am getting the following error: Data truncation: Data too long for column xxx at row 1.
However if I load the same text file manually by logging into the database and entering the SQL manually, the data loads fine.
Can someone pelase tell me what the error might be?
I am running this on Red Hat Enterprise Linux Server release 5.8 and the jdk version is 1.5.0_16 if that helps
This is the function used to load the data
public static void loaddata(Connection conn, String filename, String tablename)
{
try{
Statement stmt = null;
stmt = conn.createStatement();
File file = new File(filename);
file.getAbsolutePath().replace("\\", "\\\\");
String cmd = "LOAD DATA INFILE '"
+ file.getAbsolutePath().replace("\\", "\\\\")
+ "' INTO TABLE " + tablename + " FIELDS TERMINATED BY \'^\'";
stmt.executeUpdate(cmd);
System.out.println("cmd :" + cmd);
}
catch(SQLException sqle){
sqle.printStackTrace();
}
}
This is the function to create the JDBC connection:
public static Connection createConnection()
{
Connection conn=null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
}
catch(Exception e) {
e.printStackTrace();
}
try {
String url = ""; //URL mentioned in the actual code
conn = DriverManager.getConnection(url, user, pass);
} catch (SQLException sqe1) {
sqe1.printStackTrace();
}
return conn;
}

How can I run a script file using the "source" command?

I am coding an application that connects to a localhost. When the application first runs I want it to initialize the Database using this method:
public void initDataBase() {
try {
Statement stm = con.createStatement();
stm.executeQuery("source shippingSQLscript.sql");
} catch (SQLException ex) {
ex.printStackTrace();
}
}
Where shippingSQLscript.sql contains the correct sql statements to insert all the data. However when I run it the method throws:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:
You have an error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near 'source shippingSQLscript.sql'
at line 1
I have tried using stm.execute() as well but have had the same result.
You cannot do this with the JDBC driver. source is only a command supported by the MySQL command line tool. See here:
http://forums.mysql.com/read.php?39,406094,406329#msg-406329
Here's the list of commands for the command-line tool. Most are not supported as JDBC query statements.
http://dev.mysql.com/doc/refman/5.5/en/mysql-commands.html
You will have to load your SQL commands from the file in your code and send them to JDBC execute methods. Something like:
Statement stm = con.createStatement();
BufferedReader reader = new BufferedReader(new FileReader(new File(...)));
while (true) {
String line = reader.readLine();
if (line == null) {
break;
}
// this is the trick -- you need to pass different SQL to different methods
if (line.startsWith("SELECT")) {
stm.executeQuery(line);
} else if (line.startsWith("UPDATE") || line.startsWith("INSERT")
|| line.startsWith("DELETE")) {
stm.executeUpdate(line);
} else {
stm.execute(line);
}
}
stm.close();
I used something like this...it works if there is a sql command over several lines.
Also there may be problems if the sql-file is too big, for me it worked fine.
BufferedReader br = new BufferedReader(new FileReader(DBFILE));
statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
System.out.println("Reading SQL File...");
String line="";
StringBuilder sb = new StringBuilder();
while( (line=br.readLine())!=null)
{
if(line.length()==0 || line.startsWith("--"))
{
continue;
}else
{
sb.append(line);
}
if(line.trim().endsWith(";"))
{
statement.execute(sb.toString());
sb = new StringBuilder();
}
}
br.close();

Categories

Resources