无法基于属性文件连接到数据库

时间:2012-11-30 14:25:59

标签: java oracle jsp jdbc

嗨,我是java / jsp开发的新手。

我正在尝试连接到数据库,具体取决于jsp文件从上一页接收的参数。 如果我连接到像

这样的单个数据库,我的程序就可以工作
 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

我的尝试:

 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:thin@11.222.3.33:4444:ESMZ1
       user_user=user
       user_pass=upw1

       user1_conn=jdbc:oracle:thin@11.11.1.11:1111:GEPD1
       user1_user=user1
       user1_pass=upw1 

       batch_exec_path = ${SERVER_DIR}

堆栈跟踪

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)

我做的事情从根本上是错误的吗? 谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

尝试关注测试:

改变

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

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

并将您的属性放在classpath文件夹(src)

进一步改变

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

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