473,750 Members | 2,669 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

JDBC SQL: No errors, but problems with string to Integer conversions

108 New Member
Greetings,

I'm a newbie to Java and I cant quite get my head around why it seems most integers appears to be formatted as strings and then later have to be parsed.

The code below, passes in the autonumber table field Id value named faqId which in the HTML code the Input type is "Int"

Expand|Select|Wrap|Line Numbers
  1. <FORM NAME ="deletefaqs" ACTION="wk465682DeleteFAQbean.jsp" METHOD="POST"><PRE>
  2.   Id: <INPUT TYPE="Int" NAME="faqId">
  3. <BR><BR>
  4. <input type="submit"  value="Delete FAQ" size="8">
  5. </FORM>
wk465682DeleteF AQbean.jsp retrives the vaule from the form using the code below:-

Expand|Select|Wrap|Line Numbers
  1. <%-- create an instance of the Delete JavaBean and give it the id name of 'deletefaqs' --%>
  2. <jsp:useBean id="deletefaqs" class="robsbeans.DeleteFAQ" scope="request" />
  3.  
  4. <%-- call the bean instance's setter method to assign values from the html form properties --%>
  5. <jsp:setProperty name="deletefaqs" property="*" />
then it calls the java bean to execute the database update
Expand|Select|Wrap|Line Numbers
  1. <%-- call the bean instances method to update the database --%>
  2. <% deletefaqs.updateDatabaseforDelete(); %>
Finally the javabean code below compiles and runs, and I believe doesnt delete a record because the table field Id (autonumber) is compared against faqId value which should be an integer but is really a number.

Its runs and doesnt delete anything, I've printed out the value of faqId and it does print the value as it appears on the form i.e. its not NULL

Expand|Select|Wrap|Line Numbers
  1. package robsbeans;
  2. import java.sql.*;
  3. import java.io.*;
  4.  
  5. public class DeleteFAQ
  6. {
  7.      private String faqId;
  8.  
  9.       // setter method
  10.       public void setfaqId(String InputfaqId){ faqId = InputfaqId; } 
  11.  
  12.       // getter method returns value of the bean properties
  13.       public String getfaqId() { return faqId; }
  14.  
  15.  
  16. public void updateDatabaseforDelete() {
  17.  
  18.     Connection conn1 = null, conn2 = null;
  19.  
  20.         try    {
  21.                 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
  22.                 conn1 = DriverManager.getConnection("jdbc:odbc:FAQ");
  23.             } 
  24.             catch (Exception e1) 
  25.             {
  26.                 try        
  27.                 {
  28.                     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
  29.                     conn2 = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver*.mdb)};DBQ=C:/ProgramFiles/Apache Software Foundation/Tomcat6.0/webapps/2008-sem2/wk465682/FAQ.mdb");
  30.                  }
  31.                 catch (Exception e2) {System.out.print(e2);}
  32.             }
  33.  
  34.     Connection  conn;
  35.     if(conn1 == null) conn = conn2;
  36.     else conn = conn1;
  37.  
  38.     try {
  39.             java.sql.Statement statement = conn.createStatement();
  40.             String myquery ="DELETE * FROM FAQ WHERE Id = ? ";
  41.             PreparedStatement mystatement = conn.prepareStatement(myquery);
  42.             mystatement.setInt(1,Integer.parseInt(faqId));
  43.  
  44.         if (statement != null)
  45.             statement.close();
  46.         if (conn != null)
  47.             conn.close();
  48.         }
  49.             catch (Exception e3) {System.out.print(e3);}
  50.                                                                 }  
  51. }
Any advice to where im going wrong and how to get my head around stings/Integers and Ints?
Mar 15 '08 #1
4 1743
JosAH
11,448 Recognized Expert MVP
The 'type="Int"' tag is not a valid tag. The valid type values are: button, checkbox,
file, hidden, image, password, radio, reset, submit and text,where the default
type is 'text' so you created just a text type input field and it posts its value as
a string.

kind regards,

Jos
Mar 15 '08 #2
robtyketto
108 New Member
Ahh I was thinking that, Ive seen quite a few examples on the web that contradict each other regarding INPUT type but assumed it was always TEXT.

Ok, should this code pass the value of Id as an Int now though?

Expand|Select|Wrap|Line Numbers
  1. java.sql.Statement statement = conn.createStatement();
  2.             String myquery ="DELETE * FROM FAQ WHERE Id = ? ";
  3.             PreparedStatement mystatement = conn.prepareStatement(myquery);
  4.             mystatement.setInt(1,Integer.parseInt(faqId));
Cheers
Rob
Mar 15 '08 #3
JosAH
11,448 Recognized Expert MVP
Ahh I was thinking that, Ive seen quite a few examples on the web that contradict each other regarding INPUT type but assumed it was always TEXT.

Ok, should this code pass the value of Id as an Int now though?

Expand|Select|Wrap|Line Numbers
  1. java.sql.Statement statement = conn.createStatement();
  2.             String myquery ="DELETE * FROM FAQ WHERE Id = ? ";
  3.             PreparedStatement mystatement = conn.prepareStatement(myquery);
  4.             mystatement.setInt(1,Integer.parseInt(faqId));
Cheers
Rob
Only if the String 'faqId' represents an integer this'll work because otherwise the
Integer.parseIn t() method will throw a NumberFormatExc eption at you. You have
to anticipate on that and return an error to the user if that exception is thrown.

kind regards,

Jos
Mar 15 '08 #4
robtyketto
108 New Member
I dont BELIEVE IT !!!

I didnt execute my query thats why there was no visible results.
Expand|Select|Wrap|Line Numbers
  1. mystatement.executeUpdate();
Point taken if i enter GJGJ on the form though, I will look at adding better error handling in.

Thanks
Rob
Mar 15 '08 #5

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

Similar topics

4
6345
by: Dani | last post by:
Hi everyone Description of the problem: Using a PreparedStatement to write down an integer (int) plus a timestamp for testing purposes. When read out again the integer looks very different. We found that it was shifted three Bytes to the left, i.e. 4 becomes hex 4000000 which is 67108864 in decimal base. This means that the value written and the value read sometimes do not match, which is of course inacceptable for all real world...
2
5289
by: Marc Stiegele | last post by:
Hello, I have a problem with the IBM DB2 UDB XML Extender. I`m working on a iSeries 400 server (AS/400, V5R1) with a integrated DB2 (my client is NT 4.0 Service Pack 1 with JDK1.1.8). I want to create XML files out of the DB2 data using the XML collections method and SQL mapping. I successfully enabled my database, put the collection.dtd into DTD_REF, created a DAD file and enabled the collection (xcolltest01) using the OS/400 command...
2
9244
by: Kent Lewandowski | last post by:
hi all, Recently I wrote some stored procedures using java jdbc code (admittedly my first stab) and then tried to implement the same within java packages (for code reuse). I encountered problems doing this. I wanted to implemented a generic "Helper" class like this: /** * Helper
1
4678
by: Andrea | last post by:
Hello, I spent now several hours searching the google groups without finding an solution. I am kind of Newbie to DB2 and JSP and therefore working with JSP4Dummies (not sure whether I should recommend this book): Invoking the following jsp-file in the browser I get an ClassNotFoundException for the COM.ibm.db2.jdbc.app.DB2Driver. JSP-File
1
3861
by: Rich | last post by:
Hello, I created a DTS package VB6 script with DTS from Sql Server 2000. In a vb.net project I add a reference to Microsoft DTSpackage object library and copy the code from the DTS script to a module in the vb.net project. The following lines of code are a few of the lines with syntax issues and the description of the issue (below that is the sub (DTSrun) that these lines came from). My request is if someone could explain how to fix...
5
2691
by: Dino Nardini | last post by:
Hey folks, I'm currently evaluating an upgrade path from our current ColdFusion 5 / PostgreSQL setup to ColdFusion MX / PostgreSQL. In the current setup, we're using the Merant ODBC driver for postgres. I've installed Fedora Core 2 with Apache 2 and ColdFusion MX, and I grabbed the latest JDBC driver from the postgres website. I copied one of our production websites over to the test box, but ran into
1
2698
by: tom.eeraerts | last post by:
Hello, I have a problem migrating an application from v5r2 to v5r3. The problem is with the prepared statements. To see what the problem is, i extracted a small piece of code and debugged the application while running it on my local window machine (IBM WSAD) but still connecting to the AS400. For the local debugging I use "com.ibm.as400.access.AS400JDBCDriver"
2
4557
by: bevis | last post by:
I'm new to sql server and mysql but this seems like it should be a pretty straight forward jdbc connection. But I have spent almost 2 days just trying to get a jdbc connection. Please help if you can. I'm using Eclipse 3.2.2 and I have installed mysql-connector-java-5.0.6-bin.jar. I am trying to connect to SQL Enterprise Manager version 8.0 innstall on a Windows 2003 Server Enterprise Edition. I have been able to successfully test...
1
1888
by: wnaveenkumar | last post by:
package com.trewport.orderprocess.action; import java.io.*; import java.sql.*; import java.util.*; import java.util.Date; import java.lang.Object; import javax.servlet.*; import javax.servlet.http.*;
0
8838
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9583
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9396
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6808
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6081
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4716
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4888
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3323
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2807
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.