将 Split 元素分组到自己单独的数组中
Grouping the Split elements into own separate arrays
这是我来自 .txt 文件的数据:
**S12*T0*0889*B*99*N1C0~**S12*T0*0880*B*99*N1C0~
这是我从 .txt 文件中读取数据的代码:
{
in = new BufferedReader(new FileReader("2.txt"));
String read = null;
while ((read = in.readLine()) != null) {
read = in.readLine();
String[] splited = read.split("\*+");
PreparedStatement ps = null;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//Data Source Driver
Connection con=DriverManager.getConnection("jdbc:odbc:testing");//Data Connection
for (String part : splited){
ps=con.prepareStatement("insert into testing (d3)values('"+part+"')");//inserting data
System.out.println(part);//printing inserted data
System.out.println("inserted");//insertion confirmation
ps.executeUpdate();
}
根据我的代码,我已经能够在“*”之间拆分数据并将其存储在数组中(拆分)。结果在数据库中,只插入了 1 列。
“*”之间的数据在数据库中属于单独的列。
Problem 1:
I need to separate data between "~" first and then separate the data between "*".
Problem 2:
i need to group the separated data into arrays so that i can insert into database later(every data has its own column in the database).
Expected Output:In Database(ms-Access)
|d1 |d2 |d3|d4| d5 |<--列
S12 |T0 |S12|B |99|N1C0|<--插入元素
所以像这样:
public class Answer28535326 {
private static String input = "**S12*T0*0889*B*99*N1C0~**S12*T0*0880*B*99*N1C0~";
public static void main(String[] args)
{
String[] records = input.split("~");
for (String record : records)
{
System.out.println(record);
StringTokenizer tokenizer = new StringTokenizer(record, "*", false);
while(tokenizer.hasMoreTokens())
{
System.out.println(tokenizer.nextToken());
}
System.out.println("");
}
}
}
尝试批量添加它们
public class Answer28535326 {
private static void addRows(PreparedStatement ps, String line) throws SQLException {
String[] records = line.split("~");
for (String record : records) {
String[] values = record.replaceAll("^\*+", "").split("\*+");
for (int i = 0; i < values.length; i++) {
ps.setString(i + 1, values[i]);
}
ps.addBatch();
}
}
public static void main(String[] args) throws Exception {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");// Data Source Driver
try ( Connection con = DriverManager.getConnection("jdbc:odbc:testing") ) {
try ( PreparedStatement ps = con.prepareStatement("insert into testing (d1, d2, d3, d4, d5, d6) values(?, ?, ?, ?, ?, ?)") ) {
try (BufferedReader in = new BufferedReader(new FileReader("2.txt"))) {
String read = null;
while ((read = in.readLine()) != null) {
read = in.readLine();
addRows(ps, read);
}
ps.executeBatch();
}
}
}
}
}
编辑:按要求进行解释
原始代码是将每一列作为新行插入
数据库。 解决方案是使用准备好的语句
all 的占位符 table
的列值
因为每行可能有 1+ 条记录,所以添加了一个方法
每行处理多个记录并将它们添加为一个批次
这是我来自 .txt 文件的数据:
**S12*T0*0889*B*99*N1C0~**S12*T0*0880*B*99*N1C0~
这是我从 .txt 文件中读取数据的代码:
{
in = new BufferedReader(new FileReader("2.txt"));
String read = null;
while ((read = in.readLine()) != null) {
read = in.readLine();
String[] splited = read.split("\*+");
PreparedStatement ps = null;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//Data Source Driver
Connection con=DriverManager.getConnection("jdbc:odbc:testing");//Data Connection
for (String part : splited){
ps=con.prepareStatement("insert into testing (d3)values('"+part+"')");//inserting data
System.out.println(part);//printing inserted data
System.out.println("inserted");//insertion confirmation
ps.executeUpdate();
}
根据我的代码,我已经能够在“*”之间拆分数据并将其存储在数组中(拆分)。结果在数据库中,只插入了 1 列。
“*”之间的数据在数据库中属于单独的列。
Problem 1: I need to separate data between "~" first and then separate the data between "*". Problem 2: i need to group the separated data into arrays so that i can insert into database later(every data has its own column in the database).
Expected Output:In Database(ms-Access)
|d1 |d2 |d3|d4| d5 |<--列
S12 |T0 |S12|B |99|N1C0|<--插入元素
所以像这样:
public class Answer28535326 {
private static String input = "**S12*T0*0889*B*99*N1C0~**S12*T0*0880*B*99*N1C0~";
public static void main(String[] args)
{
String[] records = input.split("~");
for (String record : records)
{
System.out.println(record);
StringTokenizer tokenizer = new StringTokenizer(record, "*", false);
while(tokenizer.hasMoreTokens())
{
System.out.println(tokenizer.nextToken());
}
System.out.println("");
}
}
}
尝试批量添加它们
public class Answer28535326 {
private static void addRows(PreparedStatement ps, String line) throws SQLException {
String[] records = line.split("~");
for (String record : records) {
String[] values = record.replaceAll("^\*+", "").split("\*+");
for (int i = 0; i < values.length; i++) {
ps.setString(i + 1, values[i]);
}
ps.addBatch();
}
}
public static void main(String[] args) throws Exception {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");// Data Source Driver
try ( Connection con = DriverManager.getConnection("jdbc:odbc:testing") ) {
try ( PreparedStatement ps = con.prepareStatement("insert into testing (d1, d2, d3, d4, d5, d6) values(?, ?, ?, ?, ?, ?)") ) {
try (BufferedReader in = new BufferedReader(new FileReader("2.txt"))) {
String read = null;
while ((read = in.readLine()) != null) {
read = in.readLine();
addRows(ps, read);
}
ps.executeBatch();
}
}
}
}
}
编辑:按要求进行解释
原始代码是将每一列作为新行插入 数据库。 解决方案是使用准备好的语句 all 的占位符 table
的列值
因为每行可能有 1+ 条记录,所以添加了一个方法 每行处理多个记录并将它们添加为一个批次