Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello! Im trying to write some method, which will add a new record to date base.

Code:
Java
public void insertToPlayList() {
        try {
            connecting();
            stat6 = Connection.createStatement();
            stat6.executeUpdate("INSERT INTO odtwarzane VALUES('','2','4','2','2015-10-10','10:10')");
            connecting();
        } catch (SQLException Exception) {
            JOptionPane.showMessageDialog(null, "Wystąpił problem z dodaniem rekordu.", "Błąd", JOptionPane.ERROR_MESSAGE);
        }
    }

Originally, this method is getting 5 paramenters, but for fixing problem i removed it.
Java
public void insertToPlayList(int filmId, int cinemaId, int priceId, String date, String time) {
        try {
            connecting();
            stat6 = Connection.createStatement();
            String str = ("INSERT INTO odtwarzane (id_of, id_filmu, id_kina, id_ceny, data, godzina) VALUES (," + filmId + ", " + cinemaId + ", " + priceId + ", '" + date + "', '" + time + "');");
            stat6.executeUpdate(str);
            connecting();
        } catch (SQLException Exception) {
            JOptionPane.showMessageDialog(null, "Wystąpił problem z dodaniem rekordu.", "Błąd", JOptionPane.ERROR_MESSAGE);
        }
    }


I checked on PhpMyAdmin, and statment was worked
SQL
INSERT INTO odtwarzane VALUES('','2','4','2','2015-10-10','10:10');


Anyway, program is throwing SQLException, and i dont know how to fix it.

What I have tried:

I tried to change statement or code in many ways, and i was looking for some solution in the Internet, but i didnt find it.
Posted
Updated 7-Feb-16 6:12am
v2

1 solution

You have an error in your statement for the Values clause: see PHP Insert Data Into MySQL[^] for correct syntax. Also you should not use string concatenation for SQL statements: see bobby-tables.com: A guide to preventing SQL injection[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900