0
votes

I'm making a Java program that generates random data and populates a PostgreSQL Database. I'm at the beginning of my program and the following code is actually the part adding a row (not generated randomly) to the table public.bank_card_people . The columns of the table are first-name, last-name and card-number. The connection with database is effective as I achieve to print rows from the table.

public static void main(String[] args) {
    try{
        Class.forName("org.postgresql.Driver");
        Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/Benerator","postgres","newPassword");
        String SQL = "insert into public.bank_card_people (first-name, last-name, card-number) VALUES (?,?,?)";
        PreparedStatement stmt2 = con.prepareStatement(SQL);
        stmt2.setString(1, "ra");
        stmt2.setString(2, "ra");
        stmt2.setString(3, "ra");
        stmt2.executeQuery();

I ran the program and had the error ERROR: syntax error at or near "-"Position: 43

I will keep you updated if I find a solution!

2

2 Answers

2
votes

For PostgreSQL use "first-name" around your field

So the SQL would be

String SQL = "insert into public.bank_card_people (\"first-name\", \"last-name\", \"card-number\") VALUES (?,?,?)"
0
votes

I answer my question since I achieved to make my program work, I hope it will help some of you.

Actually, the problem was simple, I just changed the names of the columns in the public.bank_card_people , as SQL didn't seem to like it. I basically removed the "-" so for example first-name would become firstname and last-name would become lastname.