线程" main"中的例外情况Java e PostgreSQL

时间:2017-11-08 16:55:27

标签: java postgresql

我做了一个程序,通过Java读取文件并将其放入数据库。我使用不同的数据库和Cassandra它的工作原理。然后我尝试使用Postgres,但它给了我错误:

线程中的异常" main"在Client.main(Client.java:37)的PlantsDBPostgres.createStructure(PlantsDBPostgres.java:15)上的java.lang.NullPointerException

为什么呢?谢谢 我在下面附上代码:

import java.io.IOException;

public interface DatabaseTesi {

public abstract void connection();
public abstract void createStructure() throws IOException;
public abstract void insertFile() throws IOException;
}


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public abstract class FileCSV {

private static String file;
protected String [] obj;
protected String line;
protected BufferedReader reader;

public FileCSV(String file) {
    super();
    FileCSV.file = file;
}

public FileCSV() {
    super();
}

public void readFile() throws IOException {

    reader = new BufferedReader(new FileReader(file));
    line = reader.readLine();
}

public abstract void readSingleObj();

}


public class FilePlants extends FileCSV {

protected String symbol;
protected String synonym_symbol;
protected String scientific_name;
protected String common_name;
protected String family;

public FilePlants(String file) {
    super(file);
    // TODO Auto-generated constructor stub
}   

public FilePlants() {
    super();
}

public void readSingleObj() {

    symbol = obj[0];
    synonym_symbol = obj[1];
    scientific_name = obj[2];
    common_name = obj[3];
    family = obj[4];                
}

}



import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public abstract class DBPostgres implements DatabaseTesi{

protected FileCSV fs;

FileCSV getFs() {
    return fs;
}

void setFs(FileCSV fs) {
    this.fs = fs;
}

private PostgresType type = null;

public DBPostgres (PostgresType type) {
    this.type = type;
}

public PostgresType getType() {
    return type;
}

public void setType(PostgresType type) {
    this.type = type;
}

PreparedStatement st = null;
Connection connection = null;

public void connection() {

    try {
        Class.forName("org.postgresql.Driver");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        return;
    }
    System.out.println("PostgreSQL JDBC Driver registered");

    try {
        connection = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/dbplants", "postgres", "postgres");
    } catch (SQLException e) {
        System.out.println("Connection Failed");
        e.printStackTrace();
        return;
    }

    if (connection != null) {
        System.out.println("Connection successful");
    }else {
        System.out.println("Failed to make connection");
    }
}

public abstract void createStructure();

public abstract void insertFile() throws IOException;
}


import java.io.IOException;
import java.sql.SQLException;

public class PlantsDBPostgres extends DBPostgres{

PlantsDBPostgres() {
    super(PostgresType.PLANTS);
}

FilePlants fp = new FilePlants();

public void createStructure() {

    try {
        st = connection.prepareStatement("CREATE TABLE IF NOT EXISTS my_tab1 (symbol varchar(500), synonym_symbol varchar(500), scientific_name varchar(500), common_name varchar(500), family varchar(500))");
        st.executeUpdate();
    } catch(SQLException e) {
        System.out.println("Table already exist");
        e.printStackTrace();
        return;
    }
}

public void insertFile() throws IOException {

    try {
        fp.readFile();
        while (fp.line!=null) {
            fp.line = fp.reader.readLine();

            if (fp.line!=null && fp.line.length()>0) {
                fp.obj = fp.line.split(",");
                fp.readSingleObj();

                st = connection.prepareStatement("INSERT INTO my_tab1 (symbol, synonym_symbol, scientific_name, common_name, family) VALUES(?, ?, ?, ?, ?)");
                st.setString(1, fp.symbol);
                st.setString(2, fp.synonym_symbol);
                st.setString(3, fp.scientific_name);
                st.setString(4, fp.common_name);
                st.setString(5, fp.family);
                st.executeUpdate(); 
            }
        }
    }catch(SQLException e) {
        e.printStackTrace();
    }   
}

}



public enum PostgresType {

PLANTS;
}




public class DBPostgresFactory {

public static DBPostgres factory(PostgresType type) {
    DBPostgres dbpo = null;
    switch(type) {
    case PLANTS:
        dbpo = new PlantsDBPostgres();
        break;
    }
    return dbpo;
}
}


import java.io.IOException;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

public class Client {

public static void main(String[] args) throws IOException {

    CommandLineParser parser = new DefaultParser();
    Options options = new Options();
    options.addOption("file1", true, "File piante");

    try {
        CommandLine commandLine = parser.parse(options, args);
        String path = commandLine.getOptionValue("file1");
        FileCSV inputFile = new FilePlants(path);
    DBPostgresFactory.factory(PostgresType.PLANTS).setFs(inputFile);
    }catch(ParseException e) {
        e.printStackTrace();
    }
    DBPostgresFactory.factory(PostgresType.PLANTS).connection();
    DBPostgresFactory.factory(PostgresType.PLANTS).createStructure();
    DBPostgresFactory.factory(PostgresType.PLANTS).insertFile();
    }

   }

0 个答案:

没有答案