Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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 'group,time,lecturer)values('dzzzzzzc','876',null)' at line 1


What I have tried:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>

<%
    if(request.getParameter("submit")!=null)
    {
        String gr = request.getParameter("grp");
        String tym = request.getParameter("time");
        String lec = request.getParameter("lecturer");
        
        Connection con;
        PreparedStatement pst;
        ResultSet rs;
        
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://localhost/englishacademy","root","");
        pst=con.prepareStatement("insert into schedule(group,time,lecturer)values(?,?,?)");
        pst.setString(1, gr);
        pst.setString(2, tym);
        pst.setString(3, lec);
        pst.executeUpdate();
        
%>
        
<script>
    alert("Record Added To The Database");
</script>
<%
    }
%>
Posted
Updated 23-Apr-20 2:38am

group is a reserved name so it needs to be quoted when using it as column name. Better is to choose another name for the column.

See here for more: Reserved Words - MariaDB Knowledge Base[^]

Note: time is basically also reserved but will be handled as exception for historical reasons.
 
Share this answer
 
v2
Comments
MadMyche 23-Apr-20 9:29am    
+5 for typing faster than me
BinthLafeer 23-Apr-20 10:12am    
thank you it's working now.
Maciej Los 23-Apr-20 14:49pm    
5ed!
[no name] 23-Apr-20 14:55pm    
Thank you Maciej
The problem you are having is that you are using Reserved words in the table definition, so when they are being called in a statement they will need to be escaped.

Most IDEs for a language will highlight reserved/special/key- words and my general rule is to use different names when the colors change. This can be evidenced even in the CodeProject editor
SQL
insert into schedule(group,time,lecturer)values(...
Different DB Servers use different characters to escape reserved words; I believe MariaDB uses a back-tick to escape these, so your query would look like this
SQL
insert into schedule(`group`,`time`,lecturer)values(...
Here is the list of the words that MariaDB considers special:
Reserved Words - MariaDB Knowledge Base[^]
 
Share this answer
 
Comments
[no name] 23-Apr-20 9:38am    
+5 for describing it much more precise than me :-)
MadMyche 23-Apr-20 9:45am    
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900