473,569 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL & Database: Selecting & Deleting a record

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

Expand|Select|Wrap|Line Numbers
  1. String sql = "SELECT * FROM tblStudent WHERE Student Name = Samuel";
...And this one:
Expand|Select|Wrap|Line Numbers
  1. String input = JOptionPane.showInputDialog("Who do you want to delete:");
  2. String sql = "DELETE * FROM tblStudent WHERE Student Name = " + input;
Sep 27 '07 #1
9 1872
madhoriya22
252 Contributor
Can someone please tell me what's wrong with this instruction:

Expand|Select|Wrap|Line Numbers
  1. String sql = "SELECT * FROM tblStudent WHERE Student Name = Samuel";
...And this one:
Expand|Select|Wrap|Line Numbers
  1. String input = JOptionPane.showInputDialog("Who do you want to delete:");
  2. String sql = "DELETE * FROM tblStudent WHERE Student Name = " + input;
Hi,
Is "Student Name" is column name in ur table ?
Sep 27 '07 #2
r035198x
13,262 MVP
Can someone please tell me what's wrong with this instruction:

Expand|Select|Wrap|Line Numbers
  1. String sql = "SELECT * FROM tblStudent WHERE Student Name = Samuel";
...And this one:
Expand|Select|Wrap|Line Numbers
  1. String input = JOptionPane.showInputDialog("Who do you want to delete:");
  2. 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.
Sep 27 '07 #3
08butoryr
16 New Member
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.
Sep 27 '07 #4
08butoryr
16 New Member
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?
Sep 27 '07 #5
r035198x
13,262 MVP
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.
Sep 27 '07 #6
08butoryr
16 New Member
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:
Expand|Select|Wrap|Line Numbers
  1. boolean inputRepeatedly = true;
  2. while (inputRepeatedly = true)
  3. {
  4. try
  5. {    
  6. String input1 = JOptionPane.showInputDialog("Please enter student's name");
  7. if (input1.length() == 0)
  8. {
  9. inputRepeatedly = false;
  10. }
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:
Expand|Select|Wrap|Line Numbers
  1. String input = JOptionPane.showInputDialog("Which name are you searching for?");
  2. rs = set.executeQuery("SELECT * FROM tblStudent WHERE StudentName = " + "'" + input + "'"); 
  3.  
  4. while (rs.next())
  5. {
  6. name = rs.getString("StudentName");
  7. if (name.equals(input)
  8. {
  9. System.out.println(name + " is present in the table");
  10. }
  11. else
  12. {
  13. System.out.println(name + " is not present");
  14. }
After these small problems are solved, I am completely done so thanks in abundance for helping.
Sep 27 '07 #7
mrindia
2 New Member
Can someone please tell me what's wrong with this instruction:

Expand|Select|Wrap|Line Numbers
  1. String sql = "SELECT * FROM tblStudent WHERE Student Name = Samuel";
...And this one:
Expand|Select|Wrap|Line Numbers
  1. String input = JOptionPane.showInputDialog("Who do you want to delete:");
  2. 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
Sep 27 '07 #8
08butoryr
16 New Member
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!
Sep 27 '07 #9
r035198x
13,262 MVP
Have I suggest PreparedStateme nts yet?
Sep 27 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

8
2821
by: DylanM | last post by:
I have some checkboxes that are generated from the results of a database search. At the moment, the checkboxes are part of a table making up a form. Users are going through the form, clicking the boxes and saving to the database at the end with the 'Submit' command button. Is it possible to save the changes as the checkboxes are clicked? I...
3
2403
by: james.dixon | last post by:
Hi I was wondering if anyone else had had this problem before (can't find anything on the web about it). I have three select elements (list boxes - from here on I'll refer to them as 'the list boxes'). Users can add and remove items from the list boxes. When the users are adding and removing items, the list boxes are single
7
2703
by: Danny J. Lesandrini | last post by:
I know this has been discussed before, as I've seen the Google posts, but they all leave me with an unanswered question: Does my DAO code executed in the front end cause the back end to bloat? (May also substitute UPDATE and/or DELETE queries for DAO code.) I was just brought on to a project with Access 97 where the all data is kept on...
3
3885
by: Tim Marshall | last post by:
HI all, Access 2003, Jet back end. Rather than annoy my users in a particular app by having relationships with enforced relational integrity refuse to delete a record with related records, I'm using cascade delete. When I use a continuous form and a record is deleted, Access provides a warning that there are related records, do you want...
4
4070
by: Ed L. | last post by:
I think I'm seeing table-level lock contention in the following function when I have many different concurrent callers, each with mutually distinct values for $1. Is there a way to reimplement this function using select-for-update (or equivalent) in order to get a row-level lock (and thus less contention) while maintaining the function...
4
1906
by: Lada 'Ray' Lostak | last post by:
Hello there, I am thinking how to solve another typical problem of online systems with combination of thin client... Imagine simple case, 2 users are going to edit 'same' datas. Both see on the 'screen' the same, after they started edit them. First one changes datas and submit changes (sucessfully). Database (set tables -...
25
2539
by: Colin McKinnon | last post by:
Hi all, There's lots of DB abstraction layers out there, but a quick look around them hasn't turned up anything which seems to met my requirements. Before I go off and write one I thought I'd ask here if anyone knows of such a beast... I want some code where I present an array of data, and the corresponding primary key and let the code...
2
1248
by: panki | last post by:
Hi Frnds As a college project, I am developing college admission software in VB.net using SQL server 7.0 as backend. Problem is that The Record is not Deleting from the Database. and It is showing Error. I used same code for MS Access database and the record is being deleted. Please Help me. My code is...
1
3308
by: solargovind | last post by:
Hi, I have one Form in which i have one subform also which links together by one common_id(Payment_id). When i display record, I need to delete Current record in both form & Table. I used the following code to delete. --- Dim response Dim strsql As String response = MsgBox("Are you sure to Delete?", vbYesNo, "Payment Alteratiion") If...
6
4922
by: kfanarmy | last post by:
Hello; I am attempting to use a command button "Delete Current Assignment Record" on a form to delete a user-selected record from a table entitled "Registration" with Key field "Registration ID". I am using a combo box named "Select Task" that displays info from three tables to enable the user to select a record. The form's data source is...
0
7618
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7926
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8138
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7679
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6287
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
946
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.