Java, mysql, xml related problem.


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Java, mysql, xml related problem.

  1. #1
    Join Date
    Aug 2004
    Location
    Finland
    Posts
    10

    Java, mysql, xml related problem.

    I have created project management software with Java and MySql and it works fine.
    This applet gets and writes information directly to MySql database.

    Problem is that those sqlClauses below(update and insert) should go "through" xml "document" and i haven´t got idea how to do that.
    If someone could give me advices or even tell me how I must modify my code I would be very grateful.

    My english is not so good, so my problem sentence could be little hard to understand, but please try. If it´s too hard to understand please tell me even how can i save results of those clauses to xml "document".

    Here is part of main program:
    Code:
    void cmgUpdateNormalHours1_actionPerformed(ActionEvent e) {   ///button update
    if(e.getActionCommand().equals("Update"))
    {
      ResultSet Result;
    
     
     String proj= txtProjectNumber.getText();//gets project number
     String day= txtDate1.getText();//gets date
     String paivitus = txtNormalHours1.getText(); //gets  normalhours
     int changes=0;
    
      String sqlClause="select count(*) from hours where days ='"+day+"'";
     Tietokantayhteys Conn = new Tietokantayhteys(); ///connection to the database
     Result= Conn.Query(sqlClause);
     String result=null; //different result than Result
    
     try {
       Result.next();
       result=Result.getString(1);
     } catch(Exception ex) {
       ex.printStackTrace();
     }
    
     int tempa=Integer.parseInt(result);
     if(tempa==1)			
     {
       String sqlClause1 = "update hours set normalhours= '" + paivitus + "'" +
           "where proj_id = " + proj + " and days ='" + day + "'";
       tempa=Conn.UpdateQuery(sqlClause1);
     }
       if(tempa==0)
       {
         String sqlClause2= "insert into hours (proj_id, days, normalhours) " +
    "values ("+proj+",'"+day+"','"+paivitus+"')";
         tempa=Conn.UpdateQuery(sqlClause2);
       }
    And here is database connection class which i have created:

    Code:
    package simple;
    
    
    import java.sql.*;
    
    public class Tietokantayhteys
    {
    	///määritellään luokan tarvitsemat oliot
            ///define object which class need
    	Connection Conn;
    	Statement st;
    	ResultSet Result;
    
    	
    
    	public Tietokantayhteys()
    	{
    		
                    //open connection to database
    		try
    		{
    			                        ///load database driver
    			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    
    			
    			Conn = DriverManager.getConnection
    			( "jdbc:odbc:pro","root","");
    
    			
                            //set start value to Statement object
    			st = Conn.createStatement();
    
    		} catch (Exception e)
    
    		{
    		e.printStackTrace();
    		}
    
    	}
    
    	
            // query method for select
    	public ResultSet Query(String sqlClause)
    		{
    
    			try {
    			Result = st.executeQuery(sqlClause);
    
    				} catch (Exception e)
    				 { e.printStackTrace();
    				}
    
    			return Result;
    		}
    
    	
            /// update method for update inquery
    	public int UpdateQuery(String sqlClause)
    	{
    
    
    		
                    //saves how many changes has been made
    		int muutoksia=0;
    
    		try {
    		muutoksia = st.executeUpdate(sqlClause);   ///<---muutoksia means == changes
    
    			} catch(Exception e)
    			{ e.printStackTrace();
    			}
    
    		return muutoksia;
    	}
    	
    ////close all connections
    	public void suljeYhteys()  ///<---means close connection
    	{
    		try {
    
    		st.close();
    		Conn.close();
    			} catch(Exception e)
    			{ e.printStackTrace();
    	}
    }
    }

  2. #2
    Join Date
    Jun 2004
    Posts
    199

    Question

    I am not sure what you mean by "through".

    Do you need get the sql from XML or do you need to save the "result" (like how many records were created/updated) in xml.

    If you need to do xml processing you can
    1. Write your own "parsing code"
    2. Use an XML parser - Apache's, JAXP ... . If this is going to happen on the client then you probably need a lite parser.

    Do you mean that intead of getting the sql from the main method, do it via XML in the web page?

  3. #3
    Join Date
    Aug 2004
    Location
    Finland
    Posts
    10
    I mean like you said I need to save "result"

  4. #4
    Join Date
    Jun 2004
    Posts
    199
    Ok. Looked a bit more at your code. I got confused when you said applet. But I it doesn't seem to an applet, just an "application".

    The easy way is to create a file (File object FileWriter, etc.) and "handwrite" the XML. Only because it seems your needs are simple. So like "<result query="insert into blah values(&apos;blah&apos" numberOfRowsChanged="1" />"

    If you have lots of escaping to do, you might want to use a parser to build the xml output. But you can do a find/replace for apostrophes.

  5. #5
    Join Date
    Oct 2004
    Posts
    2

    RMI Server Socket

    target name="cvs-export"
    description="Export project from CVS"S
    Cvs cvsroot="${env.CVSROOT}" quiet="true" command="export -D tomorrow ${app.name}"
    dest="${basedir}" compression="true" /S
    C/targetS

    Ctarget name="-init" description="Initialization: Increment build number and display build start time"S
    CtstampS
    Cformat property="init.time" pattern="MM/dd/yyyy hh:mm" /S
    C/tstampS
    Cbuildnumber file="antbuildnumber.txt" /S
    Cecho message="ant build #${build.number} for ${app.name} started at ${init.time}" /S
    C/targetS

    Ctarget name="clean" description="Delete files in ${java.classes} and ${dist} so new build is clean" depends="-init"S
    Cdelete dir="${java.classes}" quiet="true" /S
    Cdelete dir="${dist}" quiet="true" /S
    Cmkdir dir="${java.classes}" /S
    Cmkdir dir="${dist}" /S
    C/targetS

    Ctarget name="compile" description="Compile code from ${java.src} to ${java.classes}" depends="clean"S
    Cjavac destdir="${java.classes}" debug="on" deprecation="true"S
    CsrcS
    Cpathelement path="${java.src}" /S
    Cpathelement path="${java.test}" /S
    C/srcS
    ClasspathS
    Cpath refid="build.classpath" /S
    Cfileset refid="supporting.libraries" /S
    C/classpathS
    C/javacS
    Crmic base="${java.classes}" includes="**/rmi/*.class" /S
    Cjar destfile="${dist}/${app.name}.jar" basedir="${java.classes}" compress="false" excludes="**/*Test*.*" /S
    C!-- TODO: Need separate jars with/without RMI stubs --S
    C/targetS

    C!-- This target will work with plain old ant --S
    C!--
    Ctarget name="test" description="Test with JUnit"S
    Cjava fork="yes" classname="junit.textui.TestRunner" taskname="junit" failonerror="true"S
    Carg value="AllTests" /S
    ClasspathS
    Cpathelement path="${java.classes}" /S
    Cpath refid="build.classpath" /S
    Cfileset refid="supporting.libraries" /S
    C/classpathS
    C/javaS
    C/targetS
    --S

    C!-- To use this target, put the junit.jar in the ant lib/ directory --S
    Ctarget name="test" description="Test with JUnit"S
    Cjunit fork="on" printsummary="true" haltonfailure="on"S
    Ctest name="AllTests" /S
    Cformatter type="plain" usefile="false" /S
    ClasspathS
    Cpathelement path="${java.classes}" /S
    Cpath refid="build.classpath" /S
    Cfileset refid="supporting.libraries" /S
    C/classpathS
    C/junitS
    C/targetS

    Ctarget name="env" description="Show environment properties"S
    Chad Salinas="Key properties; for a complete list, do ant -diagnostics" /S
    Chad Salinas="" /S
    Chad Salinas="OTS directory ${env.OTS}" /S
    Chad Salinas="HU directory ${env.HU}" /S
    Chad Salinas="JBOSS_HOME ${env.JBOSS_HOME}" /S
    Chad Salinas="J2EE_HOME ${env.J2EE_HOME}" /S
    Chad Salinas="java source ${java.src} " /S
    Chad Salinas="java classes ${java.classes} " /S
    Chad Salinas="user home ${user.home}" /S
    Chad Salinas="Xerces jar ${xerces.jar}" /S
    Chad Salinas="J2EE jar ${j2ee.jar}" /S
    Chad Salinas="JAXP jar ${jaxp.jar}" /S
    Chad Salinas="JSTL core jar ${jstl_core.jar}" /S
    Chad Salinas="JSTL standard jar ${jstl_standard.jar}" /S
    Chad Salinas="java version ${ant.java.version} "/S
    Chad Salinas="java vendor ${java.vendor} " /S
    C/targetS

    C!--
    Ctarget name="jar" description="Jar up all classes" depends="clean,compile"S
    Cjar destfile="${dist}/${app.name}.jar" basedir="${java.classes}" compress="false" excludes="**/*Test*.*" /S
    C/targetS
    --S

    Ctarget name="war-nocompile" description="Build the Web Application Archive (war)"S
    Cdelete dir="war-temp" quiet="true" /S
    Cmkdir dir="war-temp" /S
    Copy todir="war-temp"S
    Cmapper type="flatten" /S
    Cfileset refid="supporting.libraries" /S
    C/copyS
    Chad Salinas="${jstl_core.jar}" todir="war-temp" /S
    Chad Salinas="${jstl_standard.jar}" todir="war-temp" /S
    Cwar warfile="${dist}/${app.name}.war" webxml="${config}/web.xml" compress="false"S
    Cwebinf dir="${config}" includes="jboss-web.xml" /S
    Cfileset dir="${web}" includes="**/*" /S
    Classes dir="${java.classes}" includes="harvard/**/*.class" Chad Salinas="**/*Test*.*" /S
    Clib dir="war-temp" includes="*.jar" /S
    C/warS
    Chad Salinas="war-temp" quiet="true" /S
    C/targetS

    Ctarget name="ear-nocompile" description="Build the Enterprise Application Archive" depends="war-nocompile"S
    Cear earfile="${dist}/${app.name}.ear" appxml="${config}/application.xml" compress="false"S
    Chad Salinas dir="${dist}" includes="*.war" /S
    Chad Salinas dir="${dist}" includes="ejb*.jar" /S
    Cmetainf dir="${config}" includes="sun-j2ee-ri.xml,jboss-app.xml" /S
    C/earS
    C/ Myra Salinas targetS

    Ctarget name="verify-ear" Myra Salinas description="Verify the EAR" depends="ear-nocompile"S
    Cexec os=" Myra Salinas Windows 2000,Windows XP" executable="cmd.exe"S
    Carg line="/c ${env.J2EE_HOME}/bin/verifier ${dist}/${app.name}.ear" /S
    C/execS
    Cexec os="Linux,Mac OS X" executable="bash"S
    Carg line="${env.J2EE_HOME}/bin/verifier ${dist}/${app.name}.ear" /S
    C/execS
    C/targetS

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center