i m unable to solve this problem in netbeans:

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bansari
    New Member
    • Jul 2010
    • 20

    i m unable to solve this problem in netbeans:

    Get the operation to be done from the command line in the following format:
    D for Delete
    U for update
    I for Insert
    For delete operations get the userid from the command line and delete the record.
    For the update and insert operations get the following information from the command line and insert the record.
    Name:
    Birth date:
    SSC Percentage:
    Fees:
    Last accessed time:
    Decide on the format best suited for input.




    I m new to java and also to netbeans.i know ow to connect but i dnt know how to get to many command line arguments..We can give it in project property..but how can i compare that value of command line ::



    i m getting no error but not output.

    Code:
    package assign22;
    
    import java.sql.*;
    
    
    public class DataOperation {
    
        public static Connection connection = null;
        public static Statement stmt = null;
        public static ResultSet rs = null;
    
        public static void main(String[] argv) throws Exception {
            String inputCh = argv[0];
            String id = argv[1];
             String url="jdbc:postgresql:postgres";
            
            String driver = "org.postgresql.Driver";
            String user = "postgres";
            String pass = "password";
            String query = "select * from tbluser";
    
            try {
              
                Class.forName("org.postgresql.Driver");
                System.out.println("hi1");
                connection = DriverManager.getConnection("jdbc:postgresql:postgres", "postgres", "password");
                System.out.println("Connection Successful");
                stmt = connection.createStatement();
              
                String del = "delete * from tbluser where userid=" + id;
                
    
                if (inputCh.equalsIgnoreCase("Delete"))  //i also tried to use equals,startsWith methods but failed
                    //String del="delete * from tbluser where userid=" + id;
                   
                    stmt.executeUpdate(del);
    
                    System.out.println("Deleted one Row Successfully");
    
                }
                else
                {
                    System.out.println("There is some mistake");
                }
            } 
            
            }//end of try */ catch (Exception e) {
    
                e.getMessage();
            }//end of catch
    
    
        }//end of main
    }//end of class

    i m giving commandline arguments: Delete 1 //as userid

    please help me.
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    i m getting no error but not output.
    That's impossible. At least you should get the output "Hil" (whatever that means that you print out here). If not, then you should get an error.
    That means either it passes line 25 or it crashes before.
    And it's not the try-catch-statement where it can crash, it also can crash before: at line 14, where you access the 2nd array element in an act of suicide (that means without checking first, if the element really exists - maybe you didn't pass an argument ). But even then you should get "array index out of bounds" message right away at the command line!

    Are you sure you are really executing the current program? Maybe you forgot to compile and it's executing a previous version, or you are in the wrong directory, or whatever...

    Comment

    • bansari
      New Member
      • Jul 2010
      • 20

      #3
      Originally posted by chaarmann
      That's impossible. At least you should get the output "Hil" (whatever that means that you print out here). If not, then you should get an error.
      That means either it passes line 25 or it crashes before.
      And it's not the try-catch-statement where it can crash, it also can crash before: at line 14, where you access the 2nd array element in an act of suicide (that means without checking first, if the element really exists - maybe you didn't pass an argument ). But even then you should get "array index out of bounds" message right away at the command line!

      Are you sure you are really executing the current program? Maybe you forgot to compile and it's executing a previous version, or you are in the wrong directory, or whatever...
      hi...i was doing small mistake that was ...i didn't copied the jar file of postgreSQL to this project's library..it was not getting driver which is in line 24.

      Thank u.

      Comment

      • chaarmann
        Recognized Expert Contributor
        • Nov 2007
        • 785

        #4
        Originally posted by bansari
        hi...i was doing small mistake that was ...i didn't copied the jar file of postgreSQL to this project's library..it was not getting driver which is in line 24.

        Thank u.
        But classs.forName throws a ClassNotFoundEx ception if the class will not be found (because you forgot to copy to project library):
        This exception then would be caught in your catch(Exception e) statement. There you only call "e.getMessage() ". That means, you got an error, but you didn't print out anything in this case.
        (I would have printed a meaningful message instead, along with the exception, like System.out.prin tln("deletion threw exception" + e).
        So that was confusing. But maybe you meant "no error was printed" instead of "I got no error".

        So my advice for the future:
        Don't make an empty catch()-block or do some invisible logic there.
        Always print out the exception in a catch()-block or rethrow it.

        That means, if you only fixed the ClassNotFoundEx ception, your code still has a bug. That the exception happened, this is not the bug and can happen anytime again (maybe someone else deletes it from the project library by accident and then searches like hell why it doesn't work anymore!) The problem is how your code deals with errors. Your code should be stable and not error-prone. Only if you fixed the catch()-block, you really fixed the problem. An if you are very nice to your fellow programmers which use or alter your code in future, you should print out a meaningful message, like "Please add the class ... to the project library". You already had big problems finding the error cause in your own code, so how big problems would someone have that has not written the code and must parse through thousands of unknown lines without a hint?

        Comment

        • bansari
          New Member
          • Jul 2010
          • 20

          #5
          Thank you for suggesting me right path.It will be beneficial for me.It is good that expert person helps to novice too.

          Comment

          Working...