JDBC - csv file Error - java

I have a simple question, somehow I can't see where my problem is.
I've got a csv file in my C:/Temp folder. I would like to connect to the csv to get some data (depending on specific row data, different rows,...).
So I downloaded the csvjdbc-1.0-28.jar file and added it to the build path.
I wrote the code as shown below but always get the error:
"java.sql.SQLException: No suitable driver found for"
I have seen some people got also problems with it but I did not get the problem behind the issue I have. I know it has something to do with the Connection conn. Do I need to do some additional JDBC settings or how can I add the path for the connection?
Thanks in advance!
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.relique.jdbc.csv.CsvDriver;
public class Main_Class {
public static void main(String[] args) {
try {
try {
Class.forName("org.relique.jdbc.csv.CsvDriver");
Connection conn = DriverManager
.getConnection("c:\\temp\\Spieltage_log.txt");
Statement stmt = conn.createStatement();
ResultSet results = stmt
.executeQuery("select * from Offensiver_Zweikampf");
boolean append = true;
CsvDriver.writeToCsv(results, System.out, append);
conn.close();
System.out.println(results);
} catch (SQLException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
// JFrame fenster = new Main_Menue();
}
}

According to the example at (http://csvjdbc.sourceforge.net/)-
// Create a connection. The first command line parameter is
// the directory containing the .csv files.
// A single connection is thread-safe for use by several threads.
Connection conn = DriverManager.getConnection("jdbc:relique:csv:" + directoryName);
In your case it should be -
Properties props = new Properties();
props.put("fileExtension", ".txt");
Connection conn = DriverManager.getConnection("jdbc:relique:csv:C:\\temp", props);
Also you've put the content in a txt file, so you'll need to specify a custom property with the fileExtension as '.txt' in it.
Your resultSet object than can query the file using the below syntax -
ResultSet results = stmt.executeQuery("select * from Spieltage_log");

The URL string passed to DriverManager.getConnection() needs to specify the driver name:
Connection conn = DriverManager
.getConnection("jdbc:relique:csv:c:\\temp");
Besides, you need to pass the directory of the csv file and not the file itself. See answer of Sachin who in the meantime posted detailed instructions.

Sorry my fault: I saved the file as a xlsx file and not csv file. So solved it now and it worked! Thanks a lot guys! Appreciate your help

Related

Result of 'File.createNewFile()' is ignored

I'm not sure why this is but in my IDE, for file.createNewFile(); I'm getting Result of 'File.createNewFile()' is ignored for it which I've no idea what I should be doing
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
public class SQL {
public Connection connection;
public SQL(File file) {
try {
file.createNewFile(); // Result of 'File.createNewFile()' is ignored
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:sqlite:" + file.getAbsolutePath());
} catch(Exception exception) {
exception.printStackTrace();
}
}
}
Static Code Inspection in your IDE for createNewFile might be complaining this because createNewFile returns a boolean value. This is in place so that you have a way to determine if the creation of file is successful or not and act accordingly. Right now the boolean result is not assigned to any variable, so it's ignored.
Ideally you should check the result of file.createNewFile() and validate whether file actually got created before moving on to setting up connection.
// Create File
boolean fileCreated = file.createNewFile()
// Validate that file actually got created
if (!fileCreated) {
throw new IOException("Unable to create file at specified path. It already exists");
}
// Proceed to setting up connection
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:sqlite:" + file.getAbsolutePath());
The createNewFile() method returns a boolean (true or false). false indicates that the file already exists. true indicates the file did not exist, and was created.
The warning you are seeing in the IDE is just letting you know that you are ignoring that value, which might be ok depending on your requirements. An alternative might be somehting like this:
if(file.createNewFile()) {
// do something
} else {
// do something else
}
It's probably a good idea to check the return value of the createNewFile() method. If it returns false, then it means the file already existed. This may not be what you expected, because the file is being used for specifying the database URL.
I'm unable to replicate your issue to get that specific error message. It should be noted though that File.createNewFile() only works if a file of the same name does not already exist. I would instead recommend doing this to check if the file already exists:
public class SQL {
public static Connection connection;
public SQL(File file) throws IOException {
File file = new File("C:/users/capoo/Desktop/file_test.txt");
if (!file.createNewFile()) {
throw new IOException("File already exists");
} else {
try {
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:sqlite:" + file.getAbsolutePath());
} catch(Exception exception) {
exception.printStackTrace();
}
}
}
}

Properties file not loading and updating for relative path but working fine for absolute path

I am trying to access a properties file from the src/main/resources folder but when I try to load the file using a relative path it is not getting updated. But it is working fine for an absolute path.
I need the dynamic web project to work across all platforms.
public static void loadUsers() {
try(
FileInputStream in = new FileInputStream("C:\\Users\\SohamGuha\\Documents\\work-coding\\work-coding\\src\\main\\resources\\users.properties")) {
// write code to load all the users from the property file
// FileInputStream in = new FileInputStream("classpath:users.properties");
users.load(in);
System.out.println(users);
in.close();
}
catch(Exception e){
e.printStackTrace();
}
First of all you are using Spring, at least that is what the tags at the bottom say. Secondly C:\\Users\\SohamGuha\\Documents\\work-coding\\work-coding\\src\\main\\resources\\users.properties is the root of your classpath. Instead of loading a File use the Spring resource abstraction.
As this is part of the classpath you can simply use the ClassPathResource to obtain a proper InputStream. This will work regardless of which environment you are in.
try( InputStream in = new ClassPathResource("users.properties").getInputStream()) {
//write code to load all the users from the property file
//FileInputStream in = new FileInputStream("classpath:users.properties");
users.load(in);
System.out.println(users);
} catch(Exception e){
e.printStackTrace();
}
NOTE: you are already using a try with resources so you don't need to close the InputStream that is already handled for you.
Changing things inside your application simply won't work, as this would mean you could change resources (read classes) in your jar which would be quite a security risk! If you want something to be changable you will have to make it a file outside of the classpath and directly on the file-system.
Try the following code
import java.io.FileInputStream;
import java.io.IOException;
public class LoadUsers {
public static void main(String[] args) throws IOException {
try(FileInputStream fis=new FileInputStream("src/main/resources/users.properties")){
Properties users=new Properties();
users.load(fis);
System.out.println(users);
}catch(IOException ioe) {
ioe.printStackTrace();
}
}
}

Need Help executing postgresql query through java process

I need to execute a bat file that executes a PostgreSQL query in commandline using psql.exe in windows 10 and Java-8 .I have to execute the query through bat file only for testing the bat file. Below is the contents of the bat file
BatFileContents:
postgresql\bin\psql.exe -U username -p dbport -h 127.0.0.1 -c "
insert into table1 values(value1,value2);
insert into table2 values(value1,value2);
insert into table3 values(value1,value2) database"
I tried using java ProcessBuilder to call the bat file but pgsql is asking for password.Below is the code i used to call the bat file and provide password but it doesn't work as expected.
Code:
Main class:
import java.io.BufferedWriter;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Scanner;
public class ProcessTest {
public static void main(String args[])
{
Process process=null;
String batFile="\"c:\\batfilepath\\test.bat\"";
ProcessBuilder processBuilder=new ProcessBuilder(batFile);
try {
//process=Runtime.getRuntime().exec(batFile);
process=processBuilder.start();
StreamEater inputStreamEater=new StreamEater(process.getInputStream());
StreamEater errorStreamEater=new StreamEater(process.getErrorStream());
inputStreamEater.start();
errorStreamEater.start();
BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
writer.write("password");
writer.newLine();
writer.close();
int errorcode=process.waitFor();
System.out.println("errorcode:"+errorcode);
}
catch(Exception exception)
{
exception.printStackTrace();
}
}
}
StreamEater class
class StreamEater extends Thread
{
private InputStream inputStream=null;
public StreamEater(InputStream stream)
{
this.inputStream=stream;
}
public void run()
{
System.out.println("Stream Eater thread started");
Scanner scanner=new Scanner(new InputStreamReader(inputStream));
String message="";
StringBuilder sb=new StringBuilder("");
try
{
System.out.println("before reading message");
while(scanner.hasNextLine())
{
message=scanner.nextLine();
sb.append(message);
sb.append("\n");
}
scanner.close();
System.out.println("after reading message "+sb.toString());
}
catch(Exception exception)
{
exception.printStackTrace();
}
System.out.println("Stream Eater thread ended");
}
}
Can anyone point out what mistake i have done.Is there any other way to send input to the bat file.
There are several issues with this code. I will answer your direct questions first.
Passing Password Arguments (Direct Answer)
The pgsql command forces the user to enter a password interactively. This SO answer describes two techniques to handle this:
Use a .pgpass file. You can create this file independently of your program and alter the BAT file to reference it.
Set a PGPASSWORD environment variable. You can alter the BAT file to do this. You can also set this in ProcessBuilder from your application with code like processBuilder.environment().put("PGPASSWORD ", "PASSWORD");
Use JDBC (Improve your code)
Instead of having your Java app call a batch file, use JDBC. JDBC literally exists so that Java can connect to a database and run queries against that database.
This would avoid the challenges of calling a batch script and let your application directly interact with the database.
I think that your thread will end really soon since the scanner has no lines to read. It could be blocked operation though but you should loop this thread. The answer above is also good but try this without affecting you code too much:
while (stillReading) {
while(scanner.hasNextLine())
{
message=scanner.nextLine();
sb.append(message);
sb.append("\n");
}
Thread.sleep(1000);
}
The main problem is that there is no input in InputStream when the thread starts.

Java JDBC-ODBC cannot load driver for Excel

import java.sql.Connection;
import java.sql.DriverManager;
public class ConnectionExample {
public static void main(String args[]) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (Exception e) {
System.out.println("JDBC-ODBC driver failed to load.");
return;
}
try {
Connection con = DriverManager.getConnection("jdbc:odbc:abcdefg", "", "");
con.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
This code always prints
"JDBC-ODBC driver failed to load."
I can't understand what the problem is.. I follow these steps:
go to c:\windows\sysWOW64\odbcad32.exe
system dsn tab - add -> Microsoft Excel Driver (*xls, *xlsx, *xlsm, *xlsb)
give Data Source Name abcdefg
Select Workbook -> go to myFile excel path and add it -> OK
and then run my code... where is the mistake?
The JDBC-ODBC Bridge is obsolete and has been removed from Java 8. If you need to manipulate an Excel document and you are unable (or unwilling) to downgrade your environment to Java 7 then you might want to investigate Apache POI.

Reading a Txt-File in a jar-File in Java

I've write a Java programm and packaged it the usual way in a jar-File - unfortunately is needs to read in a txt-File. Thats way the programm failed to start on other computer machines because it could not find the txt-file.
At the same time Im using many images in my programm but here there is no such problem: I "copy" the images to the eclipse home directory, so that they are packaged in the jar-File and usable through following command:
BufferedImage buffImage=ImageIO.read(ClassName.class.getClassLoader()
.getResourceAsStream("your/class/pathName/));
There is something similar for simple textfiles which then can be use as a normal new File()?
Edit
Ive try to solve my problem with this solution:
package footballQuestioner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.security.auth.login.Configuration;
public class attempter {
public static void main(String[] args) {
example ex = new example();
}
}
class example {
public example() {
String line = null;
BufferedReader buff = new BufferedReader(new InputStreamReader(
Configuration.class
.getResourceAsStream("footballQuestioner/BackUpFile")));
do {
try {
line = buff.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} while (line != null);
}
}
But it gives always an NullPointerException...do I have forgotten something?
Here is as required my file structure of my jar-File:
You can load the file from the ClassPath by doing something like this:
ClassLoader cl = getClass().getClassLoader()
cl.getResourceAsStream("TextFile.txt");
this should also work:
getClass().getResourceAsStream(fileName);
File always points to a file in the filesystem, so I think you will have to deal with a stream.
There are no "files" in a jar but you can get your text file as a resource (URL) or as an InputStream. An InputStream can be passed into a Scanner which can help you read your file.
You state:
But it gives always an NullPointerException...do I have forgotten something?
It means that likely your resource path, "footballQuestioner/BackUpFile" is wrong. You need to start looking for the resource relative to your class files. You need to make sure to spell your file name and its extension correctly. Are you missing a .txt extension here?
Edit
What if you try simply:
BufferedReader buff = new BufferedReader(new InputStreamReader(
Configuration.class.getResourceAsStream("BackUpFile")));

Categories