sql.jsp

<%@ page contentType="text/html; charset=euc-jp" %>
<%@ page errorPage="error.jsp" %>
<%@ page import="javax.naming.*" %>
<%@ page import="allaire.taglib.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="javax.sql.*" %>
<%@ taglib uri="jruntags" prefix="jrun" %>

<script language="javascript">
function checkSize(obj_value) {
    if(obj_value.length > 10)
	{
		return false;
	}
	return true;
}
function printError(form_obj, input_obj, obj_value, error_msg) {
    alert("エラー名が長すぎます。");
    return false;
}
</script>


<%@ include file="html/header.html" %>

<b>SQL & SQLPARAM タグ: ( passing java.sql.Connection to jrun:sql )</b>( <a href="html/sql_jsp.html">ソースの表示</a> )

<%

//			"taglib" is a datasource defined by default with JRun. Check the JMC
//			or local.properties ( of default server ) for this datasource description.

			InitialContext ctx = new InitialContext();
			DataSource source = (DataSource)ctx.lookup("java:comp/env/jdbc/taglib");
			Connection connection = source.getConnection();


//		//
//		// If you want to create your own connection object...
//		//		
//	    Connection  connection = (Connection)session.getAttribute("dbconnection");      
//		Statement statement;		
//		// login
//		if ( connection == null )
//		{
//            String dburl = "jdbc:pointbase://embedded/sample";
//            String driver = "com.pointbase.jdbc.jdbcUniversalDriver";
//
//    	    Class.forName( driver ).newInstance();
//
//    	    // create the connection object...
//    	    connection = DriverManager.getConnection ( dburl, "public", "public" );
//		
//			session.setAttribute("dbconnection", connection);
//		}
//

		
String name = ParameterUtil.getParameterWithEncoding(request, response, "t1");
String amount = ParameterUtil.getParameterWithEncoding(request, response, "t2"); 
%>

<br/><br/>
<table width="100%">
<jrun:form method="post" name="form1" action="sql.jsp">
<tr>
<td>Name:</td>
<td colspan="2"><jrun:input name="t1" onValidate="checkSize" onError="printError" required="true"/></td>
</tr>
<tr>
<td>Amount:</td>
<td><jrun:input name="t2" type="float" required="true"/>&nbsp;&nbsp;&nbsp;
<input type="submit" value="送信"><br><br></td>
</tr>
</jrun:form>

</table>

<%--
	Make sure that table exists - else create it
--%>

<%
	try
	{
%>
<jrun:sql connection="<%= connection %>" id="dummy"> 
select * from ACCOUNTS
</jrun:sql>
<%
	}
	catch(Exception e)
	{
%>
<jrun:sql connection="<%= connection %>"> 
create table ACCOUNTS( name varchar2(100), amount FLOAT )
</jrun:sql>	
<%
	}
%>


<%--
	Start inserting values
--%>
<jrun:if expr="<%= name != null && amount != null %>">
<jrun:sql connection="<%= connection %>"> 
INSERT INTO ACCOUNTS (name,amount)
VALUES (
<jrun:sqlparam value="<%= name %>"/>,
<jrun:sqlparam sqltype="FLOAT" value="<%= new Float(amount) %>"/>)
</jrun:sql>
</jrun:if>

<%--
	Display Table Results
--%>
<jrun:sql connection="<%= connection %>" id="rs">
SELECT * FROM ACCOUNTS
</jrun:sql>

<br/><br/>
<table border="1" width="100%">
<tr>
<%--
<%
	QueryTable rs = (QueryTable)pageContext.getAttribute("rs");
%>
--%>
<jrun:param id="rs" type="QueryTable"/>
<jrun:foreach item="field" type="String" group="<%= rs.Names %>">
    <td align="center"><ul><b><%= field.toUpperCase() %></b></ul></td>
</jrun:foreach>
</tr>
<jrun:foreach group="page.rs">
<tr>
    <jrun:foreach item="x" group="<%= rs.Values %>">
        <td align="center"><%= x %></td>
    </jrun:foreach>
</tr>
</jrun:foreach>

</table>


<table>
<tr>
<td align="right" colspan="3"> &nbsp;<br> <font size="2"><a href="sql2.jsp">次へ</a></font> &nbsp;&nbsp;</td>
</tr>
</table>

<br/><br/>

<%@ include file="html/footer.html" %>