Re: PreparedStatement across classes

From: Eddie Wilson (eddie_at_wilsonsSpPlAaMce.net)
Date: 11/08/04


Date: Mon, 08 Nov 2004 01:20:44 GMT

That did it. Thanks.

I'll check out the reference.

Thanks again,
Eddie

"Andy Flowers" <notsupplied@nowayhose.com> wrote in message
news:J4vjd.1784$SP6.1654@newsfe3-win.ntli.net...
> stmt1 is redeclared as a local variable in the main(...) routine, hence
the
> static class level variable will remain as null.
>
> Change the statement
> PreparedStatement stmt1 = conn1.prepareStatement("SELECT vndnr from
> amflibl.pomast WHERE ordno = ?");
> to reference the static class level one as per
> stmt1 = conn1.prepareStatement("SELECT vndnr from amflibl.pomast WHERE
> ordno = ?");
>
> You need to look into 'scope' for variables.
> http://java.sun.com/docs/books/tutorial/java/nutsandbolts/scope.html gives
> some hints, as will a google search.
>
> "Eddie Wilson" <eddie@wilsonsSpPlAaMce.net> wrote in message
> news:gCqjd.44558$JS4.38023@twister.nyroc.rr.com...
> >
> > "Lee Fesperman" <firstsql@ix.netcom.com> wrote in message
> > news:418D3E9B.62E9@ix.netcom.com...
> >> Eddie Wilson wrote:
> >> >
> >> > Is it possible to make a PreparedStatement accessible to classes
> >> > outside
> >> > main( )? I am returning the Connection from another class but the
> > statement
> >> > remains null.
> >>
> >> Yes, you can make a PreparedStatement available to classes (?) outside
> > main(). However,
> >> you will need a better description of your situation or show some code
in
> > order to get
> >> help.
> >>
> >> --
> >> Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com)
> >> ==============================================================
> >> * The Ultimate DBMS is here!
> >> * FirstSQL/J Object/Relational DBMS (http://www.firstsql.com)
> >
> > I am sure there is a better way to get this job done but this is how I
am
> > trying to parse an XML into a database using a SAX template.
> > The connection is established and is accessible to all of the classes
but
> > the prepared statement is null outside of main().
> >
> > thanks,
> > Eddie.
> > /**
> >
> > * Create a SAX2 Parser using the Java APIs for XML Processing (JAXP)
> >
> > */
> >
> > import java.io.*;
> >
> > import java.util.*;
> >
> > import java.sql.*;
> >
> > import java.text.*;
> >
> > import java.awt.*;
> >
> > import com.ibm.as400.access.*;
> >
> > //import java.io.IOException;
> >
> > import javax.xml.parsers.FactoryConfigurationError;
> >
> > import javax.xml.parsers.ParserConfigurationException;
> >
> > import javax.xml.parsers.SAXParser;
> >
> > import javax.xml.parsers.SAXParserFactory;
> >
> > import org.xml.sax.*;
> >
> > import org.xml.sax.helpers.DefaultHandler;
> >
> > public class vmiloadSAXParser extends DefaultHandler {
> >
> > private String elmntName = "";
> >
> > private String elmntlName = "";
> >
> > private String elmntSValue = "";
> >
> > private StringBuffer elmntValue = new StringBuffer();
> >
> > private static DBconnection conn1 = null;
> >
> > private static PreparedStatement stmt1 = null;
> >
> > private static PreparedStatement stmt2 = null;
> >
> > private ResultSet rs1 = null;
> >
> > private String invcno = "";
> >
> > private String vndno = "";
> >
> > private String ponum = "";
> >
> > private String invdat = "";
> >
> > private float amttx = 0;
> >
> > private float amtntx = 0;
> >
> > private float amtstx = 0;
> >
> > private float amtfrt = 0;
> >
> > private float amtftx = 0;
> >
> > private float amtmsc = 0;
> >
> > private float amtdsc = 0;
> >
> > private String dscdat = "";
> >
> > private String pstflg = "";
> >
> > public vmiloadSAXParser() {
> >
> > super();
> >
> > }
> >
> > public void characters(char[] ch, int start, int length)
> >
> > throws SAXException {
> >
> > super.characters(ch, start, length);
> >
> > elmntValue.append(ch, start, length);
> >
> > }
> >
> > public void endDocument() throws SAXException {
> >
> > super.endDocument();
> >
> > System.out.println("Parsing Complete.");
> >
> > }
> >
> > public void endElement(String uri, String localName, String qName)
> >
> > throws SAXException {
> >
> > super.endElement(uri, localName, qName);
> >
> > elmntlName = qName;
> >
> > if (elmntName != null) {
> >
> > elmntSValue = elmntValue.toString().trim();
> >
> > System.out.println(elmntName + "=" + elmntSValue);
> >
> > elmntName = "";
> >
> > elmntValue.setLength(0);
> >
> > }
> >
> > System.out.println(":" + elmntlName);
> >
> > if (elmntlName.equals("HEADER")) {
> >
> > try {
> >
> > stmt1.setString(1, ponum);
> >
> > ResultSet rs1 = stmt1.executeQuery();
> >
> > System.out.println(ponum);
> >
> > if (rs1.next())
> >
> > vndno = rs1.getString(1);
> >
> > else
> >
> > vndno = "VNDNO";
> >
> > pstflg = "0";
> >
> > stmt2.setString(1, ponum);
> >
> > stmt2.setString(2, vndno);
> >
> > stmt2.setString(3, invcno);
> >
> > stmt2.setString(4, invdat);
> >
> > stmt2.setFloat(5, amttx);
> >
> > stmt2.setFloat(6, amtntx);
> >
> > stmt2.setFloat(7, amtstx);
> >
> > stmt2.setFloat(8, amtfrt);
> >
> > stmt2.setFloat(9, amtftx);
> >
> > stmt2.setFloat(10, amtmsc);
> >
> > stmt2.setFloat(11, amtdsc);
> >
> > stmt2.setString(12, dscdat);
> >
> > stmt2.setString(13, pstflg);
> >
> > stmt2.executeUpdate();
> >
> > } catch (SQLException e) {
> >
> > e.printStackTrace();
> >
> > }
> >
> > }
> >
> > if (elmntlName.equals("INVNUMBER"))
> >
> > invcno = elmntSValue;
> >
> > if (elmntlName.equals("INVDATE"))
> >
> > invdat = elmntSValue;
> >
> > if (elmntlName.equals("PONUMBER"))
> >
> > ponum = elmntSValue;
> >
> > if (elmntlName.equals("NONTAXABLE"))
> >
> > amtntx = Float.parseFloat(elmntSValue);
> >
> > if (elmntlName.equals("TAXABLE"))
> >
> > amttx = Float.parseFloat(elmntSValue);
> >
> > if (elmntlName.equals("SALESTAX"))
> >
> > amtstx = Float.parseFloat(elmntSValue);
> >
> > if (elmntlName.equals("FREIGHT"))
> >
> > amtfrt = Float.parseFloat(elmntSValue);
> >
> > if (elmntlName.equals("FREIGHTTAX"))
> >
> > amtftx = Float.parseFloat(elmntSValue);
> >
> > if (elmntlName.equals("MISCELLANEOUS"))
> >
> > amtmsc = Float.parseFloat(elmntSValue);
> >
> > if (elmntlName.equals("DISCOUNTAMT"))
> >
> > amtdsc = Float.parseFloat(elmntSValue);
> >
> > if (elmntlName.equals("DISCOUNTDATE"))
> >
> > dscdat = elmntSValue;
> >
> > public void endPrefixMapping(java.lang.String prefix) throws
SAXException
> > {
> >
> > super.endPrefixMapping(prefix);
> >
> > }
> >
> > public void error(SAXParseException e) throws SAXException {
> >
> > super.error(e);
> >
> > }
> >
> > public void fatalError(SAXParseException e) throws SAXException {
> >
> > super.fatalError(e);
> >
> > }
> >
> > public void ignorableWhitespace(char[] ch, int start, int length)
> >
> > throws SAXException {
> >
> > super.ignorableWhitespace(ch, start, length);
> >
> > }
> >
> > public void notationDecl(String name, String publicId, String systemId)
> >
> > throws SAXException {
> >
> > super.notationDecl(name, publicId, systemId);
> >
> > }
> >
> > public void processingInstruction(String target, String data)
> >
> > throws SAXException {
> >
> > super.processingInstruction(target, data);
> >
> > }
> >
> > public InputSource resolveEntity(String publicId, String systemId)
> >
> > throws SAXException {
> >
> > return super.resolveEntity(publicId, systemId);
> >
> > }
> >
> > public void setDocumentLocator(Locator locator) {
> >
> > super.setDocumentLocator(locator);
> >
> > }
> >
> > public void skippedEntity(String name) throws SAXException {
> >
> > super.skippedEntity(name);
> >
> > System.out.println("Skipped " + name);
> >
> > }
> >
> > public void startDocument() throws SAXException {
> >
> > super.startDocument();
> >
> > System.out.println("Parsing XML Document");
> >
> > }
> >
> > public void startElement(
> >
> > String uri,
> >
> > String localName,
> >
> > String qName,
> >
> > Attributes attributes)
> >
> > throws SAXException {
> >
> > super.startElement(uri, localName, qName, attributes);
> >
> > elmntName = qName;
> >
> > if (uri != null) {
> >
> > elmntlName = qName;
> >
> > System.out.println(elmntlName + ":");
> >
> > }
> >
> > }
> >
> > public void startPrefixMapping(String prefix, String uri)
> >
> > throws SAXException {
> >
> > super.startPrefixMapping(prefix, uri);
> >
> > }
> >
> > public void unparsedEntityDecl(
> >
> > String name,
> >
> > String publicId,
> >
> > String systemId,
> >
> > String notationName)
> >
> > throws SAXException {
> >
> > super.unparsedEntityDecl(name, publicId, systemId, notationName);
> >
> > }
> >
> > public void warning(SAXParseException e) throws SAXException {
> >
> > super.warning(e);
> >
> > }
> >
> > /**
> >
> > * Main program *
> >
> > */
> >
> > public static void main(String[] argv) {
> >
> > String xmlFile = "/transfer/test/vmientry/data/sample9189.xml";
> >
> > try {
> >
> > SAXParserFactory factory = SAXParserFactory.newInstance();
> >
> > SAXParser parser = factory.newSAXParser();
> >
> > DefaultHandler handler = new vmiloadSAXParser();
> >
> > /* custom handler class */;
> >
> > System.out.println("Setting JDBC Properties");
> >
> > conn1 = new DBconnection();
> >
> > conn1.createConnection();
> >
> > System.out.println("Getting Connection");
> >
> > Connection conn1 = DBconnection.getConnection();
> >
> > System.out.println("Preparing SQL Statements");
> >
> > PreparedStatement stmt1 =
> >
> > conn1.prepareStatement(
> >
> > "SELECT vndnr from amflibl.pomast WHERE ordno = ?");
> >
> > parser.parse(xmlFile, handler);
> >
> > } catch (FactoryConfigurationError e) {
> >
> > // Unable to get a document builder factory
> >
> > e.printStackTrace();
> >
> > } catch (ParserConfigurationException e) {
> >
> > // Parser was unable to be configured
> >
> > e.printStackTrace();
> >
> > } catch (SAXException e) {
> >
> > // Parsing error
> >
> > e.printStackTrace();
> >
> > } catch (IOException e) {
> >
> > // I/O error
> >
> > e.printStackTrace();
> >
> > } catch (SQLException e) {
> >
> > e.printStackTrace();
> >
> > }
> >
> > }
> >
> > }
> >
> > /**
> >
> > * @author ewilson
> >
> > *
> >
> > * To change this generated comment edit the template variable
> > "typecomment":
> >
> > * Window>Preferences>Java>Templates.
> >
> > * To enable and disable the creation of type comments go to
> >
> > * Window>Preferences>Java>Code Generation.
> >
> > */
> >
> > import java.sql.*;
> >
> > import com.ibm.as400.access.*;
> >
> > import java.util.*;
> >
> > public class DBconnection {
> >
> > private static Connection conn = null;
> >
> > private static PreparedStatement stmt1 = null;
> >
> > public DBconnection() {
> >
> > createConnection();
> >
> > }
> >
> > public void createConnection() {
> >
> > System.out.println("Creating Connection conn");
> >
> > Properties jdbcProperties = new Properties();
> >
> > jdbcProperties.put("user", "USER");
> >
> > jdbcProperties.put("password", "");
> >
> > jdbcProperties.put("libraries", "*LIBL");
> >
> > jdbcProperties.put("translate binary", "2");
> >
> > try {
> >
> > Class.forName("com.ibm.db2.jdbc.app.DB2Driver").newInstance();
> >
> > } catch (Exception e) {
> >
> > e.printStackTrace();
> >
> > }
> >
> > try {
> >
> > conn =
> >
> > DriverManager.getConnection(
> >
> > "jdbc:db2://S1234567",
> >
> > jdbcProperties);
> >
> > System.out.println("conn created");
> >
> > } catch (SQLException e) {
> >
> > e.printStackTrace();
> >
> > }
> >
> >
> >
> > }
> >
> >
> >
> > public static Connection getConnection() {
> >
> > System.out.println("Returning Connection conn");
> >
> > return conn;
> >
> > }
> >
> > }
> >
> >
>
>



Relevant Pages

  • Re: passing value from jcombobox to other classes?
    ... public void setX(String x) { ... // the reference to an object of A ... Run it by "java C" and see what happens. ...
    (comp.lang.java.programmer)
  • Re: Is there a limit to the size or complexity of JPanels paintComponent method?
    ... double actualnorthextent, actualsouthextent, actualwestextent, ... public void movemapdown() ... public double convertstringcoordinatetodouble(String coord) ... return screencoords; ...
    (comp.lang.java.gui)
  • Re: Is there a limit to the size or complexity of JPanels paintComponent method?
    ... double actualnorthextent, actualsouthextent, actualwestextent, ... public void movemapdown() ... public double convertstringcoordinatetodouble(String coord) ... return screencoords; ...
    (comp.lang.java.gui)
  • Re: Problem with FTP
    ... > private static Encoding ASCII = Encoding.ASCII; ... > private string message = null; ... > private int port = 21; ... > public void Login() ...
    (microsoft.public.pocketpc.developer)
  • Re: All Menu Navigation
    ... i based the implementation on sample code from http://www.asp.net/CSSAdapters/Menu.aspx, which shows how to create decent HTML for a Menu control bound to a SiteMapDataSource. ... public void RenderBeginTag(HtmlTextWriter writer, string cssClass) ... static public void RemoveProblemTypes ...
    (microsoft.public.dotnet.framework.aspnet)