473,569 Members | 2,463 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Convert This Java Code To Jsp Code

38 New Member
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.hel pers.DefaultHan dler;

import javax.xml.parse rs.SAXParserFac tory;
import javax.xml.parse rs.ParserConfig urationExceptio n;
import javax.xml.parse rs.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.prin tln("Usage: cmd filename");
System.exit(1);
}
try
{
Class.forName(" sun.jdbc.odbc.J dbcOdbcDriver") ;
Connection con=DriverManag er.getConnectio n("jdbc:odbc:vi rtual");
Statement st=con.createSt atement();
}
catch(Exception e)
{
System.out.prin tln(e);
}
rr=1;
sql="create table ";


DefaultHandler handler = new xmltodb();

SAXParserFactor y factory = SAXParserFactor y.newInstance() ;
try {

out = new OutputStreamWri ter(System.out, "UTF8");


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

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

static private Writer out;





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

public void endDocument()
throws SAXException
{
try {
sql=sql+")";
System.out.prin tln("\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(St ring namespaceURI,
String lName,
String qName,
Attributes attrs)
throws SAXException
{
String eName = lName;
if ("".equals(eNam e)) eName = qName;
if(rr==1)
{
tab=eName;
sql=sql+tab+" (";
newt=true;
fff=0;
beg=true;
}
else
{
if (attrs != null) {
if(attrs.getLen gth()==0 && newt)
{
rec=eName;
}
for (int i = 0; i < attrs.getLength (); i++) {
String aName = attrs.getLocalN ame(i);
if ("".equals(aNam e)) aName = attrs.getQName( i);
if(newt && beg)
{
if(aName.equals ("type"))
{
String val=attrs.getVa lue(i);
if(val.equals(" Text")) val="VARCHAR";
if(fff!=0) sql=sql+", ";
sql=sql+eName+" "+val;
fff++;
}
}
else
{
newt=false;
}
}
}
}
rr++;
}

public void endElement(Stri ng namespaceURI,
String sName,
String qName
)
throws SAXException
{
if("".equals(sN ame)) 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.getPrope rty("line.separ ator");
try {
out.write(lineE nd);
} catch (IOException e) {
throw new SAXException("I/O error", e);
}
}
}



THANKS IN ADVANCE

BY

SADIQ
Mar 15 '07 #1
4 2492
r035198x
13,262 MVP
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.hel pers.DefaultHan dler;

import javax.xml.parse rs.SAXParserFac tory;
import javax.xml.parse rs.ParserConfig urationExceptio n;
import javax.xml.parse rs.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.prin tln("Usage: cmd filename");
System.exit(1);
}
try
{
Class.forName(" sun.jdbc.odbc.J dbcOdbcDriver") ;
Connection con=DriverManag er.getConnectio n("jdbc:odbc:vi rtual");
Statement st=con.createSt atement();
}
catch(Exception e)
{
System.out.prin tln(e);
}
rr=1;
sql="create table ";


DefaultHandler handler = new xmltodb();

SAXParserFactor y factory = SAXParserFactor y.newInstance() ;
try {

out = new OutputStreamWri ter(System.out, "UTF8");


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

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

static private Writer out;





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

public void endDocument()
throws SAXException
{
try {
sql=sql+")";
System.out.prin tln("\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(St ring namespaceURI,
String lName,
String qName,
Attributes attrs)
throws SAXException
{
String eName = lName;
if ("".equals(eNam e)) eName = qName;
if(rr==1)
{
tab=eName;
sql=sql+tab+" (";
newt=true;
fff=0;
beg=true;
}
else
{
if (attrs != null) {
if(attrs.getLen gth()==0 && newt)
{
rec=eName;
}
for (int i = 0; i < attrs.getLength (); i++) {
String aName = attrs.getLocalN ame(i);
if ("".equals(aNam e)) aName = attrs.getQName( i);
if(newt && beg)
{
if(aName.equals ("type"))
{
String val=attrs.getVa lue(i);
if(val.equals(" Text")) val="VARCHAR";
if(fff!=0) sql=sql+", ";
sql=sql+eName+" "+val;
fff++;
}
}
else
{
newt=false;
}
}
}
}
rr++;
}

public void endElement(Stri ng namespaceURI,
String sName,
String qName
)
throws SAXException
{
if("".equals(sN ame)) 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.getPrope rty("line.separ ator");
try {
out.write(lineE nd);
} 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
mjahabarsadiq
38 New Member
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
anupkossambe
13 New Member
Quit programming and look for a real job man
Mar 16 '07 #4
r035198x
13,262 MVP
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
14614
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 this way the code will be hidden? How can a prepare it? It is an apache unix server. Thanks in advance
1
3094
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 Transformer convert that via XSLT to valid XSL-FO. So I define a SAXReader which fires the SAX Events for the Java Object. This works fine and the...
3
18181
by: Mike | last post by:
Hi, Does anyone know of reliable programs that convert C# to Java and viceversa? Thanks Mike
2
28812
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 most of the same logic and could I use alot of the same code? I noticed in the conversion that alot of the java code remained the same, so how close...
0
2399
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 ASP.NET/C# ? Please note that I am not asking about the best possible way of doing the same thing with ASP.NET architecture with those "code...
2
6862
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 recommendations from people who have successfully used some of the tools. TIA, Vinny
17
71364
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
13714
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 exactly I should do. Please give me some sample code if you could. Thanks a lot Regards, Gary
6
4174
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 to solve the problem. import java.io.*; import java.io.File; import com.jacob.com.*; import com.jacob.activeX.*; public class...
0
10737
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 inside an image, hide your complete image as text ,search for a particular image inside a directory, minimize the size of the image. However this is not...
0
7618
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7678
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7982
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5222
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3656
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3644
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
944
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.