2016-09-21 40 views
0

我重新發布我的問題SQL數據源異常

我有一個數據庫對象,其中數據庫連接可以通過它設置爲上下文參數數據庫對象建立。

ServletContext s=e.getServletContext();  
    Class.forName("com.mysql.jdbc.Driver"); 
    Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/job_portal","root","root"); 
    s.setAttribute("db_connect",con); 

我想使用SQL標籤庫來連接數據庫使用數據庫對象在我的JSP頁面和存儲數據

<sql:update dataSource="${applicationScope.db_connect}" > 
    insert into linkedin_table values(?,?) 
    <sql:param value="${param.Reference_Person}" /> 
    <sql:param value="${param.Reference_Person_Position}" /> 
    </sql:update> 

但其拋出異常

javax.servlet.ServletException: javax.servlet.jsp.JspException: 'dataSource' is neither a String nor a javax.sql.DataSource 
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:911) 
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:840) 

回答

0

那是因爲你傳遞連接,而不是數據源。

我沒有看到在JSP中連接到數據庫的正當理由。但爲了討論的方便,你可以這樣做:爲解釋

DataSource dataSource = new BasicDataSource(); 

dataSource.setDriverClassName("com.mysql.jdbc.Driver"); 
dataSource.setUsername("root"); 
dataSource.setPassword("root"); 
dataSource.setUrl("jdbc:mysql://localhost:3306/job_portal"); 

ServletContext s=e.getServletContext(); 
s.setAttribute("db_connect",con); 
+0

坦,我明白您的explanation.is有區別的任何其他方式,我可以使用JSP中數據庫對象,並建立連接 – Ramgopal

+0

你不應該」在JSP中根本不使用DB對象。 JSP是用於渲染的東西。將DB邏輯保留在服務器中,並僅將它需要呈現的數據傳遞給JSP。 –