Mysql
From Resin 3.0
(Difference between revisions)
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] | ||
− | + | == WEB-INF/resin-web.xml configuration == | |
<code><pre> | <code><pre> | ||
<web-app xmlns="http://caucho.com/ns/resin"> | <web-app xmlns="http://caucho.com/ns/resin"> | ||
Line 10: | Line 10: | ||
<driver> | <driver> | ||
<type>com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource</type> | <type>com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource</type> | ||
− | <url>jdbc:mysql://localhost:3306/ | + | <url>jdbc:mysql://localhost:3306/''dbname''</url> |
<user>''username''</user> | <user>''username''</user> | ||
<password>''password''</password> | <password>''password''</password> | ||
Line 17: | Line 17: | ||
</web-app> | </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> | ||
+ | <?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";
}