Problem with query in MySql - java
String sql = "SELECT siteapplications.Application, Count(visits.VisitId) AS CountOfVisitId
FROM visits, siteapplications
WHERE visits.SiteApplicationId=siteapplications.ApplicationID
and Month(visits.VisitTime)=" + month + "
and Year(visits.VisitTime)=" + year +
"GROUP BY siteapplications.Application
ORDER BY CountOfVisitId DESC;";
rs = st.executeQuery(sql);
When I run it I get this error in java :
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 'BY Application ORDER BY CountOfVisitId DESC' at line 1
I don't see a error ...
Can anyone help ...
thx all
" and Year(visits.VisitTime)=" + year + "GROUP BY ...
Should be:
" and Year(visits.VisitTime)=" + year + " GROUP BY ...
Your version is missing a space before GROUP.
Related
Users wanted a Calendar change from Saturday to Wednesday csv file export
I started off by replicating the issue that the users were having, used that in SQL developer to determine the query that would need to be changed. Then I found a query that would resolve the issue. Now I'm getting a syntax error, after I have applied the changed logic to the old query. Oracle Data base, and a JPQL Original Query #Query("SELECT DISTINCT new UiIdDescriptionDto( " + " c.weekId, " + " c.weekDate " + ") " + "FROM CalendarHierarchy c " + "WHERE c.weekId BETWEEN :weekId1 AND :weekId2 " + "ORDER BY c.weekId ASC " New Query that retrieves proper data users wanted #Query("SELECT DISTINCT new UiIdDescriptionDto( " + " c.weekId, " + " to_char(to_date(c.weekDate,'MM/DD/YYYY')-3,'MM/DD/YYYY') " + ") " + "FROM CalendarHierarchy c " + "WHERE c.weekId BETWEEN :weekId1 AND :weekId2 " + "ORDER BY c.weekId ASC " [UPDATE] So now, I don't get any errors at compile time and it query's properly to csv... however, in the ide it complains that I have 2 errors, both underlining in red the to_char open and close parenthesis. Is this something I should worry about? or leave as is?
H2 issues : request works in H2 console, not in jUnit test
I encounter a problem with H2 Database, I can't figure out what this problem is. I'm working on a 3 alphanums code generator that should behave by incrementing by one the highest existing code (this is a SQL function that will be stored on the DB server). The following code works perfectly in H2 console : SELECT TOP 1 concat(a.Chr, b.Chr, c.Chr) AS REF FROM (VALUES('0'),('1'),('2'),('3'),('4'),('5'),('6'),('7'),('8'),('9'),('A'),('B'),('C'),('D'),('E'),('F'),('G'),('H'),('I'),('J'),('K'),('L'),('M'),('N'),('O'),('P'),('Q'),('R'),('S'),('T'),('U'),('V'),('W'),('X'),('Y'),('Z')) a(Chr) CROSS JOIN (VALUES('0'),('1'),('2'),('3'),('4'),('5'),('6'),('7'),('8'),('9'),('A'),('B'),('C'),('D'),('E'),('F'),('G'),('H'),('I'),('J'),('K'),('L'),('M'),('N'),('O'),('P'),('Q'),('R'),('S'),('T'),('U'),('V'),('W'),('X'),('Y'),('Z')) b(Chr) CROSS JOIN (VALUES('0'),('1'),('2'),('3'),('4'),('5'),('6'),('7'),('8'),('9'),('A'),('B'),('C'),('D'),('E'),('F'),('G'),('H'),('I'),('J'),('K'),('L'),('M'),('N'),('O'),('P'),('Q'),('R'),('S'),('T'),('U'),('V'),('W'),('X'),('Y'),('Z')) c(Chr) WHERE concat(a.Chr,b.Chr,c.Chr) > ( SELECT TOP 1 CASE WHEN INSTRUCTION_CODE IS NULL THEN '' ELSE INSTRUCTION_CODE END FROM ACCOUNT ORDER BY INSTRUCTION_CODE DESC ) ORDER BY REF; I need to implement this request in a Java jUnit test. Here is what I did : public static ResultSet getReference(java.sql.Connection con) throws SQLException { String query = "SELECT TOP 1 concat(a.Chr, b.Chr, c.Chr) AS REF " + "FROM " + "(VALUES('0'),('1'),('2'),('3'),('4'),('5'),('6'),('7'),('8'),('9'),('A'),('B'),('C'),('D'),('E'),('F'),('G'),('H'),('I'),('J'),('K'),('L'),('M'),('N'),('O'),('P'),('Q'),('R'),('S'),('T'),('U'),('V'),('W'),('X'),('Y'),('Z')) a(Chr) " + "CROSS JOIN " + "(VALUES('0'),('1'),('2'),('3'),('4'),('5'),('6'),('7'),('8'),('9'),('A'),('B'),('C'),('D'),('E'),('F'),('G'),('H'),('I'),('J'),('K'),('L'),('M'),('N'),('O'),('P'),('Q'),('R'),('S'),('T'),('U'),('V'),('W'),('X'),('Y'),('Z')) b(Chr) " + "CROSS JOIN " + "(VALUES('0'),('1'),('2'),('3'),('4'),('5'),('6'),('7'),('8'),('9'),('A'),('B'),('C'),('D'),('E'),('F'),('G'),('H'),('I'),('J'),('K'),('L'),('M'),('N'),('O'),('P'),('Q'),('R'),('S'),('T'),('U'),('V'),('W'),('X'),('Y'),('Z')) c(Chr) " + "WHERE concat(a.Chr, b.Chr, c.Chr) > " + "(SELECT TOP 1 " + " CASE WHEN INSTRUCTION_CODE IS NULL " + " THEN '' " + " ELSE INSTRUCTION_CODE " + " END " + "FROM ACCOUNT order by INSTRUCTION_CODE DESC) " + "ORDER BY REF"; java.sql.ResultSet rs = con.createStatement().executeQuery(query); return rs; } Here's the error message I get when playing it : Caused by: org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "SELECT TOP 1 CONCAT(A.CHR, B.CHR, C.CHR) AS REF FROM (VALUES('0'),('1'),('2'),('3'),('4'),('5'),('6'),('7'),('8'),('9'),('A'),('B'),('C'),('D'),('E'),('F'),('G'),('H'),('I'),('J'),('K'),('L'),('M'),('N'),('O'),('P'),('Q'),('R'),('S'),('T'),('U'),('V'),('W'),('X'),('Y'),('Z'))A([*]CHR) CROSS JOIN (VALUES('0'),('1'),('2'),('3'),('4'),('5'),('6'),('7'),('8'),('9'),('A'),('B'),('C'),('D'),('E'),('F'),('G'),('H'),('I'),('J'),('K'),('L'),('M'),('N'),('O'),('P'),('Q'),('R'),('S'),('T'),('U'),('V'),('W'),('X'),('Y'),('Z'))B(CHR) CROSS JOIN (VALUES('0'),('1'),('2'),('3'),('4'),('5'),('6'),('7'),('8'),('9'),('A'),('B'),('C'),('D'),('E'),('F'),('G'),('H'),('I'),('J'),('K'),('L'),('M'),('N'),('O'),('P'),('Q'),('R'),('S'),('T'),('U'),('V'),('W'),('X'),('Y'),('Z'))C(CHR) WHERE CONCAT(A.CHR, B.CHR, C.CHR) > (SELECT TOP 1 CASE WHEN INSTRUCTION_CODE IS NULL THEN '' ELSE INSTRUCTION_CODE END FROM ACCOUNT ORDER BY ACBS_PAYMENT_INSTRUCTION_CODE DESC) ORDER BY REF "; Is there anything you see I didn't ? Thanx
It looks like you use a recent version of H2 when you work with H2 Console, and some old version (1.4.196 or older) in your application. Such old versions don't support the derived column list syntax. You need to use a more recent version in your application too.
Create Statement Returning ERROR: syntax error at or near "RETURNING"
I want to create a database table with a CREATE statement. I get an error Saying Error: syntax error at or near "RETURNING". I understand there is some sort of bug with the JDBC driver. As I found this. Postgres JDBC driver: PSQLException: syntax error at or near RETURNING It seems that I need to set Quirk Mode. But I'm not sure how to do that. Right Now I have Connection dbConnection; dbConnection = DriverManager.getConnection(connectionString,username,password); Statement st = dbConnection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY); st.executeUpdate(query,Statement.RETURN_GENERATED_KEYS); I dont know where I am supposed to set quirk mode EDIT: I'm sorry for missing details. It took me through the guided way. The statement is made through st.executeUpdate(query,Statement.RETURN_GENERATED_KEYS); And the SQL is String employeeTable="CREATE TABLE \"employee\" (\n" + " \"employee_id\" serial,\n" + " \"employee_first_name\" TEXT,\n" + " \"employee_middle_name\" TEXT,\n" + " \"employee_last_name\" TEXT,\n" + " \"employee_dob\" date ,\n" + " \"employee_ssn\" TEXT NOT NULL DEFAULT '0',\n" + " \"employee_wages\" FLOAT(30) NOT NULL DEFAULT '0',\n" + " \"employee_password\" TEXT DEFAULT '0',\n" + " \"employee_issupervisoer\" BOOLEAN NOT NULL DEFAULT 'false',\n" + " \"employee_hassupervisoer\" BOOLEAN NOT NULL DEFAULT 'false',\n" + " \"employee_supervisor_id\" integer,\n" + " \"employee_clockstatus\" BOOLEAN DEFAULT 'false',\n"+ // True = IN : False = OUT " \"employee_lastpunch\" timestamp, \n"+ " \"employee_isactive\" BOOLEAN, \n"+ " CONSTRAINT employee_pk PRIMARY KEY (\"employee_id\")\n" + ") WITH (\n" + " OIDS=FALSE\n" + ");\n"; Honestly I don't think that SQL matters though. This statement worked for me about 6 months back. I dont know what changed but I changed from Postgresql 9.4 to 10.7? whatever the most recent 10.x stable is. and I updated my Gradle to // https://mvnrepository.com/artifact/org.postgresql/postgresql compile group: 'org.postgresql', name: 'postgresql', version: '42.2.6'
You can not combine a CREATE TABLE statement with a RETURNING clause (as it does not "return" anything). When you call executeUpdate(query,Statement.RETURN_GENERATED_KEYS) you are requesting the generated keys from a DML statement to be returned. The Postgres JDBC driver does this by adding a RETURNING clause to the query - which obviously makes no sense with a DDL statement. Use execute(query) instead. Or executeUpdate(query) (without requesting generated keys)
How to retrieve a data between dates
I have 2 tables. A booking table and a room table. In the booking table I have the following columns: BookingID StartDate EndDate CustomerID RoomID In the Room table I have the following columns: RoomID RoomSize I am creating a booking system. I want to be able to query the database where I am able to get a list of rooms that are booked between 2 dates which are also based on size (small, medium or large) types. E.g. if user clicks on small room and enters dates between 2010-02-02 to 2010-02-25 then 4 should appear as my database contains 4 small rooms that are booked between those dates. This is what I have so far: String sqlStatement = "select RoomID from Booking where RoomID in (select Room.RoomID from Room where Room.RoomSize is " + type + ") AND ((Booking.StartDate between "+ startD +" AND " + endD + ") OR (Booking.EndDate between "+ startD + " AND " + endD + "))"; This is the error I am getting: 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 'Medium) AND ((Booking.StartDate between 2016-02-09 AND 2016-02-09) OR (Booking.E' at line 1 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:408) I am new to SQL and having trouble doing this. Also, is my logic right? startD and endD represents the dates that the user has entered and typeOfRoom represent the type of the room the user wants to book; e.g. eithier Small, Medium or Large
Do not use string concatenation to insert user-supplied values into SQL, especially for strings. It will leave you open to SQL Injection attacks, and SQL syntax issues. Use a PreparedStatement. Also, replace is with =. String sql = "select RoomID" + " from Booking" + " where RoomID in (" + "select Room.RoomID" + " from Room" + " where Room.RoomSize = ?" + ")" + " and ((Booking.StartDate between ? AND ?)" + " or (Booking.EndDate between ? AND ?))"; try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setString(1, type); stmt.setDate (2, startD); stmt.setDate (3, endD); stmt.setDate (4, startD); stmt.setDate (5, endD); try (ResultSet rs = stmt.executeQuery()) { while (rs.next()) { // code here } } }
The date handling look ok I think, but you need to quote the type string in the statement. And you should not use is, just use normal = String sqlStatement = "select RoomID from Booking where RoomID in (select Room.RoomID from Room where Room.RoomSize = '" + type + "') AND ((Booking.StartDate between "+ startD +" AND " + endD + ") OR (Booking.EndDate between "+ startD + " AND " + endD + "))";
Can you try if this statement works? I have replaced the 'is' keyword with '=' operator and put all the variables between "". String sqlStatement = "select RoomID from Booking where RoomID in (select Room.RoomID from Room where Room.RoomSize = \"" + type + "\") AND ((Booking.StartDate between \""+ startD +"\" AND \"" + endD + "\") OR (Booking.EndDate between \""+ startD + "\" AND \"" + endD + "\"))";
JDBC PreparedStatemnt MySQLSyntaxErrorException
I have read previous questions related to my query.Tried making those changes in my existing code.But still getting this error.I have spent 3 hours resolving this,but not able to catch the bug.Please help me with it. I am using PreparedStatement for sending SQL queries.Below is the query: String getExistingFileEntry = "select * from test " + " where a = ? and b = ? and date < DATE_SUB(NOW(), INTERVAL 1 DAY)" + "order by id" + "limit 1"; PreparedStatement pstVerify = null; pstVerify = con.prepareStatement(getExistingFileEntry); pstVerify.setString(1, a); pstVerify.setString(2, b); ResultSet rsFirst =null; String existingSum = null; //execute select SQL statement rsFirst = pstVerify.executeQuery(); Getting this error # rsFirst = pstVerify.executeQuery() 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 '1' at line 1 I tried running this SQL query on workbench and returns the correct rows. Can someone please point me where I am doing wrong? Thanks!
check your spaces replace this String getExistingFileEntry = "select * from test " + " where a = ? and b = ? and date < DATE_SUB(NOW(), INTERVAL 1 DAY)" + "order by id" + "limit 1"; by String getExistingFileEntry = "select * from test " + " where a = ? and b = ? and date < DATE_SUB(NOW(), INTERVAL 1 DAY)" + " order by id" + " limit 1";