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:-
Expand|Select|Wrap|Line Numbers
- 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);} }
- }