2012-11-30 82 views
-3

嗨,我是在java/jsp開發新。無法連接到基於屬性文件的數據庫

我試圖根據jsp文件從前一頁接收的參數連接到數據庫。

String userinfo = request.getParameter("userinfo"); 
try { 
     InputStream in = getServletContext().getResourceAsStream("/WEB-INF/db.properties"); 
     properties.load(in); 
     db_conn = userinfo + "_conn"; 
     db_user = userinfo + "_user"; 
     db_pass = userinfo + "_pass"; 
     connString = properties.getProperty(db_conn).toString(); 
     connUser = properties.getProperty(db_user).toString(); 
     connPass = properties.getProperty(db_pass).toString(); 
     bPath = properties.getProperty("exec_path").toString(); 
     in.close(); 
     } catch (IOException ex) { 
     System.out.println(ex); 
     } 

     try { 
     // --- Required database stuff --- 
     DriverManager.registerDriver(new oracle.jdbc.OracleDriver()); 
     Connection conn = DriverManager.getConnection(connString, connUser, connPass); 
     Statement stmt = conn.createStatement(); 
     More codes.... 
     } 

db.properties

 guest_conn=jdbc:oracle:thin:@33.333.3.33:3333:DKSE1 
     guest_user=guest 
     guest_pass=guestpw 

     user_conn=jdbc:oracle:[email protected]:4444:ESMZ1 
     user_user=user 
     user_pass=upw1 

     user1_conn=jdbc:oracle:[email protected]:1111:GEPD1 
     user1_user=user1 
     user1_pass=upw1 

     batch_exec_path = ${SERVER_DIR} 
: 如果我連接到單個DB像

try { 
     InputStream in = getServletContext().getResourceAsStream("/WEB-INF/db.properties"); 
     properties.load(in); 
     connString = properties.getProperty("db_conn").toString(); 
     connUser = properties.getProperty("db_user").toString(); 
     connPass = properties.getProperty("db_pass").toString(); 
     bPath = properties.getProperty("exec_path").toString(); 
     in.close(); 
     } catch (IOException ex) { 
     System.out.println(ex); 
     } 

和db.properties文件包含

 db_conn=jdbc:oracle:thin:@33.333.3.33:3333:DKSE1 
     db_user=guest 
     db_pass=guestpw 

我嘗試我的項目工程

Sta cktrace

Nov 30, 2012 9:03:55 AM org.apache.catalina.core.StandardWrapperValve invoke 
SEVERE: Servlet.service() for servlet [jsp] in context with path [/dashboard] threw exception [An exception occurred processing JSP page /alpharunBatch.jsp at line 106 

103:    db_conn = userinfo + "_conn"; 
104:    db_user = userinfo + "_user"; 
105:    db_pass = userinfo + "_pass"; 
106:    connString = properties.getProperty(db_conn).toString(); 
107:    connUser = properties.getProperty(db_user).toString(); 
108:    connPass = properties.getProperty(db_pass).toString(); 
109:    bPath = properties.getProperty("exec_path").toString(); 


Stacktrace:] with root cause 
java.lang.NullPointerException 
    at org.apache.jsp.alpharunBatch_jsp._jspService(alpharun_jsp.java:192) 
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432) 
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) 
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98) 
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) 
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999) 
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565) 
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
    at java.lang.Thread.run(Unknown Source) 

我在做一些根本性的錯誤嗎? 感謝您的幫助!測試

+0

不要混用的問題。您在檢索屬性時遇到問題,您將在以後使用JDBC的這些屬性並不相關。在請求答案之前嘗試找出你最好的問題;在這種情況下很容易找到,但更復雜的問題可能會得不到答案。 – SJuan76

回答

0

嘗試以下操作:

變化是

InputStream in = getServletContext().getResourceAsStream("/WEB-INF/db.properties"); 

InputStream in = getServletContext().getResourceAsStream("db.properties"); 

,並把你的屬性在類路徑文件夾(SRC)

而且改變

properties.getProperty("exec_path").toString(); 

properties.getProperty("batch_exec_path").toString(); 
+0

nope :(不起作用 – user1516649

+1

String userinfo = request.getParameter(「userinfo」); 返回值「user」??? –

+0

是的,它返回user,user1,guest – user1516649