java.sql.Statement Try with catch headaches

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

    java.sql.Statement Try with catch headaches

    Greetings,

    I've got my head spinning with some very simple java coding, Im a newbie to Java and utilising the code below for a javabean for some jsp Im working on.

    The problem is my code I've introduced to check the conenction type for my SQL query, I cant get my heard around the nested statements.

    If it fails to get a connection then I want it try the next connection and finally when executing the SQL statement it should check for problems.

    Any advice would be appreaciated, Thanks Rob

    Code below:-
    Code:
    package robsbeans;
    import java.sql.*;
    
    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)  {System.out.print(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");        
    	}	
    	
    
    	Connection conn;
    	
    	if(conn1 == null) conn = conn2;
    	else conn = conn1;
    	
    
    	java.sql.Statement statement = conn.createStatement();
    	statement.executeUpdate("DELETE * FROM FAQ WHERE Id = '" + faqId + "'");
    
    	
    
       		if (statement != null)
            		statement.close();
       		if (conn != null)
            		conn.close();					
    	}	
    	catch (Exception e2) {System.out.print(e2);}							}
    					
    }
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    To simplify things with try/catch blocks it's a good idea to break your code into simpler methods. Buy then it's always a good idea to break your code into simpler methods. For example, why not write one that just gets the connection:

    [CODE=Java]Connection getConnection() throws ClassNotFoundEx ception, SQLException {
    try {
    Class.forName(" class name 1...");
    return DriverManager.g etConnection("c onnection url 1...");
    } catch (SQLException e) {
    //log error?
    Class.forName(" class name 2...");
    return DriverManager.g etConnection("c onnection url 2...");
    }
    }[/CODE]

    Comment

    • robtyketto
      New Member
      • Nov 2006
      • 108

      #3
      Thanks I will try that shortly.

      Comment

      • robtyketto
        New Member
        • Nov 2006
        • 108

        #4
        Your code is mixed up. I don't know how it looks in your editor, but it looks sloppy here. It's a good idea to indent carefully so that you can read your own code.

        Your syntax error is that you nested the definition of method getConnection inside method updateDatabasef orDelete. I think you meant to call it there instead:

        old code:
        [CODE=Java]
        public void updateDatabasef orDelete() {
        Connection getConnection() throws ClassNotFoundEx ception, SQLException[/CODE]

        new code:
        [CODE=Java]
        public void updateDatabasef orDelete() throws ??? {
        Connection con = getConnection() ;
        [/CODE]

        Also, don't forget that everthing that is opened must eventually be closed.

        Comment

        • robtyketto
          New Member
          • Nov 2006
          • 108

          #5
          Thanks for all your help.

          I loaded my file into dreamweaver sorted out the formatted, took my time and now its all working.

          Its for a university degree, so I've been rushing to meet deadlines but after taking my time and actually understanding the code , it all works and I feel much better :-)

          Comment

          Working...