xml-general mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Payal Dutt <payal_dutt2...@yahoo.com>
Subject Error in a simple JDBC program
Date Thu, 24 Jan 2002 02:15:50 GMT
Hi,
    Am working on a XML API project and have a program
which has simple SQL statements(JDBC).Am having a MS
Access database.I have a simple insert statement into
a table. Problem is it works sometimes and does not
work for certain times.(THERE IS NOTHING WRONG WITH
THE DATA CONSISTENCY OR SOMETHING).For the same data
it inserts sometimes and doesn't for certain other
times(AND DOES NOT THROW AN ERROR EITHER).PLease help
me out.the program is given below:
Basically there are certain SAX parser functions that
can be ignored as i have hardcoded the insert
statement in the function "insProc()".
Any help is kindly appreciated.
Payal
******************************************************
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.Locator;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.XMLReader;
import org.xml.sax.InputSource;
import java.sql.*;
import java.net.*;

public class insProg extends DefaultHandler
{
	private int count = 0;
	String tempElement = "";
	String insStmnt = "";
	String delStmnt = "";
	String updStmnt = "";
	Connection conn;
	boolean multiVal = false;

	public void insProg()
	{
	}

	public static void main(String args[]) throws
Exception
	{
		String xml_file = args[0];
		insProg c1 = new insProg();
		c1.insProc(xml_file);
	}
	public void insProc(String xml_file) throws Exception
	{
//*******************************************************************************************************
				XMLReader xmlreader = null;
				SAXParserFactory spFactory =
SAXParserFactory.newInstance();
				spFactory.setValidating(false);
				SAXParser spParser = spFactory.newSAXParser();
				xmlreader = spParser.getXMLReader();
				xmlreader.setContentHandler(this);
//				InputSource source = new
InputSource("C:/Thesis/csesam/csesam_ins.xml");
				InputSource source = new InputSource(xml_file);
		    	xmlreader.parse(source);
//************************************************************************************************
				String url = "jdbc:odbc:csesam";
				try
				{
					Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
					conn = DriverManager.getConnection(url);
				}
				catch(ClassNotFoundException e)
				{
					System.out.println("Class Exception "+e);
				}
				catch(SQLException e)
				{
					System.out.println("Class Exception "+e);
				}
				Statement stmt = conn.createStatement();
				System.out.println("Int the insProc Insert
statement :" + insStmnt);
				//int insCnt = stmt.executeUpdate(insStmnt);
				//int insCnt = stmt.executeUpdate("insert into
task_tbl_test values
(5555,'1/1/02','1/1/02','1/1/02',2,'ssreeniv','sam','Prob4',7)");
				int insCnt = stmt.executeUpdate("insert into
sample values (1234,'JLT')");
				System.out.println("The number of rows inserted "
+ insCnt);
/*				ResultSet rs = stmt.executeQuery("SELECT * FROM
sample");
				System.out.println("Found row:");
				while (rs.next())
				{
					String Surname = rs.getString(1);
					System.out.print (" Surname=" + Surname);
				}*/

//*******************************************************************************************************
	}

	public void startDocument() throws SAXException
	{
		System.out.println("In the startDocument fn");
	}
	public void startElement(String namespURI , String
name , String qname , Attributes atts) throws
SAXException
	{
		System.out.println("In the startElement fn");
		System.out.print("Start Element: ");
		System.out.println(name);
		tempElement = name;
		if(tempElement == "INSERT")
			insStmnt = "insert into ";
	}
	public void endElement(String namespURI , String name
, String qname)
	{
		System.out.println("In the End Element");
		System.out.print("End Element: ");
		System.out.println(name);
	}
	public void characters(char[] ch,int start,int
length) throws SAXException
	{
		System.out.print(tempElement+ " : ");
		System.out.println(new String(ch,start,length));
		System.out.println("Start "+start+" , "+"Length
"+length);
		if(tempElement == "TABLENAME")
			insStmnt = insStmnt + new String(ch,start,length) +
" values (";
		else if(tempElement == "VAL")
		{
			if (!multiVal)
			{
				insStmnt = insStmnt + new String(ch,start,length);
				multiVal = true;
			}
			else
				insStmnt = insStmnt + "," + new
String(ch,start,length);

		}
	}

	public void endDocument() throws SAXException
	{
		insStmnt = insStmnt + ")";
		System.out.println("The insert statement is as
follows: "+insStmnt);
	}
}
********************************************************


__________________________________________________
Do You Yahoo!?
Great stuff seeing new owners in Yahoo! Auctions! 
http://auctions.yahoo.com

---------------------------------------------------------------------
In case of troubles, e-mail:     webmaster@xml.apache.org
To unsubscribe, e-mail:          general-unsubscribe@xml.apache.org
For additional commands, e-mail: general-help@xml.apache.org


Mime
View raw message