Mysql

From Resin 3.0

(Difference between revisions)
Jump to: navigation, search
 
Line 2: Line 2:
 
* See [http://caucho.com/resin/doc/config-database.xtp Resin Database Documentation]
 
* See [http://caucho.com/resin/doc/config-database.xtp Resin Database Documentation]
  
<b>WEB-INF/resin-web.xml</b>
+
== WEB-INF/resin-web.xml configuration ==
 
<code><pre>
 
<code><pre>
 
&lt;web-app xmlns="http://caucho.com/ns/resin">
 
&lt;web-app xmlns="http://caucho.com/ns/resin">
Line 10: Line 10:
 
     &lt;driver&gt;
 
     &lt;driver&gt;
 
       &lt;type&gt;com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource&lt;/type&gt;
 
       &lt;type&gt;com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource&lt;/type&gt;
       &lt;url&gt;jdbc:mysql://localhost:3306/<var>dbname</var>&lt;/url&gt;
+
       &lt;url&gt;jdbc:mysql://localhost:3306/''dbname''&lt;/url&gt;
 
       &lt;user&gt;''username''&lt;/user&gt;
 
       &lt;user&gt;''username''&lt;/user&gt;
 
       &lt;password&gt;''password''&lt;/password&gt;
 
       &lt;password&gt;''password''&lt;/password&gt;
Line 17: Line 17:
  
 
&lt;/web-app>
 
&lt;/web-app>
 +
</pre></code>
 +
 +
== Using a database in a servlet ==
 +
 +
<code><pre>
 +
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();
 +
    }
 +
  }
 +
}
 +
</pre></code>
 +
 +
== PHP/Quercus using a configured database ==
 +
 +
<code><pre>
 +
&lt;?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";
 +
}
 
</pre></code>
 
</pre></code>

Latest revision as of 05:17, 8 February 2008

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