openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 83a61566ea1fe3ee620c8db9609b078807f47f02
parent 93289b615a68e18a4faa1f15cfadfea2e0d31578
Author: dankert <devnull@localhost>
Date:   Sat, 16 Jan 2010 01:13:56 +0100

Beispiel- und Testdatei für JDBC-Datasources.

Diffstat:
doc/examples/jdbc/context.xml | 29+++++++++++++++++++++++++++++
doc/examples/jdbc/dbtest.jsp | 45+++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 74 insertions(+), 0 deletions(-)

diff --git a/doc/examples/jdbc/context.xml b/doc/examples/jdbc/context.xml @@ -0,0 +1,28 @@ +<!-- Sample context.xml for Tomcat Servlet Engine --> +<Context> + + <!-- Default set of monitored resources --> + <WatchedResource>WEB-INF/web.xml</WatchedResource> + + <!-- Beware: You must download the JARs with the database + driver an place them in Tomcats lib-directory --> + + <!-- Sample Oracle Datasource --> + <Resource name="jdbc/myoracledb" auth="Container" + type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver" + url="jdbc:oracle:thin:@localhost:1521:name" + username="dbuser" password="dbpassword" maxActive="20" maxIdle="10" + maxWait="-1"/> + + <!-- Sample SQlite Datasource --> + <Resource name="jdbc/mysqlitedb" auth="Container" + type="javax.sql.DataSource" driverClassName="org.sqlite.JDBC" + url="jdbc:sqlite://path/to/mydb.db" /> + + <!-- Sample PostgreSQL Datasource --> + <Resource name="jdbc/mypostgresqldb" auth="Container" + type="javax.sql.DataSource" driverClassName="org.postgresql.Driver" + url="jdbc:postgresql://127.0.0.1:5432/mydb" + username="dbuser" password="dbpass" maxActive="20" maxIdle="10" maxWait="-1"/> + +</Context>+ \ No newline at end of file diff --git a/doc/examples/jdbc/dbtest.jsp b/doc/examples/jdbc/dbtest.jsp @@ -0,0 +1,45 @@ +<html> +<title>Simple Database-Test for JDBC</title> +<body> + +<%@ page import="java.sql.*" isThreadSafe="false" %> +<%@ page import="javax.sql.*" isThreadSafe="false" %> +<%@ page import="javax.naming.*" isThreadSafe="false" %> + +<% + + final String jndiName = "java:comp/env/jdbc/mysampledb"; + Connection cn = null; + Statement st = null; + ResultSet rs = null; + try + { + InitialContext ctxt = new InitialContext(); + DataSource ds = (DataSource) ctxt.lookup(jndiName); + cn = ds.getConnection(); + st = cn.createStatement(); + rs = st.executeQuery( "select id,name from or_user" ); + ResultSetMetaData rsmd = rs.getMetaData(); + int n = rsmd.getColumnCount(); + out.println( "<table border=1 cellspacing=0><tr>" ); + for( int i=1; i<=n; i++ ) // Achtung: erste Spalte mit 1 statt 0 + out.println( "<th>" + rsmd.getColumnName( i ) + "</th>" ); + while( rs.next() ) + { + out.println( "</tr><tr>" ); + for( int i=1; i<=n; i++ ) // Achtung: erste Spalte mit 1 statt 0 + out.println( "<td>" + rs.getString( i ) + "</td>" ); + } + out.println( "</tr></table>" ); + } + finally + { + try { if( null != rs ) rs.close(); } catch( Exception ex ) {} + try { if( null != st ) st.close(); } catch( Exception ex ) {} + try { if( null != cn ) cn.close(); } catch( Exception ex ) {} + } +%> + +</body> +</html> +