473,396 Members | 1,858 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,396 software developers and data experts.

java.sql.Statement Try with catch headaches

108 100+
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:-
Expand|Select|Wrap|Line Numbers
  1. package robsbeans;
  2. import java.sql.*;
  3.  
  4. public class DeleteFAQ
  5. {
  6.   private String faqId;
  7.  
  8.   // setter method
  9.   public void setfaqId(String InputfaqId){ faqId = InputfaqId; } 
  10.  
  11.   // getter method returns value of the bean properties
  12.   public String getfaqId() { return faqId; }
  13.  
  14.  
  15. public void updateDatabaseforDelete()    {
  16.  
  17.     Connection conn1 = null, conn2 = null;
  18.  
  19.     try
  20.     {
  21.          Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
  22.          conn1 = DriverManager.getConnection("jdbc:odbc:FAQ"); 
  23.     }
  24.     catch (Exception e1)  {System.out.print(e1);}
  25.  
  26.     {
  27.  
  28.     try
  29.     {  
  30.         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
  31.         conn2 = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver*.mdb)};DBQ=C:/ProgramFiles/Apache Software 
  32.  
  33. Foundation/Tomcat6.0/webapps/2008-sem2/wk465682/FAQ.mdb");        
  34.     }    
  35.  
  36.  
  37.     Connection conn;
  38.  
  39.     if(conn1 == null) conn = conn2;
  40.     else conn = conn1;
  41.  
  42.  
  43.     java.sql.Statement statement = conn.createStatement();
  44.     statement.executeUpdate("DELETE * FROM FAQ WHERE Id = '" + faqId + "'");
  45.  
  46.  
  47.  
  48.            if (statement != null)
  49.                 statement.close();
  50.            if (conn != null)
  51.                 conn.close();                    
  52.     }    
  53.     catch (Exception e2) {System.out.print(e2);}                            }
  54.  
  55. }  
Mar 12 '08 #1
4 1824
BigDaddyLH
1,216 Expert 1GB
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:

Expand|Select|Wrap|Line Numbers
  1. Connection getConnection() throws ClassNotFoundException, SQLException {
  2.     try {
  3.         Class.forName("class name 1...");
  4.         return DriverManager.getConnection("connection url 1...");
  5.     } catch (SQLException e) {
  6.         //log error?
  7.         Class.forName("class name 2...");
  8.         return DriverManager.getConnection("connection url 2...");
  9.     }
  10. }
Mar 12 '08 #2
robtyketto
108 100+
Thanks I will try that shortly.
Mar 12 '08 #3
robtyketto
108 100+
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 updateDatabaseforDelete. I think you meant to call it there instead:

old code:
Expand|Select|Wrap|Line Numbers
  1. public void updateDatabaseforDelete()    {
  2.     Connection getConnection() throws ClassNotFoundException, SQLException
new code:
Expand|Select|Wrap|Line Numbers
  1. public void updateDatabaseforDelete()    throws ??? {
  2.     Connection con = getConnection();
  3.  
Also, don't forget that everthing that is opened must eventually be closed.
Mar 13 '08 #4
robtyketto
108 100+
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 :-)
Mar 13 '08 #5

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

Similar topics

3
by: dinesh prasad | last post by:
I'm trying to use a servlet to process a form, then send that data to an SQL server stored procedure. I'm using the WebLogic 8 App. server. I am able to retrieve database information, so I know my...
2
by: Michael | last post by:
Running DB2 v7 UDB ("DB2 v7.1.0.93", "n031208" and "WR21333") on Windows XP, I am unable to find out why the "Build for Debug" option within Stored Procedure Builder is not enabled on Java stored...
6
by: Rhino | last post by:
I'm trying to debug a simple Java UDF written in the DB2General style within Eclipse. I'm getting a java.lang.UnsatisfiedLinkError when I execute the set() method in the UDF. I know that the...
4
by: mjahabarsadiq | last post by:
HELLO FRIENDS I HAVE ONE JAVA CODE WHICH PARSES AN XML FILE AND PRODUCE A QUERY TO CREATE TABEL IN A DATABASE. BUT I NEED THE CODE TO BE USED IN A JSP PAGE. HOW TO USE THIS PAGE WITH JSP. ...
1
by: jynxxxed | last post by:
I've been reviewing some of the threads here and applying some of the advice and still getting the same error. 15:50:26,935 INFO java.sql.SQLException: Could not find stored procedure...
2
by: astolpho | last post by:
I am using a slightly outdated reference book on J2EE programming. It gives 2 methods of creating a database used in its casestudies. The first is an ANT script that gives the following output: ...
0
oll3i
by: oll3i | last post by:
package library.common; import java.sql.ResultSet; public interface LibraryInterface { public ResultSet getBookByAuthor(String author); public ResultSet getBookByName(String name);
2
by: sdanda | last post by:
Hi , Do you have any idea how to improve my java class performance while selecting and inserting data into DB using JDBC Connectivity ......... This has to work for more than 8,00,000...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.