Mysql

From Resin 3.0

Jump to: navigation, search

WEB-INF/resin-web.xml configuration

<web-app xmlns="http://caucho.com/ns/resin">

  <database>
    <name>jdbc/mysql</jndi-name>
    <driver>
      <type>com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource</type>
      <url>jdbc:mysql://localhost:3306/''dbname''</url>
      <user>''username''</user>
      <password>''password''</password>
    </driver>
  </database>

</web-app>

Using a database in a servlet

package demo;

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.sql.*;
import javax.webbeans.*;

public class MyServlet extends GenericServlet
 {
  @Named("jdbc/mysql") private DataSource _database;

  ...

  private void doQuery(PrintWriter out)
    throws IOException, SQLException
  {
     PrintWriter out = res.getWriter();

     Connection conn = _database.getConnection();
     try {
        Statement stmt = conn.createStatement();
        ResultSet rs;
        rs = stmt.executeQuery("SELECT name, price FROM brooms");

       while (rs.next()) {
          out.println(rs.getString(1) + " " + rs.getString(2));
       }
     } finally {
       conn.close();
     }
  }
}

PHP/Quercus using a configured database

<?php

$db = new PDO("java:comp/env/jdbc/mysql");

$stmt = $db->query("SELECT name, price FROM brooms");

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  echo $row['name'] . " " . $row['price'] . "\n";
}
Personal tools