通过JNDI获得数据

<%@ page language="java" pageEncoding="GBK"%>
<%@ page import="javax.sql.DataSource"%>
<%@ page import="java.sql.Connection"%>
<%@ page import="java.sql.ResultSet"%>
<%@ page import="java.sql.Statement"%>
<%@ page import="java.sql.SQLException"%>


<%
    String dataSource = "jdbc/sql";
    Connection conection = null;
    try
    {
       
        if (dataSource != null && !dataSource.equalsIgnoreCase(""))
        {           
            javax.naming.Context context = new javax.naming.InitialContext();
            DataSource ds = (DataSource) context.lookup(dataSource);
            conection = ds.getConnection();           
        }
       
        Statement stmt = conection.createStatement();
        ResultSet rst = stmt.executeQuery("select * from qb_mm5_list");
        System.out.println("以下是从数据库中读取出来的数据");
        System.out.println("<br>");
        while (rst.next())
        {
            System.out.println("id:" + rst.getString("id"));
            System.out.println("<br>");
        }
       
    }
    catch (javax.naming.NamingException e)
    {
        try
        {
            throw new Exception(
                    "can't get data connection,please check your datasource");
        }
        catch (Exception e1)
        {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
    catch (SQLException e)
    {
        e.printStackTrace();
    }
%>


你可能感兴趣的:(java,sql,jdbc)