Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.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
从Java文本文档中读取并保存多行_Java_Text - Fatal编程技术网

从Java文本文档中读取并保存多行

从Java文本文档中读取并保存多行,java,text,Java,Text,我正在做一个小项目,阅读文本文档并将内容保存到数据库中。目前,我的程序有一个小缺陷。它读取文本文档并将其放入数据库,但当有多行时,我希望有多个插入。每一行都应该是一个新的插入(所有的行都有相同的结构)我使用标记来表示信息,例如35=8,我使用多个ifs来表示8,但用标记35=等来标识它。这是我到目前为止的代码 public class insert5 { public static Properties loadPropertiesFile() throws Exception {

我正在做一个小项目,阅读文本文档并将内容保存到数据库中。目前,我的程序有一个小缺陷。它读取文本文档并将其放入数据库,但当有多行时,我希望有多个插入。每一行都应该是一个新的插入(所有的行都有相同的结构)我使用标记来表示信息,例如35=8,我使用多个ifs来表示8,但用标记35=等来标识它。这是我到目前为止的代码

 public class insert5 {

public static Properties loadPropertiesFile() throws Exception {
    //
    //declaring prop object
    Properties prop = new Properties();

    // reading properties file
    InputStream in = new FileInputStream("jdbc.properties");
    prop.load(in);
    in.close();
    return prop;
}

public static void main(String[] args) {

    System.out.println("create jdbc connection using properties file");

    Connection con = null;
    Scanner userin = new Scanner(System.in);

    // use try becuase it tends to fail sometimes and we want error messages

    try {

        //load properties file we have created
        Properties prop = loadPropertiesFile();

        //declare vars and get the properties at the same time
        String driverClass = prop.getProperty("SQLSERVERJDBC.driver");
        String url = prop.getProperty("SQLSERVERJDBC.url");
        String username = prop.getProperty("SQLSERVERJDBC.username");
        String password = prop.getProperty("SQLSERVERJDBC.password");


        // have to instance driver
        Class.forName(driverClass).newInstance();

        // make connection object using the previous things as parameters
        con = DriverManager.getConnection(url, username, password);

        //this if is to verify the connection
        if (con != null) {
            System.out.println("connection created successfully using properties file");
        }

        else {
            System.out.println(" unable to create connection");
        }

        BufferedReader reader = new BufferedReader(new FileReader(
                "C:\\Users\\darroyo\\Desktop\\pruebasx.txt"));

        ArrayList<String> array1 = new ArrayList<String>();
        // Read line from file.
        while (true) {
            String line = reader.readLine();
            if (line == null) {
                break;
            }
            // Split line on space.
            String[] parts = line.split("");
            //part in parts
            for (String part : parts) {

                //part is the element in this case each fix tag with value
                array1.add(part);

            }


        }

        System.out.println(array1);


        reader.close();

        //creating the statement(should check to use prepared statement in the future

        // fecha recp y fecha sentra in query and for and ? and order
         String query = " insert into FRONTMC.HECHO (folio_hecho, folio_orden, emisora, serie,"
                + "clave_sentido, titulos_hecho, precio, importe, liquidacion, contraparte, id_estatus, isin, contrato,"
                + "secondary_exec_id, exec_id, F11_ClOrdID, fecha_recepcion, fecha_sentra)"
                    + " values ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,convert(varchar(30),cast(? as datetime),120),convert(varchar(30),cast(? as datetime),120))";

                  // create the mysql insert preparedstatement
                  PreparedStatement preparedStmt = con.prepareStatement(query);


                  for(int counter =0; counter< array1.size();counter++){
                      if(array1.get(counter).substring(0, 3).equals("37=")){
                          preparedStmt.setString (1, array1.get(counter).substring(3));
                      }
                      if(array1.get(counter).substring(0, 3).equals("37=")){
                          preparedStmt.setString (2, array1.get(counter).substring(3));
                      }

                      // change emisora and serie to 48 with bd
                      if(array1.get(counter).substring(0, 3).equals("49=")){
                          preparedStmt.setString (3, array1.get(counter).substring(3));
                      }

                      if(array1.get(counter).substring(0, 4).equals("447=")){
                          preparedStmt.setString (4, array1.get(counter).substring(4));
                      }
                      if(array1.get(counter).substring(0, 3).equals("54=")){
                          preparedStmt.setString (5, array1.get(counter).substring(3));
                      }
                      if(array1.get(counter).substring(0, 3).equals("32=")){
                          preparedStmt.setString (6, array1.get(counter).substring(3));
                      }
                      if(array1.get(counter).substring(0, 3).equals("31=")){
                          preparedStmt.setString (7, array1.get(counter).substring(3));
                      }
                      if(array1.get(counter).substring(0, 4).equals("381=")){
                          preparedStmt.setString (8, array1.get(counter).substring(4));
                      }
                      if(array1.get(counter).substring(0, 3).equals("63=")){
                          preparedStmt.setString (9, array1.get(counter).substring(3));
                      }
                      if(array1.get(counter).substring(0, 4).equals("448=")){
                          preparedStmt.setString (10, array1.get(counter).substring(4));
                      }
                      if(array1.get(counter).substring(0, 4).equals("150=")){
                          preparedStmt.setString (11, array1.get(counter).substring(4));
                      }
                      if(array1.get(counter).substring(0, 3).equals("48=")){
                          preparedStmt.setString (12, array1.get(counter).substring(3));
                      }
                      if(array1.get(counter).substring(0, 2).equals("1=")){
                          preparedStmt.setString (13, array1.get(counter).substring(2));
                      }
                      if(array1.get(counter).substring(0, 4).equals("527=")){
                          preparedStmt.setString (14, array1.get(counter).substring(4));
                      }
                      if(array1.get(counter).substring(0, 3).equals("17=")){
                          preparedStmt.setString (15, array1.get(counter).substring(3));
                      }
                      if(array1.get(counter).substring(0, 3).equals("11=")){
                          preparedStmt.setString (16, array1.get(counter).substring(3));
                      }
                      if(array1.get(counter).substring(0, 3).equals("52=")){

                          String date = array1.get(counter).substring(3);
                            String date2 = date.substring(3,18);
                            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
                            SimpleDateFormat sdf2 = new SimpleDateFormat("dd/MM/yyyy");
                            String ds2 = sdf2.format(sdf1.parse(date));
                            String x = date.substring(9, 21);
                            String newfecha1 = ds2+" "+x;
                            System.out.println(newfecha1);

                          preparedStmt.setString (17, newfecha1);
                      }
                      if(array1.get(counter).substring(0, 3).equals("52=")){

                          String date = array1.get(counter).substring(3);
                            String date2 = date.substring(3,18);
                            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
                            SimpleDateFormat sdf2 = new SimpleDateFormat("dd/MM/yyyy");
                            String ds2 = sdf2.format(sdf1.parse(date));
                            String x = date.substring(9, 21);
                            String newfecha1 = ds2+" "+x;
                            System.out.println(newfecha1);

                          preparedStmt.setString (18, newfecha1);
                      }


                  }

                  // execute the preparedstatement
                  preparedStmt.execute();
        //notifies you that it was completed
        System.out.println("insert complete");
        // loop to check the digits ex. for() array.1get(counter).substring(0,3).equals("23=")

        //error messages
    }catch (SQLException e) {
        e.printStackTrace();
    } 
    catch (Exception e) {
        e.printStackTrace();
    } finally {

        try {
            if (con != null) {
                con.close();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }


}
}
公共类插件5{
公共静态属性loadPropertiesFile()引发异常{
//
//声明prop对象
Properties prop=新属性();
//读取属性文件
InputStream in=新文件InputStream(“jdbc.properties”);
道具荷载(in);
in.close();
返回道具;
}
公共静态void main(字符串[]args){
println(“使用属性文件创建jdbc连接”);
连接con=null;
Scanner userin=新扫描仪(System.in);
//使用try,因为它有时会失败,我们需要错误消息
试一试{
//加载我们创建的属性文件
Properties prop=loadPropertiesFile();
//声明变量并同时获取属性
String driverClass=prop.getProperty(“SQLSERVERJDBC.driver”);
字符串url=prop.getProperty(“SQLSERVERJDBC.url”);
字符串username=prop.getProperty(“SQLSERVERJDBC.username”);
字符串password=prop.getProperty(“SQLSERVERJDBC.password”);
//必须使用实例驱动程序
类.forName(driverClass.newInstance();
//使用前面的内容作为参数创建连接对象
con=DriverManager.getConnection(url、用户名、密码);
//这是为了验证连接
如果(con!=null){
System.out.println(“使用属性文件成功创建的连接”);
}
否则{
System.out.println(“无法创建连接”);
}
BufferedReader reader=新BufferedReader(新文件读取器(
“C:\\Users\\darroyo\\Desktop\\pruebasx.txt”);
ArrayList array1=新的ArrayList();
//从文件中读取行。
while(true){
字符串行=reader.readLine();
如果(行==null){
打破
}
//在空间上分割线。
String[]parts=line.split(“”);
//部分地
用于(字符串部分:部分){
//在这种情况下,part是元素,每个fix标记都有值
1.增加(部分);
}
}
系统输出打印LN(数组1);
reader.close();
//创建语句(应选中以在将来使用准备好的语句
//查询和for?和订单中的fecha recp和fecha sentra
String query=“插入FRONTMC.HECHO(对开对开、对开对开对开、对开对开对开、对开对开对开、对开对开对开、对开对开对开
+clave_Senido、Titolos_hecho、precio、importe、liquidacion、contraparte、Idu estatus、isin、contrato、
+二级执行id,执行id,F11克隆id,接收,接收
+“值(?,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,转换(varchar(30),cast(?as datetime),120),转换(varchar(30),cast(?as datetime),120));
//创建mysql insert preparedstatement
PreparedStatement preparedStmt=con.prepareStatement(查询);
对于(int counter=0;计数器try (BufferedReader br = new BufferedReader(new FileReader(path))) {
        String line;
        while ((line = br.readLine()) != null) {
            // process the line.
        }
    }
    catch (IOException e) {
        System.err.println("Caught IOException: " + e.getMessage());
    }