I am trying to execute an SQL Query to update some data in a Database column, but when I run the program I get the following error:
Hibernate: update DeputeAppeal set FilePath=/home/oleg/DeputeAppealsFiles/1 where id=38
[ERROR] [http-bio-8080-exec-2 09:21:21] (SqlExceptionHelper.java:logExceptions:131) 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 '/home/oleg/DeputeAppealsFiles/1 where id=38' at line 1
This is the method in the DAO:
public void editFilePathName(DeputeAppeal deputeAppeal, String filePathName) {
Query query = sessionDao.getSession().createSQLQuery("update DeputeAppeal set FilePath=" + filePathName + " where id=" + deputeAppeal.getId());
query.executeUpdate();
}
You missed the single quotes for 'filePathName' in your query:
Query query = sessionDao.getSession().createSQLQuery("update DeputeAppeal set FilePath=" + filePathName + " where id=" + deputeAppeal.getId());
Use this instead :
Query query = sessionDao.getSession().createSQLQuery("update DeputeAppeal set FilePath = '" + filePathName + "' where id=" + deputeAppeal.getId());
Related
I got this error i am using java and javafx and it is connected to MYsql DB i got this error while excute this statment from java to my sql please help
Got an exception!
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 'update kstds.match SET
kstds.match.Team1Goals=kstds.match.Team1Goals+1 where kst' at line 1
String Query ="use kstds; update kstds.match SET kstds.match.Team1Goals=kstds.match.Team1Goals+1 "
+ "where kstds.match.Team1ID= ( select kstds.team.TeamID from kstds.team where kstds.team.Name='AHLI' ) "
+ "and kstds.match.Matchid = 1 ; "
+ "Update kstds.match SET kstds.match.Team2Goals=kstds.match.Team2Goals+1 "
+ "where kstds.match.Team2ID= ( select kstds.team.TeamID from kstds.team where kstds.team.Name='AHLI' ) "
+ "and kstds.match.Matchid=1;";
you are trying to execute multiple sql queries which should be done using addBatch & executeBatch.
you don't have to execute use kstds because the connection to the database is set via Java
try this:
String Query1 ="update match SET match.Team1Goals=match.Team1Goals+1 "
+ "where match.Team1ID= ( select team.TeamID from team where team.Name='AHLI' ) "
+ "and match.Matchid = 1 ; "
String Query2 ="Update match SET match.Team2Goals=match.Team2Goals+1 "
+ "where match.Team2ID= ( select team.TeamID from team where team.Name='AHLI' ) "
+ "and match.Matchid=1;";
//stmt is your Statement and conn is your Connection
con.setAutoCommit(false);
stmt.addBatch(Query1);
stmt.addBatch(Query2);
stmt.executeBatch();
con.commit();
I have created the following query for PostgreSQL which is working fine in SqlWorkbench. But when I integrated this in Java code, I'm getting a syntax error exception:
Query running fine in SqlWorkbench:
ELECT poaiF.fnsku,
poaiF.acknowledgement_type_code AS last_ack_code
FROM po_acknowledgement_items poaiF,
(SELECT Max(poa.po_acknowledgement_id) AS last_po_ack_id,
poai.fnsku
FROM po_acknowledgement_items poai,
po_acknowledgements poa
WHERE poa.po_acknowledgement_id = poai.po_acknowledgement_id
AND poa.order_id = '5D7P2FLB'
GROUP BY poai.fnsku) t
WHERE t.last_po_ack_id = poaiF.po_acknowledgement_id
AND t.fnsku = poaiF.fnsku
Query in java (giving syntax error in beginning of (SELECT Max(poa.po_acknowledgement_id) AS last_po_ack_id):
private static final String LAST_ACK_CODE_QUERY1 = "SELECT poaiF.fnsku, \n" +
" poaiF.acknowledgement_type_code AS last_ack_code \n" +
"FROM po_acknowledgement_items poaiF, \n" +
" (SELECT Max(poa.po_acknowledgement_id) AS last_po_ack_id, \n" +
" poai.fnsku \n" +
" FROM po_acknowledgement_items poai, \n" +
" po_acknowledgements poa \n" +
" WHERE poa.po_acknowledgement_id = poai.po_acknowledgement_id \n" +
" AND poa.order_id = \'5D7P2FLB\' \n" +
" GROUP BY poai.fnsku) t \n" +
"WHERE t.last_po_ack_id = poaiF.po_acknowledgement_id \n" +
" AND t.fnsku = poaiF.fnsku";
While running the query through the integration test, getting the following error:
unexpected token: ( near line 4, column 8 which is the beginning of
(SELECT Max(poa.po_acknowledgement_id)
I am running through org.springframework.orm.hibernate3.LocalSessionFactoryBean which is as follows.
SessionFactory postgresSessionFactory; //initialized with org.springframework.orm.hibernate3.LocalSessionFactoryBean class.
final Session session = postgresSessionFactory.getCurrentSession();
String query = LAST_ACK_CODE_QUERY;
final Query selectQuery = session.createQuery(query).setTimeout(queryTimeout);
final List<Object[]> terms = selectQuery.list();
Please help me to resolve this issue.
From Hibernate documentation:
Query createQuery(String queryString)
Create a Query instance for the given HQL query string.
If you want a SQL query, you need to use createSQLQuery instead:
SQLQuery createSQLQuery(String queryString)
Create a SQLQuery instance for the given SQL query string.
Below I show my sql query which is dynamic in Java.
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_YEAR, -365);
Adjust to 12 months before current month with 0 hrs and min
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), 1, 0, 0, 0);
String lastYearTime = String.valueOf(cal.getTimeInMillis() / 1000);
while(iterator.hasNext())
{
tablename = Database.getTableName((String)iterator.next());
fields = "status,count(*) as count";
query.append("select " + fields + " from " + tablename + " where");
query.append(" toid ='" + collegeId + "'");
query.append(" and dtstamp >='" + lastYearTime + "'");
query.append(" group by status");
if(i.hasNext())
query.append(" UNION ");
}
StringBuffer countquery = new StringBuffer("select status, SUM(count) as count from ( " + query + ")as temp group by status ");
ResultSet rs = Database.executeQuery(countquery.toString(), connection);
In the above query, tablename will be random based on other factors. collegeId can be any id. Status can be like dropout,pursuing or any random status.
When I execute my above query, result set works fine and data is displayed. But when there are no records in the database then it throws a sql syntax error 1064 stating
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 ')as temp group by status' at line 1
Problem1
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 ')as temp group by status' at line 1
You have a problem in your query:
select status, SUM(count) as count from ( " + query + ")as temp group by status
You should to make a space in your query here :
from ( " + query + ")as
replace this ")as with this ") as
EDIT
Problem2
SQL Exception : 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 ') as temp group by status' at line 1 SQL State : 42000 Error Code : 1064
Your query is a String so you should to put it between two 'query'
now replace this :
select status, SUM(count) as count from ( " + query + ")as temp group by status
By this :
select status, SUM(count) as count from ( '" + query + "') as temp group by status
Good luck.
Resultset throws syntax error when there are no records in database
No it doesn't. It throws a syntax-error exception when you have a syntax error in your query.
I'm learning swing in java. I've make a jtable that is populated by database's values (sql table --> user(id, name, age)). I want to make jtable that is if i change values from jtable is should also update database on click on button. but where i query is executed an error occurs. I want to know what is issue with this query ???
QUERY:
String sql = "UPDATE \'alarm bell marshal\' SET mr = \'" + mrs.get(row) +
"\' , shop = \'" +shops.get(row)+ "\' where id = \'" + ids.get(row) + "\'";
ERROR:
SEVERE: null
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 ''alarm bell marshal' SET mr = '0' , shop = '0' where id = '1'' at line 1
try use something how that:
String sql = 'UPDATE `alarm bell marshal` SET `mr` = "' + mrs.get(row) + '" , `shop` = "' + shops.get(row) + '" WHERE `id` = "' + ids.get(row) + '"';
I use Java to do some SQL queries.
in general the queries that i want to perform are:
set #uid=?; set #friendsList=?; IF EXISTS(select 1 from fb_user_friends join fb_user on " +
" fb_user.id = fb_user_friends.fb_user_id where uid=#uid) then " +
"update fb_user_friends set friends = #friendsList; ELSE insert " +
"into fb_user_friends(fb_user_id,friends) values(#uid,#friendsList); END IF;
I get the MySQL Connection using:
Class.forName("com.mysql.jdbc.Driver").newInstance();
this._sqlconn = DriverManager.getConnection(this._url,this._userName,this._password);
and I try to execute the following code:
String sql="set #uid=?; set #friendsList=?; IF EXISTS(select 1 from fb_user_friends join fb_user on " +
" fb_user.id = fb_user_friends.fb_user_id where uid=#uid) then " +
"update fb_user_friends set friends = #friendsList; ELSE insert " +
"into fb_user_friends(fb_user_id,friends) values(#uid,#friendsList); END IF;";
try {
PreparedStatement stmt = this._sqlconn.prepareStatement(sql);
stmt.setLong(1,uid);
stmt.setString(2,StringUtils.join(friendsList.toArray(), ','));
stmt.executeUpdate();
}catch (SQLException e) ....
I get the exception:
class 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 'set #friendsList='110633,2018837,6813007,8803501,10711399,500061635,500214841,50'
can't I run several commands with prepareStatement ?
Do i need to find a different approach to set the MySQL variables uid and friendsList?
Thanks!
That looks a bit like a mysql stored procedure to me? If I'm not mistaken, you should register that in mysql directly. That way, you're able to call it using your prepared statement.