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();

	}

}




你可能感兴趣的:(servlet)