I want to insert some session data in a database. I have one table called SESSIONINFO which has three columns, namely SESSIONID (text), USERID(text), LASTACCESSTIME(smalldatetime). But when I am trying to insert values in the database, it returns Jasper exception.
JSP that I am using :
<html>
<head><title>Enter to database</title></head>
<body>
<table>
<%@ page import="java.util.*" %>
<%@ page import="javax.sql.*;" %>
<%
java.sql.Connection con;
java.sql.Statement s;
java.sql.ResultSet rs;
java.sql.PreparedStatement pst;
con=null;
s=null;
pst=null;
rs=null;
Date lastAccessTime = new Date(session.getLastAccessedTime());
//lastAccessTime = String("lastAccessTime");
String sessionID = session.getId();
String userName ="anni";
// Remember to change the next line with your own environment
String url=
"jdbc:jtds:sqlserver://localhost/someDB";
String id= "testuser";
String pass = "testpasswd";
try{
Class.forName("net.sourceforge.jtds.jdbc.Driver");
con = java.sql.DriverManager.getConnection(url, id, pass);
}catch(ClassNotFoundException cnfex){
cnfex.printStackTrace();
}
String sql = "insert into SESSIONINFO (SESSIONID, USERID, LASTACCESSTIME) values ('"+sessionID+"', '"+userName+"', '"+lastAccessTime"')";
try{
s = con.createStatement();
rs = s.executeQuery(sql);
}
catch(Exception e){e.printStackTrace();}
finally{
if(rs!=null) rs.close();
if(s!=null) s.close();
if(con!=null) con.close();
}
%>
</body>
</html>
Error –
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 38 in the jsp file: /pages/insertsql.jsp
Syntax error on token ""\')"", delete this token
35: cnfex.printStackTrace();
36: }
37:
38: String sql = "insert into SESSIONINFO (SESSIONID, USERID, LASTACCESSTIME) values ('"+sessionID+"', '"+userName+"', '"+lastAccessTime"')";
39: try{
40: s = con.createStatement();
41: rs = s.executeQuery(sql);
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:93)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:451)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:328)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:307)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:295)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:565)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:308)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:259)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
My select query works fine this way but seems there is something wrong in INSERT query syntax.
Kindly suggest. Thanks for all the help !!
You must add + after lastAccessTime.
by the way, it will still not work because you cannot just concatenate the Date object to your query. use PreparedStatement instead. you may check using text input to search Access date column with between/and for sample usage of PreparedStatement.