Code example for PreparedStatement

Methods: close, execute, setFloat, setInt, setString

0
 
	} 
	public void closeConnections(PreparedStatement st,Connection conn){
		try { 
			if (st != null) {
                st.close();
            }if(conn !=null){
            	conn.close();
            } 
		} catch (Exception e2) {
			e2.printStackTrace();
		} 
	} 
	 
	public void addLocation(Location loc){
		try {	 
			conn = DbConnection.GetConnection();
			st = conn.prepareStatement("INSERT INTO `fall2012g1`.`location` (`LOCATION_ID`, `NAME`, `ADDRESS1`, `ADDRESS2`, `CITY`, `REGION`, `ZIP_CODE`, `STATE`, `LATITUDE`, `LONGITUDE`) VALUES (NULL, ?, ?, ?,?, ?, ?,?,?,?);");
			 
			// `NAME`, `ADDRESS1`, `ADDRESS2`, `CITY`, `REGION`, `ZIP_CODE`, `STATE`, `LATITUDE`, `LONGITUDE` 
			st.setString(1, loc.getName());
			st.setString(2, loc.getAddress1());
			st.setString(3, loc.getAddress2());
			st.setString(4, loc.getCity());
			st.setString(5, loc.getRegion());
			st.setInt(6, loc.getZipCode());
			st.setString(7, loc.getState());
			st.setFloat(8, loc.getLatitude());
			st.setFloat(9,loc.getLongitude());
			st.execute();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{ 
			closeConnections(st, conn);
		}