Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL SELECT stmt 1

Status
Not open for further replies.

ceaton

MIS
Apr 26, 1999
27
US
I have a very large user interface, several panels, components, buttons, etc. I have approximately 146 fields that the user can possible fill in, they can fill them all in or only part of the fields. What I would like to do is do and INSERT of the data into my MSAccess database then perform the necessary queries and calculations on the data. This is a two part question, do you think an INSERT of 146 items is too large? Should I split this into several INSERT statements, but what happens if the user goes back and changes something after it has already been entered? I know to use the UPDATE, just never have. My last question is the most important and I can't seem to find the answer to this anywhere, in a SELECT statement I wan't to do something like this: SELECT * FROM regions WHERE state = 'Florida' AND region = 'Palm Beach'&quot;; Now this work however I don't want 'Florida' and 'Palm Beach' in there I wan't the the field from the user interface, ie chState and chRegion, how do I accomplish this, I've tried with double quotes and get anything to work. Any help would be greatly appreciated. Just a note, this a stand-alone application with no use of a server or web-browser, strictly run in the appletviewer.<br>
<br>
Thanks in advance! <p> Courtney<br><a href=mailto: ceaton@lrp.com> ceaton@lrp.com</a><br><a href= > </a><br>
 
Hi!<br>
<br>
1. No. (but M$Access is a M$ product...)<br>
2. You have two ways:<br>
<br>
Make the query string manually:<br>
String qs=&quot;select * from REGIONS where state='&quot;+stateTextField.getText()+<br>
&quot;' and region='&quot;+regionTextField.getText()+&quot;'&quot;;<br>
Statement stmt=conn.createStatement();<br>
ResultSet rs=stmt.executeQuery(qs);<br>
<br>
Or use prepared statement and parameter passing:<br>
String qs=&quot;select * from REGIONS where state=? and region=?&quot;;<br>
PreparedStatement pstmt=conn.prepareStatement(qs);<br>
pstmt.setString(1, stateTextField.getText()); // sets the first parameter<br>
pstmt.setString(2, regionTextField.getText()); // sets the second<br>
... // sets all parameters (setInt, setLong, etc. see the jdbc doc)<br>
ResultSet rs.pstmt.executeQuery();<br>
<br>
Good luck. Bye, Otto.<br>

 
That worked perfectly!! Thanks, I have one more question for you though. On an INSERT I'm doing the following and getting this error: SQLException: General Error<br>
<br>
Here is the SQL:<br>
<br>
String query = &quot;INSERT INTO caseInfo(caseName, evalDate, &quot;<br>
<br>
+ &quot;docketNum, state, county, geoType, caseSum,&quot; <br>
<br>
+ &quot;plainName, plainAge, plainSex, &quot; <br>
<br>
+ &quot;plainRace, plainMarital, plainOcc, plainAlcohol, &quot;<br>
<br>
+ &quot;plainObesity,plainCriminal, plainDeathUn, &quot; <br>
<br>
+ &quot;plainHandi ) &quot; <br>
<br>
+ &quot;VALUES('&quot;+labelcaseName.getText()+&quot;', &quot; <br>
<br>
+ &quot;'&quot;+labelevalDate.getText()+&quot;', &quot;<br>
<br>
+ &quot;'&quot;+labelfileNum.getText()&quot;', &quot; <br>
<br>
+ &quot;'&quot;+labelstate.getText()+&quot;', &quot;<br>
<br>
+ &quot; '&quot;+labelcounty.getText()+&quot;',&quot; <br>
<br>
+ &quot;'&quot;+labeltypeofCounty.getText()+&quot;', &quot;<br>
<br>
+ &quot;'&quot;+labelcaseS.getText()+&quot;',&quot; <br>
<br>
+ &quot;'&quot;+labelpname.getText()+&quot;', '&quot;+labelpage.getText()+&quot;',&quot; <br>
<br>
+ &quot;'&quot;+labelpsex.getText()+&quot;', '&quot;+labelprace.getText()+&quot;',&quot; <br>
<br>
+ &quot;'&quot;+labelpmarry.getText()+&quot;',&quot; <br>
<br>
+ &quot;'&quot;+labelpocc.getText()+&quot;', '&quot;+labelpada.getText()+&quot;',&quot; <br>
<br>
+ &quot;'&quot;+labelobesity.getText()+&quot;', '&quot;+labelpcr.getText()+&quot;',&quot; <br>
<br>
+ &quot;'&quot;+labelpdeathUn.getText()+&quot;', &quot;<br>
<br>
+ &quot;'&quot;+labelhandi.getText()&quot;')&quot;; <br>
<br>
<br>
This is just a snippet, there are 96 total fiels that could be inserted. Is it possible that I'm getting this error because some of these are blank? If so, how do skip over blank fields?<br>
<br>
Thanks,<br>
Courtney<br>
<p> Courtney<br><a href=mailto: ceaton@lrp.com> ceaton@lrp.com</a><br><a href= > </a><br>
 
Hi!<br>
<br>
I do not know the structure (field types) of your table, and<br>
unfortunately I do not know what does it mean &quot;Genereal error&quot; in M$Access term, thus I can make guesses only:<br>
1. The whole query string is wrong ('' pairs, etc.).<br>
2. You use quotes all field types, but you for integers you must not do it.<br>
3. Try to assign empty string(s) to field(s), but them were created &quot;not null&quot;.<br>
4. The getText() give back an empty string (&quot;&quot;, not null) when the given field is empty. If you have only string fields (I think not), it will be good.<br>
But for integer, date, float, etc. fields it make conversion error.<br>
...<br>
X. M$ bug or feature :)).<br>
<br>
I hope these things will help you, and you can find the mistake. If no, write here the structure and query string.<br>
<br>
Good luck. Bye, Otto.<br>

 
I guess I don't really fully understand because all of the fields that I'm getting my information from are all labelFields. Aren't these String fields? In my database (MSAccess) all of the fields are TextFields. So why would it matter if the data is numerical? I also really didn't fully understand the &quot;Null&quot; issue. Please elaborate. The stucture in my database is 96 fields, all of them text fields. I am retrieving the data from labelFields and a partial example of the query string is in my other post.<br>
<br>
Thanks,<br>
<br>
<p> Courtney<br><a href=mailto: ceaton@lrp.com> ceaton@lrp.com</a><br><a href= > </a><br>
 
Hi!<br>
<br>
I am very sorry, but my english is poor :-(((.<br>
I saw in your post evalDate, docketNum, etc. fields, so I thought the evalDate was declared as DATE, docketNum was declared as INTEGER in the M$Access database (table), so I thought when the user give empty string then the query string will be &quot;insert into .. (evalDate) values ('')&quot; and it will cause sql exception (the &quot;... (docketNum) values ('1')&quot; is wrong too).<br>
<br>
In your last post wrote that all fields are declared as STRING, so that I wrote above will not matter.<br>
<br>
If you create the database so that some fields will not not blank (like this &quot;DECLARE TABLE PERSONS(ID INTEGER, NAME CHAR(30) NOT NULL)&quot;) and the user leave blank these fields it will cause sql exception.<br>
<br>
But what does it mean &quot;General error&quot; in M$Access, I do not know (I installed the access98 now, but I could not find this error :-(().<br>
<br>
I am really sorry. Anyway you may try other SQL server(s) (if possible).<br>
<br>
Good luck. Bye, Otto.<br>

 
Basically what I need to know how to do is pass a null value to the database. I cannot use an SQL server because this is a stand-alone applications which will be installed by the user from a CD-ROM. So for example if I have the SQL<br>
<br>
String query = &quot;INSERT INTO caseInfo(caseName, evalDate, &quot;<br>
<br>
+ &quot;docketNum )&quot; <br>
<br>
+ &quot;VALUES('&quot;+labelcaseName.getText()+&quot;', &quot; <br>
<br>
+ &quot;'&quot;+labelevalDate.getText()+&quot;', &quot;<br>
<br>
+ &quot;'&quot;+labelfileNum.getText()&quot;')&quot;;<br>
<br>
<br>
and nothing is entered in for the Docket/File Number field, how do I send the other info to the database without the SQL failing entirely?<br>
<br>
Thanks, <p> Courtney<br><a href=mailto: ceaton@lrp.com> ceaton@lrp.com</a><br><a href= > </a><br>
 
The &quot;INSERT INTO caseInfo(caseName, evalDate) VALUES(null, null)&quot;; inserts<br>a record into the table and sets these two fields null (blank).<br>You should do this for all fields. You can make a converter method like this:<br><br>public String getAsSQLString(TextField tf) {<br> String s;<br><br> s=tf.getText();<br> if ((s==null) ¦ (s.length()==0)) { s=&quot;null&quot;; }<br> else { s=&quot;'&quot;+s+&quot;'&quot;; }<br>}<br><br>After all the sql string will be like this:<br><br>String query = &quot;INSERT INTO caseInfo(caseName, evalDate, &quot;<br>+&quot;docketNum )&quot; <br>+&quot;VALUES(&quot;+getAsSQLString(labelcaseName)<br>+&quot;,&quot;+getAsSQLString(labelevalDate)<br>+&quot;,&quot;+getAsSQLString(labelfileNum)<br>+&quot;)&quot;;<br><br>Good luck. Bye, Otto.<br>
 
Of course, I wanted to write ¦¦, not ¦.<br>
if ((s==null) ¦¦ (s.length()==0)) {<br>
<br>
Sorry. By, Otto.<br>

 
Hey Otto, That all works great, yes of course there is but to this. When I need to pass a name such as &quot;O'brian&quot; or &quot;Plaintiff's&quot; it is giving me an error. How do I get around this?<br>
<br>
Thanks,<br>
<p> Courtney<br><a href=mailto: ceaton@lrp.com> ceaton@lrp.com</a><br><a href= > </a><br>
 
&quot;Nothing is perfect&quot;, said the hedgehog and climbed off the scrubbing-brush...<br>
<br>
Possible solutions:<br>
1. Use parameter passing (I have adviced and described it in my first post).<br>
2. Put more cleverness to the converter method, like this:<br>
<br>
public String getAsSQLString(TextField tf) {<br>
String s;<br>
StringBuffer sb;<br>
int i, l;<br>
<br>
s=tf.getText();<br>
if ((s==null) ¦¦ (s.length()==0)) { s=&quot;null&quot;; }<br>
else {<br>
sb=new StringBuffer(s);<br>
l=s.length();<br>
for (i=l-1; i&gt;=0; i--) {<br>
if (sb.charAt(i)==''') { sb.insert(i, '''); }<br>
}<br>
s=&quot;'&quot;+sb.toString()+&quot;'&quot;;<br>
}<br>
return s;<br>
}<br>
Note: If you want to put ' to the query string put '' (' twice).<br>
<br>
Good luck. Bye, Otto.<br>

 
Thanks Otto for the help, but the code won't compile.<br>
<br>
I'm getting errors on this part of the code:<br>
<br>
if (sb.charAt(i)== ' ' ' ) { sb.insert(i, ' ' ' );<br>
<br>
Invalid character constant. and it points to the first single quote on each one. These are single quotes right?<br>
<br>
Any ideas, I tried couple of things, but my inexperience isn't helping. Sorry to keep bothering you about this, I do really appreciate all of your assistance.<br>
<br>
<p> Courtney<br><a href=mailto: ceaton@lrp.com> ceaton@lrp.com</a><br><a href= > </a><br>
 
I forgot to mention that I will also need to pass an 's similar to &quot;Plaintiff's Drug Addiction&quot;. Will this function work with this also?<br>
<br>
Thanks <p> Courtney<br><a href=mailto: ceaton@lrp.com> ceaton@lrp.com</a><br><a href= > </a><br>
 
Hi!<br>
<br>
It seems that I found an incompatibility (bug) in jdk1.1.8.<br>
I work with 1.1.8, and tested the code on it, and worked well.<br>
But after your post I tried to compile with 1.2.1, and I got the<br>
errors like you. Sorry.<br>
The corrected line is:<br>
if (sb.charAt(i)=='\'') { sb.insert(i, '\''); }<br>
<br>
I think (and hope :)) finally :) it will work for every variations.<br>
<br>
Good luck. Bye, Otto.<br>

 
I am using jdk1.2.2 and I tried that one :-( earlier and got errors for everyone of my Inserts. Unbelievable isn't!<br>
<br>
I tried a bunch of various ways with the single quote and the double quote with the escape and I can't get it to work.<br>
<br>
<p> Courtney<br><a href=mailto: ceaton@lrp.com> ceaton@lrp.com</a><br><a href= > </a><br>
 
Otto,<br>
<br>
I was mistaken when I told you that I tried that, I hadn't.<br>
<br>
This is what I did try:<br>
<br>
public String getAsSQLString(Label tf) {<br>
String s;<br>
<br>
s=tf.getText();<br>
if (s.charAt(i)==' \ ' ') { s.insert(i, ' \ ' '); }<br>
else { s=&quot; ' &quot;+s+&quot; ' &quot;; } <br>
return s;<br>
}<br>
<br>
The error that I'm getting is for the i and it reads:<br>
Undefined variable: i<br>
<br>
I know I must be missing something, I'm just not sure what.<br>
<br>
Any ideas?<br>
<br>
Thanks again,<br>
Courtney<br>
<p> Courtney<br><a href=mailto: ceaton@lrp.com> ceaton@lrp.com</a><br><a href= > </a><br>
 
Hi!<br>
<br>
Here is the full code again :)) :<br>
<br>
public String getAsSQLString(TextField tf) {<br>
String s;<br>
StringBuffer sb;<br>
int i, l;<br>
<br>
s=tf.getText();<br>
if ((s==null) ¦¦ (s.length()==0)) { s=&quot;null&quot;; }<br>
else {<br>
sb=new StringBuffer(s);<br>
l=s.length();<br>
for (i=l-1; i&gt;=0; i--) {<br>
if (sb.charAt(i)=='\'') { sb.insert(i, '\''); }<br>
}<br>
s=&quot;'&quot;+sb.toString()+&quot;'&quot;;<br>
}<br>
return s;<br>
}<br>
I have tested it with 1.1.8 and 1.2.1, and it's worked well.<br>
<br>
<br>
Good luck. Bye, Otto.<br>

 
Thanks a million Otto!<br>
<br>
It works like a champ!!<br>
<br>
Courtney <p> Courtney<br><a href=mailto: ceaton@lrp.com> ceaton@lrp.com</a><br><a href= > </a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top