Query issue
Solved
Helene
-
Helene -
Helene -
Hello everyone,
I am currently working on a project in Java and for that, I created my database on phpMyAdmin. I connected my database to Eclipse, but the problem occurs when I want to add a given element as an argument to my function to my table (which currently has only one column).
I can add any string, but the issue is that it doesn't work when it's a variable.
String sql = "INSERT INTO javadb (personne) VALUES ('j')";
When I replace 'j' with nom, which is the name of my variable, it doesn't work. I've also tried:
.$nom.
$nom
"nom"
'nom'
Nothing works, I can't find the solution.
Any help would be greatly appreciated.
Best regards,
Hélène
I am currently working on a project in Java and for that, I created my database on phpMyAdmin. I connected my database to Eclipse, but the problem occurs when I want to add a given element as an argument to my function to my table (which currently has only one column).
I can add any string, but the issue is that it doesn't work when it's a variable.
String sql = "INSERT INTO javadb (personne) VALUES ('j')";
When I replace 'j' with nom, which is the name of my variable, it doesn't work. I've also tried:
.$nom.
$nom
"nom"
'nom'
Nothing works, I can't find the solution.
Any help would be greatly appreciated.
Best regards,
Hélène
2 answers
Hello,
You need a Prepared Statement.
https://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html
--
Trust does not exclude control.
You need a Prepared Statement.
https://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html
String sql = "INSERT INTO javadb (personne) VALUES (?)"; PreparedStatement ps = con.prepareStatement(sql); ps.setString(1, nom); // "nom" is a String type variable ps.executeUpdate();
--
Trust does not exclude control.
Hello,
Thank you for your response, however I'm getting an error :/ :
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?)' at line 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2941)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1623)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1715)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3243)
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1343)
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1260)
at com.objis.demojdbc.DemoJdbc.sauverEnBase(DemoJdbc.java:34)
at com.objis.demojdbc.DemoJdbc.main(DemoJdbc.java:12)
Thank you for your response, however I'm getting an error :/ :
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?)' at line 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2941)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1623)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1715)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3243)
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1343)
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1260)
at com.objis.demojdbc.DemoJdbc.sauverEnBase(DemoJdbc.java:34)
at com.objis.demojdbc.DemoJdbc.main(DemoJdbc.java:12)
You have:
If you had used the code I provided, in case of an error you should have seen:
Moreover, your import must be incorrect; you likely imported com.mysql.jdbc.* but you need to import java.sql.* instead!
at com.mysql.jdbc.Statement.executeUpdatewhich means you directly executed the query with ""?"" without replacing the value.
If you had used the code I provided, in case of an error you should have seen:
at java.sql.PreparedStatement.executeUpdate()
Moreover, your import must be incorrect; you likely imported com.mysql.jdbc.* but you need to import java.sql.* instead!
Example:
If I concatenate like this:
String sql = "INSERT INTO javadb (person) VALUES ('"+name+"')";It results in this:
INSERT INTO javadb (person) VALUES (''); DROP javadb; --')And bam! It deletes the entire table!