Exzilla.net
Contact
Home -> Articles -> Send and Receive Data in MS874 Streams for JSP/Servlet Applications
 
Features
Forums
Files
Blogs
Sample Code: send and Receive Data in MS874 Streams

<%@ page contentType="text/html;charset=WINDOWS-874"%>

<%@ page import="java.sql.*" %>
<!-- send and Receive Data in MS874 Streams -->
<!-- For CHAR,VARCHAR2 testing -->

<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=WINDOWS-874">
<META NAME="GENERATOR" CONTENT="Oracle JDeveloper">
<TITLE>
Send and Receive Data in MS874 Streams
</TITLE>
</HEAD>

<BODY>
<h2>The following output is from JSP code:</h2>
<P><% out.println(" Test thinJDBC,send and receive data in MS874 streams "); %></P>
<!-- part I: connection creation -->

<%
Connection conn = null;

try

{
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@archer.exzilla.net :1521:venom","scott",
"tiger");
}

catch(ClassNotFoundException e)

{
out.println("ClassNotFoundException: " + e.getMessage() + "<BR>");
}

%>

<!-- end part I: connection creation -->
<!-- start part II: get parameter from user form -->

<%

String omyEmpno=request.getParameter("EMPNO");
String omyEname=request.getParameter("ENAME");
String omyJob=request.getParameter("JOB");
String omySal=request.getParameter("SAL");
String omyHiredate=request.getParameter("HIREDATE");

%>

<% if (omyEmpno==null) { omyEmpno=";)"; } %>
<% if (omyEname==null) { omyEname=";)"; } %>
<% if (omyJob==null){ omyJob=";)"; } %>
<% if (omySal==null){ omySal=";)"; } %>
<% if (omyHiredate==null){ omyHiredate=":)";} %>

<%

String myEmpno = new String(omyEmpno.getBytes("ISO8859_1"),"MS874");
String myEname = new String(omyEname.getBytes("ISO8859_1"),"MS874");
String myJob= new String(omyJob.getBytes("ISO8859_1"),"MS874");
String mySal= new String(omySal.getBytes("ISO8859_1"),"MS874");
String myHiredate= new String(omyHiredate.getBytes("ISO8859_1"),"MS874");

%>

<!-- end part II: get parameter from user form -->
<!-- start part III: Insert data from html form to database -->

<%

if (myEmpno.compareTo(";)")!=0)
{
out.println(" Print from myEmpno :: "+myEmpno);
try

{
PreparedStatement pstmt = conn.prepareStatement("insert into " +
"EMP(EMPNO,ENAME,JOB,SAL,HIREDATE,DEPTNO) VALUES(?,?,?,?,SYSDATE,10)");
pstmt.setString(1,myEmpno);
pstmt.setString(2,myEname);
pstmt.setString(3,myJob);
pstmt.setInt(4,1000);
pstmt.execute();

}

catch(SQLException e)

{

out.println("SQLException: " + e.getMessage() + "<BR>");
while((e = e.getNextException()) != null)
out.println(e.getMessage() + "<BR>");

}

} // end if

%>

<!-- end part III: Insert data from html form to database -->
<!-- start start IV: Query all data from current table -->

<%

try
{
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT EMPNO,ENAME,JOB,SAL,HIREDATE FROM scott.emp");
//Print start of table and column headers
out.println("<TABLE CELLSPACING=\"0\" CELLPADDING=\"3\" BORDER=\"1\">");
out.println("<TR><TH>ID</TH><TH>NAME</TH><TH>SURNAME</TH>");
out.println(" <TH>SALARY</TH><TH>STARTDATE</TH></TR>");
//Loop through results of query.
while(rs.next())

{

out.println("<TR>");
out.println(" <TD>" + rs.getString("EMPNO") + "</TD>");
out.println(" <TD>" + rs.getString("ENAME") + "</TD>");
out.println(" <TD>" + rs.getString("JOB") + "</TD>");
out.println(" <TD>" + rs.getInt("SAL") + "</TD>");
out.println(" <TD>" + rs.getString("HIREDATE") + "</TD>");
out.println("</TR>");

}

out.println("</TABLE>");
}

catch(SQLException e)
{
out.println("SQLException: " + e.getMessage() + "<BR>");
while((e = e.getNextException()) != null)
out.println(e.getMessage() + "<BR>");
}

finally
{
//Clean up resources, close the connection.
if(conn != null)

{
try
{
conn.close();
}
catch (Exception ignored) {}
}

}

%>
<!-- end part IV: Query all data from current table -->
<!-- start part V: HTML input form -->
<!-- <form method="get" action=""> -->

<h2>Input Form </h2>
<form method="get">
<table width="100%" border="0" bgcolor="#CCCCFF">

<tr>
<td>EMPNO</td>
<td>
<input type="text" name="EMPNO">
</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>ENAME</td>
<td>
<input type="text" name="ENAME">
</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>JOB</td>
<td>
<input type="text" name="JOB">
</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>SAL</td>
<td>
<input type="text" name="SAL">
</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>HIREDATE</td>
<td>
<input type="text" name="HIREDATE">
</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>
<input type="submit" name="Submit" value="Submit">
<input type="reset" name="Submit2" value="Reset">
</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>

</tr>
</table>
</form>

<!-- end part V: HTML input form -->
<!-- start part VI: Display current encoding and language environment -->
<h2>Current encoding and language environment.</h2>
File encoding is
<%= System.getProperty("file.encoding") %> ,
User language is
<%= System.getProperty("user.language") %> <p>
<!-- end part VI: Display current encoding and language environment -->
</BODY>
</HTML>

 




Copyright (c) 2001-2005 - Exzilla.net -  All Rights Reserved.
Contact Us | Privacy Policy | Terms & Conditions