I am using the following code to update my mysql table where both moving50 and moving200 are variable characters.
String sql = "update toplosers set Moving50 = ?, where Symbol = ?";
PreparedStatement stmt = conn.prepareStatement(sql);
for(int i1=0;i1<i;i1++)
{
stmt.setString(1, moving50[i1]);
stmt.setString(2,symbol[i1]);
stmt.addBatch();
}
stmt.executeBatch();
}
I am getting "have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where Symbol = 'mysymbol'' at line 1
What is the problem
You have unexpected comma right after first parameter.
No need to add 'comma' for where clause.
Now check it.
String sql = "update toplosers set Moving50 = ? where Symbol = ?";
PreparedStatement stmt = conn.prepareStatement(sql);
for(int i1=0;i1<i;i1++)
{
stmt.setString(1, moving50[i1]);
stmt.setString(2,symbol[i1]);
stmt.addBatch();
}
stmt.executeBatch();
}
see sample example here
Avoid the comma character before the 'where' clause.
Related
The syntax that from the java code is not applicable to mysql. The setString() from java will come out with ' and not ` which is not accepted in mysql.
I tried in my localhost to run the code, it really not accepting 'doctor' and only accept ``doctor`.
Below are my code:
PreparedStatement ps = con.prepareStatement("SELECT id FROM ? WHERE id = ?");
ps.setString(1, "doctor");
ps.setInt(2, 123);
ResultSet rs = ps.executeQuery();
and there is an error
com.mysql.jdbc.exceptions.jdbc4.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 ''doctor' WHERE id = 123' at line 1
It's because your code produces following query:
SELECT id FROM 'doctor' WHERE id = 123
As you can see table name is used as a String which is invalid SQL Syntax, so you can either hard code table name and or if you really want it to be dynamic you can achieve it like:
String sql = String.format("SELECT id from %s where id = ?", tblName);
PreparedStatement ps = con.prepareStatement(sql);
ps.setInt(1, 123);
ResultSet rs = ps.executeQuery();
I have the following line of code that I am trying to execute:
List<File> sectList = new ArrayList<File>();
try {
String query = "SELECT * FROM legaf WHERE ida = ?";
PreparedStatement preparedStmt = connection.prepareStatement(query);
preparedStmt.setInt(1, idforfile);
ResultSet result = preparedStmt.executeQuery(query);
while (result.next())
{
fls.add(result.getInt("idf"));
}
}
catch (SQLException ex)
{
ex.printStackTrace();
}
but I receive the following exception:
java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '?' at line 1
which points at the following statement:
ResultSet result = preparedStmt.executeQuery(query);
I am trying to apply the query to a XAMPP MySQL database. I am using Intellij.
fls is declared as
private List<Integer> fls = new ArrayList<Integer>();
Write
preparedStmt.executeQuery();
and not
preparedStmt.executeQuery(query);
else you are executing Statement.executeQuery(String) which does not resolve parameters in the query string.
You don't need to pass in the query string to executeQuery().
Change this:
ResultSet result = preparedStmt.executeQuery(query);
to this:
ResultSet result = preparedStmt.executeQuery();
Primary offenders among tutorials promoting passing in the query string is MkYong:
https://www.mkyong.com/jdbc/jdbc-preparestatement-example-select-list-of-the-records/
Here we find the following line:
ResultSet rs = preparedStatement.executeQuery(selectSQL );
Consider the below code,
String query1 = "insert into quizquestion (ques, quizId) values "
+ "('"+ques+"', '"+quizId+"')";
s = con.createStatement();
s.executeUpdate(query1, Statement.RETURN_GENERATED_KEYS);
rs = s.getGeneratedKeys();
if (rs.next()){
quesId=rs.getString(1);
}
con.setAutoCommit(false);
String query2 = "insert into quizOption (option, quizQuesId, correct) values (?,?,?)";
ps = con.prepareStatement(query2);
for(int i=0; i<options.length; i++){
ps.setString(1, options[i]);
ps.setString(2, quesId);
if(correctOption.equals((i+1)+"")){
ps.setString(3, "1");
}else{
ps.setString(3, "0");
}
ps.addBatch();
}
int x[] = ps.executeBatch();
con.commit();
con.close();
return true;
The problem is my query1 is executed successfully, however I get an exception for query2
One sample error that I'm getting is as follows,
java.sql.BatchUpdateException: 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 'option, quizQuesId, correct) values ('o13','16','1')' at line 1
Any ideas why I'm getting this exception?
Thanks in advance.
Your query2 contains a MySQL reserved keyword, option. This is likely causing the problem. Try enclosing the column name in quotes (") or backticks (`), like this:
String query2 = "insert into quizOption (\"option\", quizQuesId, correct) values (?,?,?)";
String req="INSERT INTO NOTIFICATIONS VALUES(6,1,sysdate,'toz',02542,'bporp')(SELECT valide from mouvement where valide=?)";
I want to make a request with Conditions but I get the error:
SQL command not properly ended
You have an invalid SQL query. Here's your current SQL statement:
INSERT INTO NOTIFICATIONS VALUES(6,1,sysdate,'toz',02542,'bporp')(SELECT valide from mouvement where valide=?)
If we split this into several lines for better understanding, you will have this:
INSERT INTO NOTIFICATIONS
VALUES(6,1,sysdate,'toz',02542,'bporp')
(SELECT valide from mouvement where valide=?)
Which is not a valid statement, not even for any SQL tool. That's because you have 2 statements without separating them: an INSERT and then a SELECT, and you're not executing an INSERT INTO <TABLE1> SELECT ... FROM <TABLE2>.
You should execute a single SQL statement per Statement or PreparedStatement. This, in Java, should be done like this:
String sql1 = "INSERT INTO NOTIFICATIONS"
+ " VALUES(6,1,sysdate,'toz',02542,'bporp')";
String sql2 = "SELECT valide from mouvement where valide=?";
Statement stmt = con.createStatement();
stmt.executeUpdate(sql1);
PreparedStatement pstmt = con.prepareStatement(sql2);
pstmt.setString(1, <parameter_value>);
ResultSet rs = pstmt.executeQuery();
When i am running the following SQL query i am getting the following error:
java.sql.SQLException: ORA-01747: invalid user.table.column, table.column, or column specification
String strQuery = "UPDATE themed_night SET theme_night_name = ?, theme_night_date = TO_DATE(?, 'dd-MM-yy'), theme_night_description = ?, WHERE theme_id = ?";
PreparedStatement stmt = conn.prepareStatement(strQuery);//prepare the SQL Query
stmt.setString(1, title);
stmt.setString(2, output);
stmt.setString(3, details);
stmt.setString(4, themeID);
Is my SQL query syntax correct? I have double checked the columns and i have typed the correct names.
You have an extra comma in your statement. The correct statement should be
String strQuery = "UPDATE themed_night SET theme_night_name = ?, theme_night_date = TO_DATE(?, 'dd-MM-yy'), theme_night_description = ? WHERE theme_id = ?";
Also, you might need to enclose some values with single quotes (though I'm not sure about that)