0

This is the output im recieving:

INSERT INTO [ J] VALUES ([J1, Sorter, Paris)
INSERT INTO [ J] VALUES (J2, Punch, Rome)
INSERT INTO [ J] VALUES (J3, Reader, Athens)
INSERT INTO [ J] VALUES (J4, Console, Athens)
INSERT INTO [ J] VALUES (J5, Collator, London)
INSERT INTO [ J] VALUES (J6, Termninal, Oslo)
INSERT INTO [ J] VALUES (J7, Tape, London])

As you can see i have a [ at the beginning and a ] at the end and i don't know what is causing it? how can i remove it? i have tried regex it with .replace("[ ]",""); where i belive the problem is occuring but to no avail.

Code for inserting:

  if (state == 3||!tableLineScanner.hasNextLine()){
                        try{
                            Class.forName("org.sqlite.JDBC");
                            Connection conn = null;
                            conn = DriverManager.getConnection("jdbc:sqlite:test.db");
                            //TODO Input values goes here
                            tableName =""+tableNames;
                            Statement stmt = conn.createStatement();
                            String query = tableValues + "";
                            ResultSet rs = stmt.executeQuery("SELECT * FROM "+ tableName);
                            ResultSetMetaData rsmd = rs.getMetaData();
                            query.replaceAll("[ ]","");
                            tableName =""+tableNames;
                            tableName.replaceAll("[ ]","");
                            String[] splittedOutput = query.split(", ");
                            int valuesOnLine = rsmd.getColumnCount();
                            for (int i = 0; i < splittedOutput.length; i++) {
                                if (i % valuesOnLine == 0) {
                                    System.out.print("INSERT INTO " + tableName + " VALUES (");
                                }
                                System.out.print(splittedOutput[i]);
                                if (i % valuesOnLine == valuesOnLine - 1) {
                                    System.out.println(")");
                                } else {
                                    System.out.print(", ");
                                }
                            }
                            tableNames.clear();
                            tableValues.clear();
                            tableFields.clear();
                        }
                        catch (SQLException ex){
                            System.out.println("SQLException: " + ex.getMessage());
                            System.out.println("VendorError: " + ex.getErrorCode());
                        }
                        catch (ClassNotFoundException e){
                            e.printStackTrace();
                        }
                    }

2 Answers 2

1

You're going to get more problems if you have more than one element in tableNames. Do you intend to just use the first table name? Or to iterate through tableNames using all of them? Right now, you're converting tableNames to a String of the form [element1, element2, element3] which is almost certainly not what you want.

If you did want to just remove the [] characters though, you need to write something like replaceAll("[\\[\\]]", "") because [] are special characters in a regular expression.

Sign up to request clarification or add additional context in comments.

3 Comments

Im having an array with element1,element2,element3 but i cant seem to get rid of the [] even with your replaceAll code
I don't think you have an array. You have either a List or a Set of some type. If it's a list, I recommend calling get(0) on it to get the value that you want.
If your solution is different from what I said, can you please post it as an answer? For the benefit of anyone else who has this problem in the future? Thanks.
0

The solution to remove the brackets was using by configuring splittedOutput to this:

System.out.print(splittedOutput[i].replaceAll("[\\[\\]]", ""));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.