SQL & Database: Selecting & Deleting a record

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 08butoryr
    New Member
    • Sep 2007
    • 16

    SQL & Database: Selecting & Deleting a record

    Can someone please tell me what's wrong with this instruction:

    Code:
    String sql = "SELECT * FROM tblStudent WHERE Student Name = Samuel";
    ...And this one:
    Code:
    String input = JOptionPane.showInputDialog("Who do you want to delete:");
    String sql = "DELETE * FROM tblStudent WHERE Student Name = " + input;
  • madhoriya22
    Contributor
    • Jul 2007
    • 251

    #2
    Originally posted by 08butoryr
    Can someone please tell me what's wrong with this instruction:

    Code:
    String sql = "SELECT * FROM tblStudent WHERE Student Name = Samuel";
    ...And this one:
    Code:
    String input = JOptionPane.showInputDialog("Who do you want to delete:");
    String sql = "DELETE * FROM tblStudent WHERE Student Name = " + input;
    Hi,
    Is "Student Name" is column name in ur table ?

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by 08butoryr
      Can someone please tell me what's wrong with this instruction:

      Code:
      String sql = "SELECT * FROM tblStudent WHERE Student Name = Samuel";
      ...And this one:
      Code:
      String input = JOptionPane.showInputDialog("Who do you want to delete:");
      String sql = "DELETE * FROM tblStudent WHERE Student Name = " + input;
      Which database are you using?
      The first statement should have quotes around Samuel if name is of type varchar or varchar2 depending on your database.
      Your second statement should say delete from not delete * from.

      Comment

      • 08butoryr
        New Member
        • Sep 2007
        • 16

        #4
        Originally posted by madhoriya22
        Hi,
        Is "Student Name" is column name in ur table ?
        Firstly, thank you very much for responding to my problem. "Student Name" is indeed one of the column names in my table.

        Comment

        • 08butoryr
          New Member
          • Sep 2007
          • 16

          #5
          Originally posted by r035198x
          Which database are you using?
          The first statement should have quotes around Samuel if name is of type varchar or varchar2 depending on your database.
          Your second statement should say delete from not delete * from.
          Thanks also for responding. The database I am using is called "School". How do I tell if the name is of type varchar or varchar2? Do I check directly in the database?

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by 08butoryr
            Thanks also for responding. The database I am using is called "School". How do I tell if the name is of type varchar or varchar2? Do I check directly in the database?
            I don't mean the name of your database in that sense. I mean is it MySQL, Oracle, Access, e.t.c. The structure of the sql depends on the database.

            Comment

            • 08butoryr
              New Member
              • Sep 2007
              • 16

              #7
              Originally posted by r035198x
              I don't mean the name of your database in that sense. I mean is it MySQL, Oracle, Access, e.t.c. The structure of the sql depends on the database.
              It is Access. But in fact I've been working on this the whole day and I've virtually completed the whole thing. I have just 2 small problems left:
              1. In the search method, I need to figure out how to display "Name not found" when the user inputs a name that's not in the database. I tried using the following code but it didn't work:
              Code:
              boolean inputRepeatedly = true;
              while (inputRepeatedly = true)
              {
              try
              {	
              String input1 = JOptionPane.showInputDialog("Please enter student's name");
              if (input1.length() == 0)
              {
              inputRepeatedly = false;
              }
              2. How do you exit a submenu? i.e. The main menu pops up, user selects 'A) Insert name', user is repeatedly prompted to insert new records in the table until he wishes to cancel. I tried using the following code but it didn't work:
              Code:
              String input = JOptionPane.showInputDialog("Which name are you searching for?");
              rs = set.executeQuery("SELECT * FROM tblStudent WHERE StudentName = " + "'" + input + "'"); 
              
              while (rs.next())
              {
              name = rs.getString("StudentName");
              if (name.equals(input)
              {
              System.out.println(name + " is present in the table");
              }
              else
              {
              System.out.println(name + " is not present");
              }
              After these small problems are solved, I am completely done so thanks in abundance for helping.

              Comment

              • mrindia
                New Member
                • Sep 2007
                • 2

                #8
                Originally posted by 08butoryr
                Can someone please tell me what's wrong with this instruction:

                Code:
                String sql = "SELECT * FROM tblStudent WHERE Student Name = Samuel";
                ...And this one:
                Code:
                String input = JOptionPane.showInputDialog("Who do you want to delete:");
                String sql = "DELETE * FROM tblStudent WHERE Student Name = " + input;

                Hi,
                Some one gave good response. I am newly added in this group...

                so u better to use string.Format method .. here we can avoid collisions

                if u want assign any parameter to your query so better to create one format string based on that no need to take care of complicated built query

                string.Format(" Select statements from table where col =place holer",col1,col 2,...)

                Ex:

                String.Format(" Select * FROM tblStudent WHERE StudentName ='{0}' ",StudentNa me);

                SeleStat=String .Format("DELETE * FROM tblStudent WHERE StudentName = '{0}'",input);


                write like this ..if column is string u have specify place holder in string otherwise no need strings(Invited commas)...

                Note: columns should not take any gaps


                ok

                if it is helpful take it

                otherwise ...take just kidding
                bye

                Comment

                • 08butoryr
                  New Member
                  • Sep 2007
                  • 16

                  #9
                  Originally posted by mrindia
                  Hi,
                  Some one gave good response. I am newly added in this group...

                  so u better to use string.Format method .. here we can avoid collisions

                  if u want assign any parameter to your query so better to create one format string based on that no need to take care of complicated built query

                  string.Format(" Select statements from table where col =place holer",col1,col 2,...)

                  Ex:

                  String.Format(" Select * FROM tblStudent WHERE StudentName ='{0}' ",StudentNa me);

                  SeleStat=String .Format("DELETE * FROM tblStudent WHERE StudentName = '{0}'",input);


                  write like this ..if column is string u have specify place holder in string otherwise no need strings(Invited commas)...

                  Note: columns should not take any gaps


                  ok

                  if it is helpful take it

                  otherwise ...take just kidding
                  bye
                  Thank you very much - I will definitely take it!

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Have I suggest PreparedStateme nts yet?

                    Comment

                    Working...