Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过命令行编译和运行JavaMaven项目?_Java_Sqlite_Maven 3_Opencsv - Fatal编程技术网

如何通过命令行编译和运行JavaMaven项目?

如何通过命令行编译和运行JavaMaven项目?,java,sqlite,maven-3,opencsv,Java,Sqlite,Maven 3,Opencsv,我使用SQLite 3.27.2.1和JDBC编写了一个简单的内存数据库程序。我在这个项目中也使用OpenCSV4.6。到目前为止,我一直在使用EclipseIDE运行该程序,但我想知道如何通过命令行运行这个maven项目。这是我第一次使用Maven,我在Google上搜索了很多次,但没有一个解决方案是有效的。我尝试过用不同插件列表编辑pom.xml文件,还尝试过其他解决方案,比如javac-classpath/to/jar-app.java MS3.java: package com.ms3.

我使用SQLite 3.27.2.1和JDBC编写了一个简单的内存数据库程序。我在这个项目中也使用OpenCSV4.6。到目前为止,我一直在使用EclipseIDE运行该程序,但我想知道如何通过命令行运行这个maven项目。这是我第一次使用Maven,我在Google上搜索了很多次,但没有一个解决方案是有效的。我尝试过用不同插件列表编辑pom.xml文件,还尝试过其他解决方案,比如
javac-classpath/to/jar-app.java

MS3.java:

package com.ms3.dbx;

import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;
import com.opencsv.CSVWriter;

import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

public class MS3
{
    private Connection connection = null;
    private final String URL = "jdbc:sqlite::memory:";

    private final String CSV_PATH = System.getProperty("user.home") + File.separator + "Desktop" + File.separator + "ms3Interview.csv";
private final String BAD_DATA_PATH = System.getProperty("user.home") + File.separator + "Desktop" + File.separator + "bad-data-";
    private final String BAD_DATA_EXT = ".csv";

    private DateFormat df = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss a");
    private String badDataFilename = BAD_DATA_PATH + df.format(new Date()) + BAD_DATA_EXT;

    private final String LOG_FILE_PATH = System.getProperty("user.home") + File.separator + "Desktop" + File.separator + "Statistics.log";

    private int recordsReceived = 0;
    private int recordsSuccessful = 0;
    private int recordsFailed = 0;

    static
    {
        try
        {
            Class.forName("org.sqlite.JDBC");
        }

        catch (ClassNotFoundException e)
        {
            throw new ExceptionInInitializerError(e);
        }
    }

    // Opens connection to in-memory database
    private void openConnection() throws SQLException
    {
        if (connection == null || connection.isClosed())
        {
            connection = DriverManager.getConnection(URL);
            System.out.println("Connection to database established!");
        }
    }

    // Closes connection to database
    private void closeConnection() throws SQLException
    {
        connection.close();
        System.out.print("Database connection closed!");
    }

    // Creates a table named X in database
    private void createTable()
    {
        try
        {
            final Statement statement = connection.createStatement();

            statement.executeUpdate("CREATE TABLE IF NOT EXISTS X"
                    + "(A       TEXT,"
                    + " B       TEXT,"
                    + " C       TEXT,"
                    + " D       TEXT,"
                    + " E       TEXT,"
                    + " F       TEXT,"
                    + " G       TEXT,"
                    + " H       TEXT,"
                    + " I       TEXT,"
                    + " J       TEXT);");

        }

        catch (SQLException e)
        {
            e.getMessage();
        }
    }

    // Reads data from sample.csv file using OpenCSV
    // If there is a blank column in a row, write it to "bad-data-<timestamp>.csv" file
    // Else insert the row into the database
    // Increment recordsReceived for each row in sample.csv file
    // Increment recordsSuccessful for each row that has every column filled with data
    // Increment recordsFailed for each row that has at least one blank column
    private void insertFromCSV()
    {
        try
        {
            Reader reader = Files.newBufferedReader(Paths.get(CSV_PATH));
            CSVReader csvReader = new CSVReaderBuilder(reader).withSkipLines(1).build();

            Writer writer = Files.newBufferedWriter(Paths.get(badDataFilename));

            CSVWriter csvWriter = new CSVWriter(writer,
                    CSVWriter.DEFAULT_SEPARATOR,
                    CSVWriter.NO_QUOTE_CHARACTER,
                    CSVWriter.DEFAULT_ESCAPE_CHARACTER,
                    CSVWriter.DEFAULT_LINE_END);

            final String[] headerRecord = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"};
            csvWriter.writeNext(headerRecord);

            PreparedStatement pstatement = connection.prepareStatement("INSERT INTO X(A,B,C,D,E,F,G,H,I,J) "
                    + "VALUES(?,?,?,?,?,?,?,?,?,?);");

                String[] nextRecord;
                while ((nextRecord = csvReader.readNext()) != null)
                {
                    recordsReceived++;

                    if (!Arrays.asList(nextRecord).contains(""))
                    {
                        recordsSuccessful++;
                        pstatement.setString(1, nextRecord[0]);
                        pstatement.setString(2, nextRecord[1]);
                        pstatement.setString(3, nextRecord[2]);
                        pstatement.setString(4, nextRecord[3]);
                        pstatement.setString(5, nextRecord[4]);
                        pstatement.setString(6, nextRecord[5]);
                        pstatement.setString(7, nextRecord[6]);
                        pstatement.setString(8, nextRecord[7]);
                        pstatement.setString(9, nextRecord[8]);
                        pstatement.setString(10, nextRecord[9]);
                        pstatement.executeUpdate();
                    }

                    else
                    {
                        recordsFailed++;
                        csvWriter.writeNext(nextRecord);
                    }
                }

                csvWriter.close();
        }

        catch (IOException e)
        {
            e.getMessage();
        }

        catch (SQLException e)
        {
            e.getMessage();
        }
    }

    // Query the database and print everything to make sure the data is actually being inserted
    private void testDB()
    {
        try
        {
            final Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery("SELECT * FROM X;");

            ResultSetMetaData rsmd = rs.getMetaData();
            final int numColumns = rsmd.getColumnCount();

            while(rs.next())
            {
                for (int i = 1; i <= numColumns; i++)
                {
                    System.out.print(rs.getString(i) + ",");
                }

                System.out.println("\n");
            }
        }

        catch (SQLException e)
        {
            e.getMessage();
        }
    }

    // Log the received, successful, and failed records in a log file
    private void logStats()
    {
        try
        {
            FileWriter fw = new FileWriter(LOG_FILE_PATH);
            fw.write("Records Received: " + recordsReceived + "\n");
            fw.write("Records Successful: " + recordsSuccessful + "\n");
            fw.write("Records Failed: " + recordsFailed);
            fw.close();
        }

        catch (IOException e)
        {
            e.getMessage();
        }
    }

    public static void main(String[] args) throws SQLException
    {
        MS3 obj = new MS3();
        obj.openConnection();
        obj.createTable();
        obj.insertFromCSV();
        obj.logStats();
        obj.testDB();
        obj.closeConnection();
    }
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.ms3.dbx</groupId>
  <artifactId>dbx</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>dbx</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.opencsv/opencsv -->
    <!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
    <dependency>
      <groupId>org.xerial</groupId>
      <artifactId>sqlite-jdbc</artifactId>
      <version>3.27.2.1</version>
    </dependency>
    <dependency>
      <groupId>com.opencsv</groupId>
      <artifactId>opencsv</artifactId>
      <version>4.6</version>
    </dependency>    
  </dependencies>
  <build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <configuration>
                <mainClass>main</mainClass>
            </configuration>
        </plugin>
    </plugins>
  </build>
</project>
package com.ms3.dbx;
导入com.opencsv.CSVReader;
导入com.opencsv.CSVReaderBuilder;
导入com.opencsv.CSVWriter;
导入java.io.FileWriter;
导入java.io.IOException;
导入java.io.Reader;
导入java.io.Writer;
导入java.nio.file.Files;
导入java.nio.file.path;
导入java.sql.Connection;
导入java.sql.DriverManager;
导入java.sql.PreparedStatement;
导入java.sql.Statement;
导入java.text.DateFormat;
导入java.text.simpleDataFormat;
导入java.util.array;
导入java.util.Date;
导入java.sql.ResultSet;
导入java.sql.ResultSetMetaData;
导入java.sql.SQLException;
公共类MS3
{
私有连接=null;
私有最终字符串URL=“jdbc:sqlite::memory:”;
私有最终字符串CSV_PATH=System.getProperty(“user.home”)+File.separator+“Desktop”+File.separator+“ms3Interview.CSV”;
私有最终字符串BAD_DATA_PATH=System.getProperty(“user.home”)+File.separator+“Desktop”+File.separator+“BAD DATA-”;
私有最终字符串BAD_DATA_EXT=“.csv”;
private DateFormat df=新的SimpleDateFormat(“MM dd yyyy hh:MM:ss a”);
私有字符串badDataFilename=BAD_DATA_PATH+df.format(new Date())+BAD_DATA_EXT;
私有最终字符串LOG\u FILE\u PATH=System.getProperty(“user.home”)+FILE.separator+“Desktop”+FILE.separator+“Statistics.LOG”;
私有int记录接收=0;
私有int记录成功=0;
私有int记录失败=0;
静止的
{
尝试
{
Class.forName(“org.sqlite.JDBC”);
}
catch(classnotfounde异常)
{
抛出新异常初始化错误(e);
}
}
//打开与内存中数据库的连接
私有void openConnection()引发SQLException
{
if(connection==null | | connection.isClosed())
{
connection=DriverManager.getConnection(URL);
System.out.println(“已建立到数据库的连接!”);
}
}
//关闭与数据库的连接
私有void closeConnection()引发SQLException
{
connection.close();
System.out.print(“数据库连接已关闭!”);
}
//在数据库中创建名为X的表
私有void createTable()
{
尝试
{
final Statement=connection.createStatement();
语句.executeUpdate(“如果不存在,则创建表X”
+(A)文本
+“B文本,”
+“C文本,”
+“D文本,”
+“电子文本,”
+F文本
+“G文本,”
+“H文本,”
+“我发短信,”
+“J(文本);”;
}
捕获(SQLE异常)
{
e、 getMessage();
}
}
//使用OpenCSV从sample.csv文件读取数据
//如果行中有空白列,请将其写入“坏数据-.csv”文件
//否则将该行插入数据库
//sample.csv文件中每行接收的增量记录
//增量记录对于每个列都填充了数据的行成功
//对于至少有一个空白列的每行,增量记录失败
私有void insertFromCSV()
{
尝试
{
Reader Reader=Files.newbuffereder(PATH.get(CSV_PATH));
CSVReader CSVReader=新的CSVReaderBuilder(reader).WithKiplines(1.build();
Writer-Writer=Files.newBufferedWriter(path.get(badDataFilename));
CSVWriter CSVWriter=新的CSVWriter(编写器,
CSVWriter.DEFAULT_分隔符,
CSVWriter.NO_QUOTE_字符,
CSVWriter.DEFAULT\u转义\u字符,
CSVWriter.DEFAULT\u LINE\u END);
最后一个字符串[]headerRecord={“A”、“B”、“C”、“D”、“E”、“F”、“G”、“H”、“I”、“J”};
csvWriter.writeNext(headerRecord);
PreparedStatement pstatement=连接。prepareStatement(“插入X(A、B、C、D、E、F、G、H、I、J)”
+“值(?,,,,,,,,,,,,,,,,;”;
字符串[]下一条记录;
而((nextRecord=csvReader.readNext())!=null)
{
记录接收++;
如果(!Arrays.asList(nextRecord).contains(“”)
{
recordsSuccessful++;
pstatement.setString(1,nextRecord[0]);
pstatement.setString(2,nextRecord[1]);
pstatement.setString(3,nextRecord[2]);
pstatement.setString(4,nextRecord[3]);
pstatement.setString(5,nextRecord[4]);
pstatement.setString(6,nextRecord[5]);
pstatement.setString(7,nextRecord[6]);
pstatement.setString(8,nextRecord[7]);
pstatement.setString(9,nextRecord[8]);
pstatement.setString(10,nextRecord[9]);
pstatement.executeUpdate();
}
其他的
{
记录失败++;
csvWriter.writeNext(下一条记录);