I have a simple SQL query. It works fine if I use it in SequelPro but when I try to use the query in a Java statement if fails.
The query is operating on a simple table that only has two fields, idpolari and word. I originally structured the query as SELECT * FROM polari when this did not work I tried naming the specific fields. Have tried executing the query in SequelPro and against the database and it works fine. It also works from the command line. Just not working in Java.
I am working in Netbeans running MySQL on an IMac.
sql = "SELECT idpolari, word FROM polari";
Statement stmt = con.createStatement();
ResultSet res1 = stmt.executeQuery(sql);
I would have expected the full contents fo the polari table, what I get is this error message:
INFO: 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 SELECT idpolari, word FROM polari
So I can successfully insert information into one table called "parts" using the code:
String sql1 = "INSERT INTO parts(PART1, PART2, PART3, PART4, PART5)" + "VALUES(?,?,?,?,?)";
PreparedStatement prepareStatement = conn.prepareStatement(sql1);
prepareStatement.setString(1,strarr.StringArray[0][0]);
prepareStatement.setString(2,strarr.StringArray[0][1]);
prepareStatement.setString(3,strarr.StringArray[0][2]);
prepareStatement.setString(4,strarr.StringArray[0][3]);
prepareStatement.setString(5,strarr.StringArray[0][4]);
prepareStatement.execute();
However when I then try to insert some data into a different table, called subsubparts of the same schema in the exact same why it throws up 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 '2.1, 2.2, 2.3,
2.4, 2.5)VALUES('$$$Epidemiology of the Disease\r\nLiterature sea' at line 1
and this is on the line which executes the preparedStatement.
Below is the code of the second way which fails (if this helps):
String sql2 = "INSERT INTO subsubparts(2.1, 2.2, 2.3, 2.4, 2.5)" + "VALUES(?,?,?,?,?)";
PreparedStatement preparedStatement0 = conn.prepareStatement(sql2);
preparedStatement0.setString(1, strarr.StringArray[2][0]);
preparedStatement0.setString(2, strarr.StringArray[2][1]);
preparedStatement0.setString(3, strarr.StringArray[2][2]);
preparedStatement0.setString(4, strarr.StringArray[2][3]);
preparedStatement0.setString(5, strarr.StringArray[2][4]);
preparedStatement0.execute();
Whats even more confusing is that if throws a com.mysql.jdbc exception when I have imported import java.sql.SQLException.
Any help would be great please!!
You second query looks wrong:
INSERT INTO subsubparts(2.1, 2.2, 2.3, 2.4, 2.5) VALUES(?,?,?,?,?)
Should be changed to:
INSERT
INTO subsubparts(FieldName1,FieldName2, FieldName3, FieldName4, FieldName5)
VALUES(?,?,?,?,?)
Or if 2.1 is the name of the column then the name must be quoted e.g. "2.1"
If your Columns names are numbers like 2.1 then wrap it with quotes like,
String sql2 = "INSERT INTO subsubparts(`2.1`, `2.2`, `2.3`, `2.4`, `2.5`)"
+ "VALUES(?,?,?,?,?)";
The major difference I see between the two queries is query two has periods in the column name. MySQL won't recognize this as-is. Instead, you need to quote each of the column names with backticks:
(`2.1`, `2.2`, `2.3`, `2.4`, `2.5`)
It may also be possible to use double-quotes. See the MySQL documentation on identifiers for more detail.
I am trying a update an entry in my SQL table which has a column name "from" in JDBC.
Following is the SQL command that I am trying to execute:
sql = "Update email_template set [from]="+"'"+3+"'"+" WHERE id="+idno;
stmt.executeUpdate(sql);
However it shows the following error:
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 '[from]='Akshit' WHERE id=1' at line
MySQL's way of escaping column names is by using backticks:
sql = "Update email_template set `from`="+"'"+3+"'"+" WHERE id="+idno;
I recommend using java.sql.PreparedStatement when handling SQL in Java. It can be used for batches and ensures malicious SQL is not injected as part of the SQL code.
This is how your code looks with a PreparedStatement:
PreparedStatement stmt = connection.prepareStatement("UPDATE `email_template` SET `from` = ? WHERE id = ?");
stmt.setInt(1, 3);
stmt.setInt(2, idno);
stmt.executeUpdate();
If this is an operation you execute for many rows in one go, replace stmt.executeUpdate() with stmt.addBatch() (likely in some loop) and when you're ready to execute the batched updates you call stmt.executeBatch().
Note that both executeUpdate() and executeBatch() return how many rows were affected; which is something you may want to validate after a commit.
This works in MySql-
create database CarRentalCo;
use CarRentalCo;
create table LuxuryCars(rate varchar(5));
I tried to do the same thing in java, using jdbc and I got an error-
//Some code here
String query =
"create database CarRentalCo; "
"use CarRentalCo; "
"create table LuxuryCars(rate varchar(5)); "
//some code here
executeUpdate(query);
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 'use CarRentalCo; create table LuxuryCars(rate varchar(5))' at line 1
Some jdbc drivers have problems including a ; at the end of the statement, but the main problem is only one statement per executeUpdate.
Also i don't think it's a good idea to change the database connection through a query when you're using jdbc. Should be part of the url connect string.
I am trying to make a select from a table, which works fine with every other table in my database, but when I try the following I recieve an error:
db.makeQuery("SELECT * FROM References");
Which calls:
public ResultSet makeQuery(String query) throws Exception
{
preparedStatement = connect.prepareStatement(query);
resultSet = preparedStatement.executeQuery(query);
return resultSet;
}
It then throws the following 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 'References' at line 1
I seems very strange to me, since this statement works:
db.makeQuery("select * from Products");
References is a keyword in SQL, so you better avoid it for table names. (See for instance this documentation.)
As suggested by Nishant, you can use reserved words in queries if you escape them with backticks.
Related question:
Using MySQL keywords in a query?
use
db.makeQuery("SELECT * FROM `References`");
if you can, better, avoid having names that are MySQL keywords. As suggested by aioobe
You might be misspelling the name of your table. MySQL gives this error when it can't find that table you're referring to.
Use SHOW TABLES; to see the names of the tables in your database, and double-check the name.