Java advance 3

profilewilden
javasolution.zip

JavaSolution/Functionality.docx

Discussion Board 3

William Denison

22 April 2014

INSERTION

Here when we run the application, we first get a window asking for CustomerNumber, CustomerName, ProductName and Quantity. Once we enter this and press Insert Button, the Data is inserted into the table as shown below.

Snap 1 is also attached separately.

RETRIEVAL

After that what we do is to see whether data is retrieved from the database or not.

So we give the CustomerNumber and select GetProductName button.

This does 2 things:

1) It shows the name of the customer in the customername textbox

2) It shows the product name and quantity in JOptionPane.

One of the main environment issues about doing this codes. Was switching from MySQL to Net Beans writing different codes.

I didn’t use the customer names or CTU online usage or even identified. However I used another similar code that wrote just playing with MySQL. What I made was similar to the exercise which shows you the connection from MYSQL to Netbeans.

I also showed by snapshots of what I did. And I even sent over the codes I worte through the extraction from netbeans and My SQL.

William Denison

SNAP1.png

snap2.png

JavaSolution/MainClass.zip

MainClass/build/classes/.netbeans_automatic_build

MainClass/build/classes/mainclass/MainClass$1.class

package mainclass;
synchronized class MainClass$1 {
}

MainClass/build/classes/mainclass/MainClass$Handler.class

package mainclass;
synchronized class MainClass$Handler implements java.awt.event.ActionListener {
    private void MainClass$Handler();
    public void actionPerformed(java.awt.event.ActionEvent);
}

MainClass/build/classes/mainclass/MainClass.class

package mainclass;
public synchronized class MainClass {
    static javax.swing.JTextField CustomerName;
    static javax.swing.JTextField ProductName;
    static javax.swing.JTextField ProductQuantity;
    static javax.swing.JTextField CustomerNumber;
    static javax.swing.JButton Insert;
    static javax.swing.JButton GetProductName;
    void MainClass();
    public static void main(String[]);
}

MainClass/build.xml

Builds, tests, and runs the project MainClass.

MainClass/manifest.mf

Manifest-Version: 1.0 X-COMMENT: Main-Class will be added automatically by build

MainClass/nbproject/build-impl.xml

Must set src.dir Must set test.src.dir Must set build.dir Must set dist.dir Must set build.classes.dir Must set dist.javadoc.dir Must set build.test.classes.dir Must set build.test.results.dir Must set build.classes.excludes Must set dist.jar Must set javac.includes Must select some files in the IDE or set javac.includes To run this application from the command line without Ant, try: java -cp "${run.classpath.with.dist.jar}" ${main.class} To run this application from the command line without Ant, try: java -jar "${dist.jar.resolved}" To run this application from the command line without Ant, try: java -jar "${dist.jar.resolved}" Must select one file in the IDE or set run.class Must select one file in the IDE or set run.class Must select one file in the IDE or set debug.class Must select one file in the IDE or set debug.class Must set fix.includes Must select some files in the IDE or set javac.includes Some tests failed; see details above. Must select some files in the IDE or set test.includes Some tests failed; see details above. Must select one file in the IDE or set test.class Must select one file in the IDE or set applet.url Must select one file in the IDE or set applet.url

MainClass/nbproject/genfiles.properties

build.xml.data.CRC32=114f05a9 build.xml.script.CRC32=b077f051 [email protected] # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. nbproject/build-impl.xml.data.CRC32=114f05a9 nbproject/build-impl.xml.script.CRC32=e6d67699 nbproject/[email protected]

MainClass/nbproject/private/private.properties

compile.on.save=true user.properties.file=C:\\Users\\Dell\\.netbeans\\6.9\\build.properties

MainClass/nbproject/private/private.xml

MainClass/nbproject/project.properties

annotation.processing.enabled=true annotation.processing.enabled.in.editor=false annotation.processing.processor.options= annotation.processing.processors.list= annotation.processing.run.all.processors=true annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output build.classes.dir=${build.dir}/classes build.classes.excludes=**/*.java,**/*.form # This directory is removed when the project is cleaned: build.dir=build build.generated.dir=${build.dir}/generated build.generated.sources.dir=${build.dir}/generated-sources # Only compile against the classpath explicitly listed here: build.sysclasspath=ignore build.test.classes.dir=${build.dir}/test/classes build.test.results.dir=${build.dir}/test/results # Uncomment to specify the preferred debugger connection transport: #debug.transport=dt_socket debug.classpath=\ ${run.classpath} debug.test.classpath=\ ${run.test.classpath} # This directory is removed when the project is cleaned: dist.dir=dist dist.jar=${dist.dir}/MainClass.jar dist.javadoc.dir=${dist.dir}/javadoc excludes= includes=** jar.compress=false javac.classpath=\ ${libs.MySQLDriver.classpath} # Space-separated list of extra javac options javac.compilerargs= javac.deprecation=false javac.processorpath=\ ${javac.classpath} javac.source=1.5 javac.target=1.5 javac.test.classpath=\ ${javac.classpath}:\ ${build.classes.dir}:\ ${libs.junit.classpath}:\ ${libs.junit_4.classpath} javac.test.processorpath=\ ${javac.test.classpath} javadoc.additionalparam= javadoc.author=false javadoc.encoding=${source.encoding} javadoc.noindex=false javadoc.nonavbar=false javadoc.notree=false javadoc.private=false javadoc.splitindex=true javadoc.use=true javadoc.version=false javadoc.windowtitle= main.class=mainclass.MainClass manifest.file=manifest.mf meta.inf.dir=${src.dir}/META-INF platform.active=default_platform run.classpath=\ ${javac.classpath}:\ ${build.classes.dir} # Space-separated list of JVM arguments used when running the project # (you may also define separate properties like run-sys-prop.name=value instead of -Dname=value # or test-sys-prop.name=value to set system properties for unit tests): run.jvmargs= run.test.classpath=\ ${javac.test.classpath}:\ ${build.test.classes.dir} source.encoding=UTF-8 src.dir=src test.src.dir=test

MainClass/nbproject/project.xml

org.netbeans.modules.java.j2seproject MainClass

MainClass/src/mainclass/MainClass.java

MainClass/src/mainclass/MainClass.java


package  mainclass ;

import  java . awt . event . ActionEvent ;
import  java . awt . event . ActionListener ;
import  javax . swing . * ;
import  java . awt . * ;
import  java . sql . * ;
public   class   MainClass {
     static   JTextField   CustomerName , ProductName , ProductQuantity , CustomerNumber ;
     static   JButton   Insert , GetProductName ;
   

     private   static   class   Handler   implements   ActionListener   {
        @ Override
         public   void  actionPerformed ( ActionEvent  e )   {
             if ( e . getActionCommand () == "Insert" )
             {
                 try {
                     Class . forName ( "sun.jdbc.odbc.JdbcOdbcDriver" );
                   
                     int   Customernumber = Integer . parseInt ( CustomerNumber . getText ());
                     String   Customername   =   CustomerName . getText ();
                     String   Product   =   ProductName . getText ();
                     int   Productquantity = Integer . parseInt ( ProductQuantity . getText ());


                     Connection  con  =   DriverManager . getConnection ( "jdbc:mysql://localhost:3306/STORE" ,   "root" ,   "aakash" );
                     PreparedStatement  statement =  con . prepareStatement ( "INSERT INTO PRODUCT VALUES(?,?,?)" );
                    statement . setInt ( 1 ,   Customernumber );
                    statement . setString ( 2 , Product );
                    statement . setInt ( 3 , Productquantity );
                    statement . executeUpdate ();

                    statement =  con . prepareStatement ( "INSERT INTO CUSTOMER VALUES(?,?)" );
                    statement . setInt ( 1 ,   Customernumber );
                    statement . setString ( 2 , Customername );
                     statement . executeUpdate ();

                 }
                 catch   ( Exception  ex )   {
                         ex . printStackTrace ();
                 }
             }
             if ( e . getActionCommand () == "GetProductName" )
             {
                 try {
                     Class . forName ( "sun.jdbc.odbc.JdbcOdbcDriver" );
                     Connection  con  =   DriverManager . getConnection ( "jdbc:mysql://localhost:3306/STORE" ,   "root" ,   "aakash" );
                     Statement  statement =  con . createStatement ();
                     int   Customernumber   =   Integer . parseInt ( CustomerNumber . getText ());

                     String  quer = "SELECT CUSTOMERNAME FROM CUSTOMER WHERE CUSTOMERNUMBER='" + Customernumber + "'" ;
                     ResultSet  resultSet  =  statement . executeQuery ( quer );
                     if ( resultSet . next ())
                     {
                           String  custname = resultSet . getString ( 1 );
                           CustomerName . setText ( custname );
                     }

                     String  query  =   "SELECT PRODUCTNAME,QUANTITY FROM PRODUCT WHERE CUSTOMERNUMBER='" + Customernumber + "'" ;
                     System . out . println ( query );
                    resultSet  =  statement . executeQuery ( query );
                     if ( resultSet . next ())
                     {
                           String   Productname = resultSet . getString ( 1 );
                           int   Quantity = Integer . parseInt ( resultSet . getString ( 2 ));
                           JOptionPane . showMessageDialog ( null , "Name of Product=" + Productname + "Quantity=" + Quantity , "Success" , JOptionPane . PLAIN_MESSAGE );
                     }
                 else
                 {
                     JOptionPane . showMessageDialog ( null , "Unsuccessful!" , "Error" , 1 );
                 }
              } catch   ( Exception  ex )   {
                         ex . printStackTrace ();
                 }
             }
         }
     }
     MainClass (){
         JFrame  main  =   new   JFrame ( "MENU FOR STORE" );
        main . setBounds ( 350 , 150 , 500 , 500 );
        main . setVisible ( true );
        main . setDefaultCloseOperation ( JFrame . EXIT_ON_CLOSE );
         CustomerNumber = new   JTextField ( 10 );
         CustomerName   =   new   JTextField ( 10 );
         ProductName   =   new   JTextField ( 10 );
         ProductQuantity = new   JTextField ( 10 );
        main . setLayout ( new   GridLayout ( 0 , 1 ));
         JPanel  pane  =   new   JPanel ();
        main . add ( pane );
        pane . add ( new   JLabel ( "Customer Number: " ));
        pane . add ( CustomerNumber );
        pane . add ( new   JLabel ( "Customer Name: " ));
        pane . add ( CustomerName );
        pane . add ( new   JLabel ( "Product Name: " ));
        pane . add ( ProductName );
        pane . add ( new   JLabel ( "Product Quantity: " ));
        pane . add ( ProductQuantity );
         Insert   =   new   JButton ( "Insert" );
        pane . add ( Insert );
         Insert . addActionListener ( new   Handler ());
         GetProductName =   new   JButton ( "GetProductName" );
        pane . add ( GetProductName );
         GetProductName . addActionListener ( new   Handler ());


     }
     public   static   void  main ( String  args []){
         new   MainClass ();

     }

}

JavaSolution/SNAP1.png

JavaSolution/snap2.png