2012-07-27 83 views
0

我想在數據庫中插入一些會話數據。我有一個名爲SESSIONINFO的表,它有三列,即SESSIONID (text), USERID(text), LASTACCESSTIME(smalldatetime)。但是當我試圖在數據庫中插入值時,它會返回Jasper異常。JSP:MS SQL Server 2008插入查詢

JSP我使用:

<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> 

錯誤 -

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) 

我的選擇查詢工作正常這種方式,但似乎有些不妥在INSERT查詢語法。

請提出建議。感謝所有的幫助!

回答

2

您必須添加+的LastAccessTime後。

String sql = "insert into SESSIONINFO (SESSIONID, USERID, LASTACCESSTIME) values ('"+sessionID+"', '"+userName+"', '"+lastAccessTime + "')"; 

順便說一下,它仍然不起作用,因爲您不能只將日期對象連接到您的查詢。改用PreparedStatement。您可以檢查using text input to search Access date column with between/and以瞭解PreparedStatement的示例用法。

+0

您的建議似乎很有效;但如果我使用準備好的語句邏輯'pstatement.setDate(3,lastAccessTime);''其中'pstatement'是一個PS對象,m獲取'類型PreparedStatement中的方法setDate(int,Date)不適用於參數(int ,日期)' – anujin 2012-07-27 07:18:31

+0

傑伊 - 它的工作。通過導入java.sql.Date而不是java.util.Date消除了上述錯誤。謝謝哥們 !! – anujin 2012-07-27 07:27:09

0

請原諒我,如果我錯了,但在你的輸出行之間閱讀它會顯示有一個惡意的'/'在你的一個變量?它似乎將/(也可能是引號)放入INSERT語句中,而不是您想要的實際值。

我已經看得更遠了,我在上面的正確軌道上,但我認爲這是一個缺少的連接符號而不是錯誤的變量。你似乎在最後缺少一個+。

"+lastAccessTime"')"; 

應該是...

"+lastAccessTime+"')"; 
0

試試這個

String sql = "insert into SESSIONINFO (SESSIONID, USERID, LASTACCESSTIME) values (' " +sessionID + " ', ' " + userName + " ',' " + lastAccessTime+"')"