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
Related
I am trying to make a project in java but I'm having problems with MySQL, after trying to get the material inside the table I get this error message:
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 'table' at line 1
String sql = "SELECT Name, OS, Disk, Office, Serialnomain, Serialnotast, Serialnoscreen, Serialnomouse FROM table ";
ResultSet rs = stmt.executeQuery(sql);
Change your name from table to anything else and will work fine.
table is a reserved word in mariadb
so rename your table and it will run
The SQL Language has some keywords which you should not use in your statement, table is one of them just rename your table to myTable
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.
I am connecting to a DB2 database (DB2 v9.7.400.501) from my Java web application using the IBM DB2 Type 4 driver (db2jcc4.jar). When I try to execute an SQL statement like this,
SELECT * FROM USERS WHERE UPPER(USERNAME) = UPPER('testuser');
I get the following exception:
com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error:
SQLCODE=-104, SQLSTATE=42601, SQLERRMC=;;=
UPPER('testuser');END-OF-STATEMENT, DRIVER=4.12.55
The problem is from the UPPER function since, a normal select statement executes normally.
Maybe you should use is this way:
SELECT * FROM USERS WHERE UPPER(USERNAME) LIKE UPPER('testuser');
Your code with '=' is seems ok for SQLite but don't know anbout db2.
UPD. After some investigation, I can say that error is cause by Java code which tries to execute multiple statements in one query using ';' as a delimiter.
You should try using the PreparedStatement, addBatch() and executeBatch() for multiple statements.
UPD2. This is DB2 related issue. PostgreSQL, afaik, allows multiple statements in single query.
sql = "select milk_rate from special_milk_rate
where code_producer_id=? and effective_from <= ?
and effective_till >= ?"
I am getting error in this query when in my JDBC code
Exception: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 '? and effective_from <= ? and effective_till >= ?' at line 1
But when I run it in MySQL command prompt with some values it runs fine there.
ResultSet rst= prest.executeQuery(sql);
This is incorrect. It should be executeQuery().
The sql gets passed to execute when using a Statement. It gets passed to the PreparedStatement instead when using binding variables. Since you are using the Statement API, JDBC is executing it without substituting in your binding variables and wondering what to do with the literal question marks.
You have to use prepared statements instead of "normal" statements to have the question marks replaced by parameters.
String myQuery1 = "insert into mytable(mycol) values(myval) \ngo";
String myQuery2 = "insert into mytable(mycol) values(myval2) \ngo";
String myQuery = myQuery1 + myQuery2;
Query query = myEntityManager.createNativeQuery(myQuery);
List<?> insertResultList = queryInsertDefaults.getResultList();
using the eclpise debugger I can see the string used, it works fine when I copy and paste into sql server management studio - so I am guessing there is something to do with entity manger that doesn't like multiple line statements/go ... ?
Any advice gratefully recieved (yeah I know about stringbuilder etc etc), and the error I get is :
SQL Error: 102, SQLState: S0001
Incorrect syntax near 'go'.
EDIT
turns out insert is not supported by entitymanager and query class. So I have to use either a prepared statement or persist the object.
From the MSSQL documentation: "GO is not a Transact-SQL statement; it is a command recognized by the sqlcmd and osql utilities and SQL Server Management Studio Code editor."
That's why it works in SSMS but not when sent 'directly' to the database. Just remove it from your INSERT statements completely.