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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • robtyketto
    New Member
    • Nov 2006
    • 108

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

    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"

    Code:
    <FORM NAME ="deletefaqs" ACTION="wk465682DeleteFAQbean.jsp" METHOD="POST"><PRE>
      Id: <INPUT TYPE="Int" NAME="faqId">
    <BR><BR>
    <input type="submit"  value="Delete FAQ" size="8">
    </FORM>
    wk465682DeleteF AQbean.jsp retrives the vaule from the form using the code below:-

    Code:
    <%-- create an instance of the Delete JavaBean and give it the id name of 'deletefaqs' --%>
    <jsp:useBean id="deletefaqs" class="robsbeans.DeleteFAQ" scope="request" />
    
    <%-- call the bean instance's setter method to assign values from the html form properties --%>
    <jsp:setProperty name="deletefaqs" property="*" />
    then it calls the java bean to execute the database update
    Code:
    <%-- call the bean instances method to update the database --%>
    <% 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

    Code:
    package robsbeans;
    import java.sql.*;
    import java.io.*;
    
    public class DeleteFAQ
    {
     	private String faqId;
      
      	// setter method
      	public void setfaqId(String InputfaqId){ faqId = InputfaqId; } 
    
      	// getter method returns value of the bean properties
      	public String getfaqId() { return faqId; }
     
    
    public void updateDatabaseforDelete() {
       
    	Connection conn1 = null, conn2 = null;
    		
       	 try	{
            		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
            		conn1 = DriverManager.getConnection("jdbc:odbc:FAQ");
        	    } 
    			catch (Exception e1) 
    			{
    			    try		
    				{
    	        		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
            			conn2 = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver*.mdb)};DBQ=C:/ProgramFiles/Apache Software Foundation/Tomcat6.0/webapps/2008-sem2/wk465682/FAQ.mdb");
        			 }
    				catch (Exception e2) {System.out.print(e2);}
    			}
    																					
    	Connection  conn;
    	if(conn1 == null) conn = conn2;
    	else conn = conn1;
    	
    	try {
    			java.sql.Statement statement = conn.createStatement();
    			String myquery ="DELETE * FROM FAQ WHERE Id = ? ";
    			PreparedStatement mystatement = conn.prepareStatement(myquery);
    			mystatement.setInt(1,Integer.parseInt(faqId));
    		
    		if (statement != null)
    			statement.close();
    		if (conn != null)
    			conn.close();
    		}
    			catch (Exception e3) {System.out.print(e3);}
    	    														}  
    }
    Any advice to where im going wrong and how to get my head around stings/Integers and Ints?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    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

    Comment

    • robtyketto
      New Member
      • Nov 2006
      • 108

      #3
      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?

      Code:
      java.sql.Statement statement = conn.createStatement();
                  String myquery ="DELETE * FROM FAQ WHERE Id = ? ";
                  PreparedStatement mystatement = conn.prepareStatement(myquery);
                  mystatement.setInt(1,Integer.parseInt(faqId));
      Cheers
      Rob

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by robtyketto
        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?

        Code:
        java.sql.Statement statement = conn.createStatement();
                    String myquery ="DELETE * FROM FAQ WHERE Id = ? ";
                    PreparedStatement mystatement = conn.prepareStatement(myquery);
                    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

        Comment

        • robtyketto
          New Member
          • Nov 2006
          • 108

          #5
          I dont BELIEVE IT !!!

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

          Thanks
          Rob

          Comment

          Working...