I am new to java and trying to read a very complex .txt file and inject it into my MySQL DB.
For me, the file has some very strange delimiting rules. the distinction seems to be all commas, but the other parts just don't make any sense. Here are some examples:
" "," "," "," "," "
" ",,,,,,," "
" ",0.00," "
" ",," ",," ",," "
I know that all fields containing letters will be in the usual format ,"text",.
all columns that have only numbers will follow this format: ,0.00,with the exception of the first column following the normal format"123456789",
Then everything without data will alternate between ,,or," ",
I managed to get the program to read java.sql.Statement correctly, but I need it to work with java.sql.PreparedStatement
, 100 + , , . "Some Company, LLC"
, , , .
import java.io.BufferedReader;
import java.io.FileReader;
import java.sql.*;
public class AccountTest {
public static void main(String[] args) throws Exception {
String dbName = "jdbc:mysql://localhost:3306/local";
String userName = "root";
String password = "";
String fileName = "file.txt";
String psQuery = "insert into accounttest"
+ "(account,account_name,address_1,address_2,address_3) values"
+ "(?,?,?,?,?)";
Connection connect = null;
PreparedStatement statement = null;
String account = null;
String accountName = null;
String address1 = null;
String address2 =null;
String address3 = null;
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException e) {
System.out.println("JDBC driver not found.");
e.printStackTrace();
return;
}
try {
connect = DriverManager.getConnection(dbName,userName,password);
}
catch (SQLException e) {
System.out.println("E1: Connection Failed.");
e.printStackTrace();
return;
}
if (connect != null) {
System.out.println("Connection successful.");
}
else {
System.out.println("E2: Connection Failed.");
}
BufferedReader bReader = new BufferedReader(new FileReader(fileName));
String line;
try {
while ((line = bReader.readLine()) != null) {
String data[] = line.split("\",\"");
account = data[0];
accountName = data[1];
address1 = data[2];
address2 = data[3];
address3 = data[4];
account = account.replaceAll("\"", "");
accountName = accountName.replaceAll("\"", "");
address1 = address1.replaceAll("\"", "");
address2 = address2.replaceAll("\"", "");
address3 = address3.replaceAll("\"", "");
statement = connect.prepareStatement(psQuery);
statement.setString(1, account);
statement.setString(2, accountName);
statement.setString(3, address1);
statement.setString(4, address2);
statement.setString(5, address3);
statement.executeUpdate();
}
}
catch (Exception e) {
e.printStackTrace();
statement = null;
}
finally {
bReader.close();
}
}
}
, , , , , , .
: , - ? , ? , , ?
.
: , , txt MySQL, ( ) , ",", ,,,,, ,0.00, - Some Company, LLC. 100 + , 3000 6000 . . , , , .
EDIT2: , , rpc1. String data[] = line.split("\",\""); String data[] = line.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)"); , data[], statement.setString , replaceALL("\"", ""); , , . !