Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed help on connecting to database using datasource connection pool
    primarykey
    data
    text
    <p>I'd like to create a simple program where it connects to a database. I don't know what the problem is.</p> <p>Here is my code: </p> <p>Products.java /<strong><em>getters and setters</em>*</strong>/</p> <hr> <pre><code>ProductsDao.java public class ProductsDao { public ArrayList getAllProducts() throws NamingException, SQLException{ Connection conn = null; Statement stmt = null; ResultSet rs = null; conn = ConnectionFactory.getConnection(); stmt = conn.createStatement(); rs = stmt.executeQuery("SELECT * FROM products"); ArrayList products = new ArrayList(); while(rs.next()){ Products product = new Products(); product.setId(rs.getInt("id")); products.add(product); } return products; } </code></pre> <p>}</p> <hr> <p>context.xml</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;Context path="/grocerific"&gt; &lt;Resource auth="Container" driverClassname="com.mysql.jdbc.Driver" name="/pool/grocerific" maxActive="100" maxIdle="30" maxWait="1000" type="javax.sql.DataSource" url="jdbc:mysql://localhost:3306/grocerific" username="root" password="secret" /&gt; &lt;/Context&gt; </code></pre> <hr> <p>ConnectionFactory.java</p> <pre><code> public static Connection getConnection() throws NamingException, SQLException{ Context ctx = new InitialContext(); DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/grocerific"); Connection conn = ds.getConnection(); return conn; } </code></pre> <hr> <pre><code> GetProductsServlet.java protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, NamingException, SQLException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { /* TODO output your page here. You may use following sample code. */ ProductsDao productsDao = new ProductsDao(); ArrayList products = productsDao.getAllProducts(); out.println(products); } finally { out.println("error!"); } } </code></pre> <hr> <p>I have also configured services from and expanded the database node and create a new connection. I tested it, and says it was successful. I've also added the library <code>mysql-jdbc-connector.zip</code> in libraries.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload