相关文章推荐
打酱油的野马  ·  Spark ...·  2 年前    · 
大力的充电器  ·  多层级 TreeView - 掘金·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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");
                Connecting the txt file works fine, but with a csv file I still get an error (File not found even if it's there...
– user3721905
                Feb 16, 2016 at 17:59

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.

I see, but now I get the error that the Directory has not been found: java.sql.SQLException: Directory not found: c:\temp\Spieltage_log.txt\ - But the folder is definitely there. In a different class I point to it.... – user3721905 Feb 16, 2016 at 16:59

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.