473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Convert This Java Code To Jsp Code

HELLO FRIENDS

I HAVE ONE JAVA CODE WHICH PARSES AN XML FILE AND PRODUCE A QUERY TO CREATE TABEL IN A DATABASE.

BUT I NEED THE CODE TO BE USED IN A JSP PAGE.

HOW TO USE THIS PAGE WITH JSP.

I AM USING TOMCAT 4.1 WEBSERVER FOR COMPILING JSP PAGES.

WHETHER IT SUPPORT JAVA BEANS WITH JSP OR NOT..



THE CODE IS AS FOLLOWS


import java.io.*;

import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import java.sql.*;

public class xmltodb extends DefaultHandler
{
static int rr;
static String sql,tab,rec;
static Connection con;
static Statement st;
static ResultSet rs;
static boolean newt, beg;
static int fff;
public static void main(String argv[])
{
if (argv.length != 1) {
System.err.println("Usage: cmd filename");
System.exit(1);
}
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:virtual ");
Statement st=con.createStatement();
}
catch(Exception e)
{
System.out.println(e);
}
rr=1;
sql="create table ";


DefaultHandler handler = new xmltodb();

SAXParserFactory factory = SAXParserFactory.newInstance();
try {

out = new OutputStreamWriter(System.out, "UTF8");


SAXParser saxParser = factory.newSAXParser();
saxParser.parse( new File(argv[0]), handler);

} catch (Throwable t) {
t.printStackTrace();
}
System.exit(0);
}

static private Writer out;





public void startDocument()
throws SAXException
{
nl();
}

public void endDocument()
throws SAXException
{
try {
sql=sql+")";
System.out.println("\n SQL="+sql);
nl();
out.flush();
} catch (IOException e) {
throw new SAXException("I/O error", e);
}
catch(Exception ee){System.out.println(ee);}
}

public void startElement(String namespaceURI,
String lName,
String qName,
Attributes attrs)
throws SAXException
{
String eName = lName;
if ("".equals(eName)) eName = qName;
if(rr==1)
{
tab=eName;
sql=sql+tab+" (";
newt=true;
fff=0;
beg=true;
}
else
{
if (attrs != null) {
if(attrs.getLength()==0 && newt)
{
rec=eName;
}
for (int i = 0; i < attrs.getLength(); i++) {
String aName = attrs.getLocalName(i);
if ("".equals(aName)) aName = attrs.getQName(i);
if(newt && beg)
{
if(aName.equals("type"))
{
String val=attrs.getValue(i);
if(val.equals("Text")) val="VARCHAR";
if(fff!=0) sql=sql+", ";
sql=sql+eName+" "+val;
fff++;
}
}
else
{
newt=false;
}
}
}
}
rr++;
}

public void endElement(String namespaceURI,
String sName,
String qName
)
throws SAXException
{
if("".equals(sName)) sName=qName;
if(sName.equals(tab)) newt=false;
if(sName.equals(rec)) beg=false;
}

public void characters(char buf[], int offset, int len)
throws SAXException
{
String s = new String(buf, offset, len);
}

private void emit(String s)
throws SAXException
{
try {
out.write(s);
out.flush();
} catch (IOException e) {
throw new SAXException("I/O error", e);
}
}


private void nl()
throws SAXException
{
String lineEnd = System.getProperty("line.separator");
try {
out.write(lineEnd);
} catch (IOException e) {
throw new SAXException("I/O error", e);
}
}
}



THANKS IN ADVANCE

BY

SADIQ
Mar 15 '07 #1
4 2480
r035198x
13,262 8TB
HELLO FRIENDS

I HAVE ONE JAVA CODE WHICH PARSES AN XML FILE AND PRODUCE A QUERY TO CREATE TABEL IN A DATABASE.

BUT I NEED THE CODE TO BE USED IN A JSP PAGE.

HOW TO USE THIS PAGE WITH JSP.

I AM USING TOMCAT 4.1 WEBSERVER FOR COMPILING JSP PAGES.

WHETHER IT SUPPORT JAVA BEANS WITH JSP OR NOT..



THE CODE IS AS FOLLOWS


import java.io.*;

import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import java.sql.*;

public class xmltodb extends DefaultHandler
{
static int rr;
static String sql,tab,rec;
static Connection con;
static Statement st;
static ResultSet rs;
static boolean newt, beg;
static int fff;
public static void main(String argv[])
{
if (argv.length != 1) {
System.err.println("Usage: cmd filename");
System.exit(1);
}
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:virtual ");
Statement st=con.createStatement();
}
catch(Exception e)
{
System.out.println(e);
}
rr=1;
sql="create table ";


DefaultHandler handler = new xmltodb();

SAXParserFactory factory = SAXParserFactory.newInstance();
try {

out = new OutputStreamWriter(System.out, "UTF8");


SAXParser saxParser = factory.newSAXParser();
saxParser.parse( new File(argv[0]), handler);

} catch (Throwable t) {
t.printStackTrace();
}
System.exit(0);
}

static private Writer out;





public void startDocument()
throws SAXException
{
nl();
}

public void endDocument()
throws SAXException
{
try {
sql=sql+")";
System.out.println("\n SQL="+sql);
nl();
out.flush();
} catch (IOException e) {
throw new SAXException("I/O error", e);
}
catch(Exception ee){System.out.println(ee);}
}

public void startElement(String namespaceURI,
String lName,
String qName,
Attributes attrs)
throws SAXException
{
String eName = lName;
if ("".equals(eName)) eName = qName;
if(rr==1)
{
tab=eName;
sql=sql+tab+" (";
newt=true;
fff=0;
beg=true;
}
else
{
if (attrs != null) {
if(attrs.getLength()==0 && newt)
{
rec=eName;
}
for (int i = 0; i < attrs.getLength(); i++) {
String aName = attrs.getLocalName(i);
if ("".equals(aName)) aName = attrs.getQName(i);
if(newt && beg)
{
if(aName.equals("type"))
{
String val=attrs.getValue(i);
if(val.equals("Text")) val="VARCHAR";
if(fff!=0) sql=sql+", ";
sql=sql+eName+" "+val;
fff++;
}
}
else
{
newt=false;
}
}
}
}
rr++;
}

public void endElement(String namespaceURI,
String sName,
String qName
)
throws SAXException
{
if("".equals(sName)) sName=qName;
if(sName.equals(tab)) newt=false;
if(sName.equals(rec)) beg=false;
}

public void characters(char buf[], int offset, int len)
throws SAXException
{
String s = new String(buf, offset, len);
}

private void emit(String s)
throws SAXException
{
try {
out.write(s);
out.flush();
} catch (IOException e) {
throw new SAXException("I/O error", e);
}
}


private void nl()
throws SAXException
{
String lineEnd = System.getProperty("line.separator");
try {
out.write(lineEnd);
} catch (IOException e) {
throw new SAXException("I/O error", e);
}
}
}



THANKS IN ADVANCE

BY

SADIQ
JSPs are used for displaying. Do you want to display the data being printed to the console on a JSP page?
Do you have the J2EE compiler installed together with the server?
Mar 15 '07 #2
I would like to have the output of the java program to be used in the jsp page.

I am having Apache Tom Cat 4.1 Webserver. It compiles the jsp page as well as run it.

By
Sadiq
Mar 16 '07 #3
Quit programming and look for a real job man
Mar 16 '07 #4
r035198x
13,262 8TB
Quit programming and look for a real job man
What's that for? Becareful what you post in the technical forums. This kind of tone is not allowed here.


@OP You can print on a JSP by using
Expand|Select|Wrap|Line Numbers
  1. <%=stringVariable%>
or by using the implicitly defined variable out.

Just say
Expand|Select|Wrap|Line Numbers
  1. out.println(stringVariable); 
and that out put is sent to the JSP page
Mar 16 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Danny | last post by:
I am new to java/javascript. I wrote javascript to proces a cookie. I don't like that fact that the code is visible to all. Can I create a java application that resides on the server and call it...
1
by: Jens Mueller | last post by:
Hi there, this is a Java-XML Question, so I am not sure whether this is the right place, haven't found anything better .... I try to convert a Java object to XML via SAX and let the FOP...
3
by: Mike | last post by:
Hi, Does anyone know of reliable programs that convert C# to Java and viceversa? Thanks Mike
2
by: Barney | last post by:
I have to convert a Java app to .NET, i found and used the conversion tool which converts java to C#, but it only converted about 10% of it, how hard would it be to re-write the java app and keep...
0
by: NoSpamForMe | last post by:
I am currently a java programmer, with some knowledge of the C# language but very little knowledge about ASP.NET, and I am know wondering if someone can *translate* the java servlet code below to...
2
by: Vinny Vinn | last post by:
I have to convert some JSP's to C#(ASP.Net),I know there are a number of tools to do this task including the Microsoft Java Language Conversion Assistant 2.0. I a looking for some...
17
by: Terry Jolly | last post by:
New to C# ---- How do I convert a Date to int? In VB6: Dim lDate as long lDate = CLng(Date) In C#
3
by: GM | last post by:
Dear all, Could you all give me some guide on how to convert my big5 string to unicode using python? I already knew that I might use cjkcodecs or python 2.4 but I still don't have idea on what...
6
by: nanaveraa | last post by:
Hai, i am tried to convert a .doc file into .rtf format file using jacob jar.i am written a code but its gives some error. In below i given that programs and error.please give me the solution...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.