jsp servlet init 不能直接连数据库解决



package com.ibox.init;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import com.ibox.util.ServerTimer;
import com.provideo.ibox.ftp.bean.FTPBean;
import com.provideo.ibox.ftp.dao.FTPDAO;

public class InitServlet extends HttpServlet {

public InitServlet() {
super();
}

public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}

public void init() throws ServletException {
String base = "Database";
ResourceBundle resourcebundle = ResourceBundle.getBundle(base);

String projecttype = resourcebundle.getString("project.type");
String db_databaseurl = resourcebundle
.getString("default.init.dbserver.databaseurl");
String db_driver = resourcebundle
.getString("default.init.dbserver.driver");
String db_username = resourcebundle.getString("default.init.dbserver.usrname");
String db_password = resourcebundle
.getString("default.init.dbserver.password");

ServletContext application = getServletConfig().getServletContext();
application.setAttribute("projecttype", projecttype);

// Look up the JNDI data source only once at init time
String context[] = { "", "", "", "" };// new String[]{"",""}
String sql = "select * from tablea where Oid = (select tab.Oid from (select Oid from tablea limit 0,1) as tab );";
// Context envCtx = (Context) new InitialContext()
// .lookup("java:comp/env");
// DataSource datasource = (DataSource) envCtx.lookup("jdbc/mysql");
Connection con = null;
try {
Class.forName(db_driver);
con = DriverManager.getConnection(db_databaseurl, db_username,
db_password);
PreparedStatement ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
context[0] = rs.getString(1);
context[1] = rs.getString(2);
context[2] = rs.getString(3);
context[3] = rs.getString(4);
System.out.println("context[0]:" + context[0]
+ " \ncontext[1]:" + context[1] + "\ncontext[2]:"
+ context[2] + " \ncontext[3]:" + context[3]);
break;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

String realpath = this.getServletContext().getRealPath("/");
new ServerTimer(application, realpath).start();

}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值