import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; public class Test { private static DataSource pool; public static void main(String[] args) { Context env = null; try { env = (Context) new InitialContext().lookup("java:comp/env"); pool = (DataSource)env.lookup("jdbc/DBPool"); if(pool==null) System.err.println("'DBPool' is an unknown DataSource"); } catch(NamingException ne) { ne.printStackTrace(); } Connection conn; try { conn = pool.getConnection(); String sql = "select * from allbook"; PreparedStatement ps; ps = conn.prepareStatement(sql); ResultSet rs=ps.executeQuery(); while(rs.next()){ System.out.println(rs.getString("BOOKNAME")); } } catch (SQLException e) { e.printStackTrace(); } } } |