You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Zhou, Qin (Eric)" <qz...@lucent.com> on 2003/11/12 21:36:26 UTC

Connect close the DB connection in Form Action

I have the following code in my Form file. The methods retrieve data from access DB to populated the two drop down boxes. But con.close() generates 'General error' exception? Any help is appreciated.


	/**
		 * Return a list of week ending date
		 * @return	ArrayList	List of week ending date
	 */
	
	private static ArrayList getWeekEndingDateList()
	{
		Connection con = null;
		statement stmt = null;
		ResultSet rs = null;
		ArrayList weekEndingDate = new ArrayList();

		try {
			//Register driver:
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance(); 
			//Establish a connection to given database URL:
			con = DriverManager.getConnection("jdbc:odbc:Timesheet","","");
			//Create a Statement object for sending SQL statements to the database:
			stmt = con.createStatement();
   
			String sql = "select * from Weeks order by Wk_Ending";
;			
				rs = stmt.executeQuery(sql);

				System.out.println("SQL = " + sql);

			while (rs.next()) {

				String weekNum = rs.getString("Wk_Num");
				String wkEndingDate = rs.getString("Wk_Ending");
				//System.out.println("Week Num = " + weekNum);
				//System.out.println("wkEndingDate = " + wkEndingDate);

				weekEndingDate.add(new LabelValueBean(wkEndingDate, wkEndingDate));
			
			}

		} catch(Exception e) 
			{ 
				System.out.println(e.toString()); 
			}
			finally {
				//Close the connection:
				try {
						if (rs != null) rs.close();
						if (stmt != null) stmt.close();
						if (con != null) con.close();
	
				} catch (SQLException e) {
					System.out.println(e.toString()); 
				}
			}


		return weekEndingDate;
	}


	/**
		 * Return a list of work category
		 * @return	ArrayList	List of work category
	 */
	
	private static ArrayList getWorkCategoryList()
	{
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		ArrayList workCategory = new ArrayList();

		try {
			//Register driver:
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance(); 
			//Establish a connection to given database URL:
			con = DriverManager.getConnection("jdbc:odbc:Timesheet","","");
			//Create a Statement object for sending SQL statements to the database:
			stmt = con.createStatement();
   
			String sql = "select * from workCategory";
;			
				rs = stmt.executeQuery(sql);

				System.out.println("SQL = " + sql);

			while (rs.next()) {

				String categoryNum = rs.getString("Cat_Num");
				String workCategories = rs.getString("Category");
				//System.out.println("Category Num = " + categoryNum);
				//System.out.println("work category = " + workCategories);

				workCategory.add(new LabelValueBean(workCategories, workCategories));
			
			}

		} catch(Exception e) 
			{ 
				System.out.println(e.toString()); 
			}
			finally {
				//Close the connection:
				try {
						if (rs != null) rs.close();
						if (stmt != null) stmt.close();
						if (con != null) con.close();
	
				} catch (SQLException e) {
					System.out.println(e.toString()); 
				}
			}


		return workCategory;
	}

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org