473,325 Members | 2,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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

108 100+
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>
wk465682DeleteFAQbean.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 1720
JosAH
11,448 Expert 8TB
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 100+
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 Expert 8TB
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.parseInt() method will throw a NumberFormatException 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 100+
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
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...
2
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...
2
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...
1
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...
1
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...
5
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...
1
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...
2
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...
1
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.